Multiplying arrays (dot vectors)

This is a discussion on Multiplying arrays (dot vectors) within the C Programming forums, part of the General Programming Boards category; Hello, I was in need of assistance with an assignment. I have gotten help from a friend that knows how ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Multiplying arrays (dot vectors)

    Hello,
    I was in need of assistance with an assignment. I have gotten help from a friend that knows how to program and yet it still isn't quite working correctly. What I need to have is a prog. that asks for the amount of numbers in an array (less then 10), then asks for the user to input these numbers into the first vector (1..4..2..5) then prompts the user to enter numbers in for the second vector. I have got most of the code but the program is not functioning properly and i am not sure of how to fix it. any assistance is helpful.

    thank you,
    Matter

    Code:
    int dotproduct(int a[], int b[], int n)
    { 
        int result; 
        result = 0;
        for (n=0;n<10;n++)
     {
        result=result+a[]*b[];
      }
        return result;
    }
    
    int main()
    {
        int n, r, a[10], b[10]; // this will not allow me to compile without numbers in the array?
        char compute;
        compute=y;
    
        while (compute==y)
     {
        printf("enter number of terms: ");
        scanf("%d\n", &n);
    
        while (n>=10)
      {
        printf("number entered must be less then ten, please try again:");
        n=0;
        scanf("%d\n", &n);
       }
        
        printf("enter first vector: ");
        for (;n>0;n--)
      {
        scanf("%d\n", a[])
       }
    
        printf("enter second vector: ");
        scanf("%d\n", b[]);
    
        r=dotproduct(a, b, n);
    
        printf("the dot product is:%d", &r);
    
        printf("compute another dot product? (y/n) ");
        scanf("%c\n", compute);
      }
    
        return 0;
    }
    I apologize if this is sloppy, im a first year student who is trying to learn. any help is great.

    thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are using "n" to remember the number of vectors, and you trash it on vector A. You need a second variable to remember the count.

    Your scanf into vector A needs an index. You would be better off looping "up to and less than n" instead of "down from n", and use the loop control variable to index into vector A.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2

    Thanks

    Thank you,
    but the program is still not functioning correctly. after compiling and running it asks how many terms, but upon typing a number and pushing enter it does nothing? any ideas?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Post your updated code.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplying Huge numbers in integer arrays
    By invierno in forum C Programming
    Replies: 5
    Last Post: 04-11-2009, 08:40 PM
  2. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  3. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  4. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 11:11 AM
  5. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21