Program Question

I'm a little confused as to how I would write the end of the if statement for infinityNorm. This is a Euclidean norm program. For example, the Euclidean norm of {6.7, 9.2, 4.3, 6.1} is the square root of (6.72+9.22+4.32+6.12
} = 13.6099. Wouldn't I return the value of the largest integer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iomanip>
using namespace std;

double euclideanNorm(double v[], int sz);
double infinityNorm(double v[], int sz);
int main() {
	const int N = 4;
	double data[N] = {6.7, 9.2, 4.3, 6.1} ;

	cout << "The Euclidean norm is " << fixed << setprecision(4) << 
		euclideanNorm(data,N)  << endl;
	//cout << "The Infinity norm is " << fixed << setprecision(4) << 
	//	infinityNorm(data,N)  << endl;
	return 0;
}
double euclideanNorm(double v[], int sz){
	int i;
	double sum = 0;
	for (i = 0; i < sz; i++)
		sum += v[i]*v[i];
	return sqrt(sum);
}
double infinityNorm(double v[], int sz){
	int i;
	double largest = abs(v[0]);
	for (i = 1; i < sz; i++)
		if (abs(v[i]) > largest);
			//  THIS LINE
	return largest;
}
if (abs(v[i]) > largest) largest = abs(v[i]);

They are doubles, not integers. The Euclidean norm isn't quite what you have described either.

For your sqrt() function you will also require in the header:
#include <cmath>
Last edited on
Topic archived. No new replies allowed.