Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey guys, I'm having a little trouble with writing norms and then putting them into classes, I'm doing a masters at university and we've got a programming course, and so far my life experience of C++ is four hours of lectures so what I'm going to write is probably quite off the mark, anyway I'm trying to calculate the 1, 2 and sup (infinity) norms of vectors in C++ and then put these functions into "a class with hidden i.e. private data structures of vectors". Here is what I've done for the vectors, I appreciate it probably reads similar to how a child trying to play a concerto on a violin after four hours practice would sound, but here goes:
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int i,n;
vector<double> v(n);string onenorm, twonorm, infinitynorm;
//Calculating The 1 Norm
int main ()
{
cout<<"Input size of vector"<<endl;
cin>>n;
//cout<< "Input the values of the vector you wish to have";double sum [i];
sum[0]=0;
for (int i=0; i<=n-1; i++)
{
sum[i+1]= sum[i] + abs(v[i]);
}onenorm=sum[n-1];
int p;
double pow (double& Variable,double& p)//Calculating the 2 Norm
double sumtwonorm[i];
sumtwonorm[0]=0;
for (i=0; i<=n-1; i++)
{
sumtwonorm[i+1]= sum [i] + pow (double V[i],int 2);
}
twonorm = pow (double sumtwonorm[n-1], double 1/2);//Calculating the Infinity Norm
for (i=0; i<=n-1; i++)
{
infinitynorm=0;if ( V[i]>infinitynorm
infinitynorm=V[i]);}
/* infinity norm should hopefully now be given by typing e.g cout<< infinitynorm; */
}
Could anyone give me any pointers as to what to do?

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |