Thread: Help. Simple Array Program.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Help. Simple Array Program.

    The goal of this code is to find the inner product of two arrays and then to take the first array and "reverse" the elements of the values in the array. I've figured out the inner product part, but for some reason my reverse function isn't working right. It reverses the first two but then doesn't work for the remaining values. Any help would be appreciated. Thank you ahead of time.
    Code:
    #include <stdio.h>
    
    int inner_product(int a[], int b[], int count);
    int reverse(int a[], int count);
    
    int main(void)
    {
        int a[4] = {1, 4, 3, 5};
        int b[4] = {2, -1, 1, 4};
        int count = 0;
    
        inner_product(a, b, count);
        reverse(a, count);
    
        return 0;
    }
    
    int inner_product(int a[4], int b[4], int count){
        int total = 0;
        for(count = 0; count < 4; count++){
            total = (a[count] * b[count]) + total;}
        printf("%d\n", total);
        return 0;}
    
    int reverse(int a[4], int count){
        printf("%d ", a[0]);
        printf("%d ", a[1]);
        printf("%d ", a[2]);
        printf("%d\n", a[3]);
        int x = 3;
        for(count = 0; count < 4; count++){
            a[count] = a[x];
            x = x - 1;}
        printf("%d ", a[0]);
        printf("%d ", a[1]);
        printf("%d ", a[2]);
        printf("%d\n", a[3]);
        return 0;}

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Hi,

    you are actually overwriting the values of a in this part of the code
    Code:
    a[count] = a[x];
    check the logic there again.

    thanks and regards,
    satya

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You seem to be using the count parameter incorrectly. It's presumably supposed to be the size of the arrays, not to be used as an index variable for the loop! Declare a local variable (say i) inside the functions for their loops.

    You should have your inner_product function return its result to main, and you could print the result there.

    You should declare your reverse function as void and return nothing. And it would be better if it didn't print out the array at all, but simply reversed it (like its name says). You should write a separate print function and call it from main before and after calling reverse.

    As for the reversing algorithm, you need to "swap" the elements, otherwise you overwrite them as Satya said. And you should only loop up to the halfway point, otherwise you'll swap them back again!

    So, if you follow the above advice, your functions would be declared as
    Code:
    int inner_product(int a[], int b[], int size);
    void reverse(int a[], int size);
    void print(int a[], int size);
    and main would look like
    Code:
    #define SIZE 4
    
    int main(void)
    {
        int a[SIZE] = {1, 4, 3, 5};
        int b[SIZE] = {2, -1, 1, 4};
    
        print(a, SIZE);
        print(b, SIZE);
        printf("%d\n", inner_product(a, b, SIZE));
    
        reverse(a, SIZE);
        print(a, SIZE);
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    I appreciate all the help you two, but I'm still having trouble with the reverse function. I've gotten it so that it would reverse the last two numbers but not the first two, which is the opposite of what I was doing the first go round...

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Unless you post your latest code, there's not much we can do to help you!

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Try making another array and putting the original array in to the new one backwards.

    ex.
    Code:
          x=4;
          for(count=0;count<4;count++)
    	{
    		array2[count]=array[x];
    		x--;
    	}
          print array2
    Last edited by camel-man; 03-09-2012 at 01:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Array Program
    By Chemeng11 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2011, 08:09 PM
  2. Seemingly simple array program yielding bizarre output
    By alter.ego in forum C Programming
    Replies: 7
    Last Post: 04-07-2011, 11:15 AM
  3. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  4. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  5. Array question on my simple program?
    By correlcj in forum C++ Programming
    Replies: 11
    Last Post: 10-21-2002, 02:16 PM