Thread: Sparse Vector Homework Help

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    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 [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?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 [0] to array[49]. (not 51 as you have it now - that's an error, make it array[50], 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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    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 ....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find inverse of a sparse matrix ?
    By iamc in forum C Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 AM
  2. Accessor for Sparse Matrix
    By Clark in forum C Programming
    Replies: 1
    Last Post: 04-02-2010, 05:14 PM
  3. Sparse Matrix
    By brb9412 in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 01:12 PM
  4. Genrate sparse matrix - C99
    By anirban in forum C Programming
    Replies: 0
    Last Post: 06-10-2008, 11:44 PM
  5. how to implement SPARSE graph in c/c++?
    By howhy in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2005, 08:23 AM