# Thread: Sparse Vector Homework Help

1. ## Sparse Vector Homework Help

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 , x;
double value , 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? 2. When you're reading values in, the integers are indexes, and the corresponding double is the element at that index.

The element corresponding to indexes you have not input are deemed to be zero.

Beyond that, the assignment is pretty self-explanatory. I can see no way to clarify on that without actually giving you the required answer - which would defeat the purpose of the exercise (you learning by doing it yourself). 3. For the minimum and maximum values, create two appropriately named variables. Assign their initial value, to the first element of the array (both the same, at this point).

Now go through the array with a for loop from array  to array. (not 51 as you have it now - that's an error, make it array, and use 0 to 49 for your indeces.)

Anyway, if(array[i] greater than maxValue in array, then maxValue equals array[i]. Repeat with minValue, switching the greater to less than.

For H, just sum up your array values, and when you're done, use square root function in math.h. 4. Originally Posted by Adak For H, just sum up your array values, and when you're done, use square root function in math.h.
Actually, compute the sum of squares of array values .... Popular pages Recent additions 