#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
struct Vector{
short *x;
int n; //numarul de elemente
};
int getN (Vector *v){
return v->n;
}
void setN (Vector *v, int nr){
if (nr<1) v->n=1;
else v->n=nr;
}
short getX (Vector *v, int pos){
if ((pos>=0)&&(pos<getN(v)))
return v->x[pos];
cout << "Position Error" << endl;
return 0;
}
void setX (Vector *v, int pos, short value){
if ((pos>=0)&&(pos<getN(v)))
v->x[pos]=value;
else cout << "Position Error" << endl;
}
void initVector(Vector *v,int max){
setN(v,max);
short *ptr=new short[v->n];
v->x=ptr;
for (int i=0; i<v->n; i++)
v->x[i]=0;
}
void deleteVector (Vector *v){
delete[] v->x;
v->n=NULL;
}
short SumPar (Vector *v){
short sum=0;
for (int i=1; i<getN(v); i+=2)
sum+=getX(v,i);
return sum;
}
double Norma (Vector *v){
short sum=0;
for (int i=0; i<getN(v); i++)
sum+=getX(v,i)*getX(v,i);
return sqrtf(sum);
}
void main(){
Vector *tab;
int N,num;
short val;
cout << "Dati numarul de vectori: ";
cin >> N;
if (N==0) return;
tab = new Vector[N];
for (int i=0; i<N; i++){
cout << "dati numarul de elemente a vectorului " << (i+1) << ": ";
cin >> num;
initVector(&tab[i], num);
cout << "dati elementele: ";
for (int j=0; j<getN(&tab[i]); j++){
cin >> val;
setX(&tab[i],j,val);
}
}
//Afisarea vectorilor
for (int i=0; i<N; i++){
cout << "v" << (i+1) << ": ";
for (int j=0; j<getN(&tab[i]); j++)
cout << getX(&tab[i],j) << " ";
cout << endl;
}
//Afisarea sumelor
for (int i=0; i<N; i++)
cout << "\ns" << (i+1) << "=" << SumPar(&tab[i]);
//Compararea vectorilor
cout << "\ncare 2 vectori doriti sa comparati?" << endl;
int v1,v2;
double n1,n2;
cin >> v1 >> v2;
if (v1<=0||v1>N||v2<=0||v2>N)
cout << "\nEroare. Nu exista astfel de vectori\n";
else {
n1=Norma(&tab[v1-1]);
n2=Norma(&tab[v2-1]);
if (n1>n2) {
cout << "v" << v1 << " > v" << v2 << endl;
cout << n1 << " > " << n2 << endl;
}
else if (n1==n2){
cout << "v" << v1 << " = v" << v2 << endl;
cout << n1 << " = " << n2 << endl;
}
else {
cout << "v" << v1 << " < v" << v2 << endl;
cout << n1 << " < " << n2 << endl;
}
}
//Stergerea tuturor vectorilor
for (int i=0; i<N; i++)
deleteVector(&tab[i]);
getch();
}