My assignment (due Tuesday night) is to:
Write a C program that:
a) Declare an integer array index and a double array value both with
room for 50 elements;
b) Reads the integer values of n and nzeros from the terminal by
using scanf;
c) Exit with error message if n > 50.
d) In a for loop, read an integer and a double value from the
terminal by using scanf and store the integer in the index array
and the double in the value array sequentially; You should repeat
this operation nzeros times.
e) Print the sparse vector exactly as shown below;
f) Compute and print the index and value of the maximum element of
the vector;
g) Compute and print the index and value of the minimum element of
the vector;
h) Compute and print the Euclidean norm of the vector;
HINT: The Euclidean norm is the standard square root of the sum of
the squares of the entries of a vector.
Use the files hw6.input and hw6.input2 provided to test your program.
The output of your program should like:
.................................................. ......................
% ./a.out < hw6.input
50-dimensional sparse vector with 10 nonzero entries:
2: 3.0000 3: -1.2000 7: 2.8000
8: 3.4000 10: 7.9300 11: -2.6300
20: 8.5600 30: -0.4000 31: 2.3000
39: 3.9000
Maximum element has index 20 = 8.560000
Minimum element has index 11 = -2.630000
Euclidean norm = 13.913138
% ./a.out < hw6.input2
50-dimensional sparse vector with 8 nonzero entries:
2: 3.0000 3: -1.2000 7: 2.8000
8: 3.4000 10: 7.9300 11: -2.6300
20: 8.5600 30: -0.4000
Maximum element has index 20 = 8.560000
Minimum element has index 11 = -2.630000
Euclidean norm = 13.155812
I have everything down to E, but I just do not know where to begin when it comes to the last three. I can imagine it involves if statements, but how they're utilized, I have no idea. Here's what I have so far:
Code:
#include <stdio.h>
#include <math.h>
main ()
{
int i, n, nzeros, index [51], x;
double value [51], y;
printf("Enter value of n: ");
scanf("%d",&n);
if(n>50 && n<0)
{
printf("DOES NOT COMPUTE!\n");
return 0;
}
printf("Enter value of nzeros: ");
scanf("%d",&nzeros);
for(i=0; i<nzeros; i++)
{
printf("Input int: ");
scanf("%d",&x);
printf("Input double: ");
scanf("%lf",&y);
index[i]=x;
value[i]=y;
}
for(i=0; i<nzeros; i++)
{
if((i)%3==0)
{
printf("\n");
}
printf("%d: %f ", index[i], value[i]);
}
}
Can you guys give me any hints about how to complete F, G, and H?