Add two members two different arrays together, please help.

This is a discussion on Add two members two different arrays together, please help. within the C Programming forums, part of the General Programming Boards category; This is a function that is supposed to add two different arrays containing 9 items together, but it has a ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    5

    Exclamation Add two members two different arrays together, please help.

    This is a function that is supposed to add two different arrays containing 9 items together, but it has a problem when it comes to doing any actual adding, I would much appreciate it if someone could help me with this little project.
    Code:
    int * add_data(int x[], int y[], int z[])
    {
    
    
        int i;
        char a[9];
        for (i=1;i!=9;i++)
        {
            printf("%d\t\t=x\n", x[1]+y[1]);
            x[1] + y[1]= a[1]; 
        }
        return a;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,852
    I'm not going to fix your code - you'll learn more by fixing it yourself - but I will point out obvious problems you MUST address if you want your function to work. At present, it would not even compile. But you also have problems that will be acceptable to a compiler, but will not work when the code is executed.

    1) Array indices in C start at zero. So, for an array like "char a[9]", valid indices are 0 to 8. a[0] is the first, a[8] is the last. Accessing a[-1] or a[9] gives undefined behaviour.

    2) In your loop body, the only array elements accessed are x[1], y[1], and a[1]. You probably want to use x[i], y[i], and a[i], where the value of i needs to run from 0 to 8 (as per point 1).

    3) When performing an assignment, the thing being assigned is on the left hand side, not the right. So line 10 should be "a[1] = x[1] + y[1]" rather than "x[1] + y[1] = a[1]" - otherwise the code will not compile at all. Or, combined with my second point, it should probably be "a[i] = x[i] + y[i]"

    4) Even though compilers don't necessarily complain when you do it, arrays cannot be returned directly from functions. A pointer is also not an array. Furthermore, your array a is not guaranteed to exist any more after the function returns. Any attempt by the caller of your function to use the array (via the returned pointer) gives undefined behaviour, since the returned pointer points at something that has ceased to exist (i.e. it is a dangling pointer). Common symptoms of undefined behaviour are program crashes. One alternative is to pass the array a as an argument (or, since z is not being used in your function, put results in array z).

    5) You haven't shown code which calls the function. However, the caller needs to ensure it passes valid arrays for the arguments x,y,and z. And, as I said in my 4th point, your caller cannot use the returned pointer.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I suppose you should take x and y and put result into z instead of a
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    5

    Smile Thank you.

    Quote Originally Posted by grumpy View Post
    I'm not going to fix your code - you'll learn more by fixing it yourself - but I will point out obvious problems you MUST address if you want your function to work. At present, it would not even compile. But you also have problems that will be acceptable to a compiler, but will not work when the code is executed.

    1) Array indices in C start at zero. So, for an array like "char a[9]", valid indices are 0 to 8. a[0] is the first, a[8] is the last. Accessing a[-1] or a[9] gives undefined behaviour.

    2) In your loop body, the only array elements accessed are x[1], y[1], and a[1]. You probably want to use x[i], y[i], and a[i], where the value of i needs to run from 0 to 8 (as per point 1).

    3) When performing an assignment, the thing being assigned is on the left hand side, not the right. So line 10 should be "a[1] = x[1] + y[1]" rather than "x[1] + y[1] = a[1]" - otherwise the code will not compile at all. Or, combined with my second point, it should probably be "a[i] = x[i] + y[i]"

    4) Even though compilers don't necessarily complain when you do it, arrays cannot be returned directly from functions. A pointer is also not an array. Furthermore, your array a is not guaranteed to exist any more after the function returns. Any attempt by the caller of your function to use the array (via the returned pointer) gives undefined behaviour, since the returned pointer points at something that has ceased to exist (i.e. it is a dangling pointer). Common symptoms of undefined behaviour are program crashes. One alternative is to pass the array a as an argument (or, since z is not being used in your function, put results in array z).

    5) You haven't shown code which calls the function. However, the caller needs to ensure it passes valid arrays for the arguments x,y,and z. And, as I said in my 4th point, your caller cannot use the returned pointer.

    Thank you very much, I was able to fix the major problem, and it pretty much works now except one thing.

    This is my full program, it writes random numbers to two different arrays and then adds them both together. The only problem remaining is that the program generates the same pair of random numbers for each array, instead of each array having completely different numbers, they both have the exact same numbers. So when I finish running the program I end up with effectively double of each of the random numbers generated, I would be very grateful if you could point me in the right direction.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <time.h>
    #include <winclear.h>
    int * filldata(int x[]);
    int * add_data(int x[], int y[], int z[]);
    
    
    
    
    int main()
    {
        srand (time(NULL));
        int i;
        
        int dataqq[9];
        int atadqq[9];
        int final[9];
        filldata(dataqq);
        filldata(atadqq);
        for(i=0; i!= 9; i++)
            {
                printf("%d\t\t#1\n",dataqq[i]);
                printf("%d\t\t#2\n",atadqq[i]);
            }
        add_data(dataqq, atadqq, final);
        for(i=0; i!= 9; i++)
            {
                printf("%d\t\t#z\n",final[i]);
                //printf("%d\t\t#2\n",atadqq[i]);
            }
        
    }
    
    
    int * filldata(int x[])
    
    
    {
    
    
        
        srand (time(NULL));
        int i;
        for (i=0;i!=9;i++)
        {
                x[i]=rand()%1000;
        }
        return x;
    
    
    }
    int * add_data(int x[], int y[], int z[])
    
    
    {
    
    
        int i;
        for (i=0;i!=9;i++)
        {
            z[i] = x[i] + y[i];
        }
        return z;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,852
    Only call srand() once within your entire program, not each time filldata() is called. In short: remove line 46.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    Thank you very much))!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ pointer arrays as static class members
    By l1F in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2010, 04:55 AM
  2. Debugger : watching data members or arrays
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2009, 06:02 AM
  3. 4,000 something members
    By Driveway in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 09-02-2002, 06:36 PM
  4. Looking for Members
    By Esparno in forum C++ Programming
    Replies: 16
    Last Post: 03-28-2002, 01:11 PM
  5. Looking for members
    By Esparno in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-27-2002, 11:05 AM

Tags for this Thread


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