Thread: Adding Arrays

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Adding Arrays

    I have done this very simple program a few times and it worked. Now its not and I cannot figure it out.

    Code:
     #include <stdio.h>
    
    #define MAXNUMBER 11
    
    
    void addfloat(float *, float *);
    
    
    int main(int argc, const char * argv[])
    {
    
    
    float array1[MAXNUMBER] = {10.2,12.8,11.9,18.3,19.5,29.8,19.4,15.4,13.6,13.8};
    float array2[MAXNUMBER] = {11.2,17.5,12.6,13.1,11.3,19.3,9.4,5.4,3.6,3.8};
    
       addfloat(&array1[MAXNUMBER], &array2[MAXNUMBER]);
    
       return 0;
    }
    
    
    void addfloat(float array1[],float array2[])
    {
       int i;
       float sum[MAXNUMBER];
    
       for (i=1; i < MAXNUMBER; i++) {
    
           sum[i] = array1[i] + array2[i];
    
    printf("Array1 + Array2 = %4.2f\n",sum[i]);
    }
    }

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Code:
    #include <stdio.h>
    
    
    #define MAXNUMBER 11
    
    
    
    
    void addfloat(float * array1, float * array2);
    
    
    
    
    int main( )
    {
        float array1[MAXNUMBER] = {10.2,12.8,11.9,18.3,19.5,29.8,19.4,15.4,13.6,13.8};
        float array2[MAXNUMBER] = {11.2,17.5,12.6,13.1,11.3,19.3,9.4,5.4,3.6,3.8};
    
    
        addfloat(array1, array2);
    
    
       return 0;
    }
    
    
    
    
    void addfloat(float * array1,float * array2)
    {
        int i;
        float sum[MAXNUMBER];
    
    
        for (i=1; i < MAXNUMBER; i++)
        {
            sum[i] = array1[i] + array2[i];
            printf("Array1 + Array2 = %4.2f\n",sum[i]);
        }
    }
    I'll get to what I did here in a few minutes.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by jocdrew21 View Post
    I have done this very simple program a few times and it worked. Now its not and I cannot figure it out.

    Code:
     #include <stdio.h>
    
    #define MAXNUMBER 11
    
    
    void addfloat(float *, float *);
    
    
    int main(int argc, const char * argv[])
    {
    
    
    float array1[MAXNUMBER] = {10.2,12.8,11.9,18.3,19.5,29.8,19.4,15.4,13.6,13.8};
    float array2[MAXNUMBER] = {11.2,17.5,12.6,13.1,11.3,19.3,9.4,5.4,3.6,3.8};
    
       addfloat(&array1[MAXNUMBER], &array2[MAXNUMBER]);
    
       return 0;
    }
    
    
    void addfloat(float array1[],float array2[])
    {
       int i;
       float sum[MAXNUMBER];
    
       for (i=1; i < MAXNUMBER; i++) {
    
           sum[i] = array1[i] + array2[i];
    
    printf("Array1 + Array2 = %4.2f\n",sum[i]);
    }
    }
    Step 1 : Fixed all of the indentation that was off in your code.

    Step 2 : Changed the function prototype to include the proper variable names instead of just a type like you probably did by mistake.

    Step 3 : You over-complicated the calling of the addfloat function. You just needed to pass a float pointer to it and you didn't need to include the types or anything.

    Step 4 : Fixed the addfloat function to just use float pointers instead of pointers to float pointers.

    Step 5 : Ran the program.
    Last edited by HelpfulPerson; 08-12-2013 at 09:22 AM.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Step 2 : Changed the function prototype to include the proper variable names instead of just a type like you probably did by mistake
    There is nothing technically wrong with his function prototype. However many people consider it a good practice to add the names, but it's not a requirement. The compiler doesn't actually use the variable names in the prototype, it's the names in the implementation that are important.

    Step 4 : Fixed the addfloat function to just use float pointers instead of pointers to float pointers.
    Do you realize that the following are equivalent?

    Code:
    void addfloat(float *, float *);
    void addfloat(float[], float[]);

    Also unless you're going to actually use the parameters to main() you shouldn't use that version. And the argv[][] argument should not be declared const.

    Jim

  5. #5
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by jimblumberg View Post
    There is nothing technically wrong with his function prototype. However many people consider it a good practice to add the names, but it's not a requirement. The compiler doesn't actually use the variable names in the prototype, it's the names in the implementation that are important.


    Do you realize that the following are equivalent?

    Code:
    void addfloat(float *, float *);
    void addfloat(float[], float[]);

    Also unless you're going to actually use the parameters to main() you shouldn't use that version. And the argv[][] argument should not be declared const.

    Jim
    1) I didn't know that, I've always been taught to add names in prototypes.
    2) I do, but I'm used to using pointers instead of arrays.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by HelpfulPerson View Post
    1) I didn't know that, I've always been taught to add names in prototypes.
    You do it (as a lot of other things like indentation) for programmer reading the code, not for compiler.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    2) I do, but I'm used to using pointers instead of arrays.
    Do you know a pointer is designated with a *, the [] denote arrays.

    Jim

  8. #8
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by jimblumberg View Post
    Do you know a pointer is designated with a *, the [] denote arrays.

    Jim
    I know that? Why are you asking me that?
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you asking me that?
    Because you don't seem to understand the concept.
    Code:
    // His original.
    void addfloat(float * array1,float * array2)
    {
    // Your modification.
    void addfloat(float array1[],float array2[])
    And then your statement:
    2) I do, but I'm used to using pointers instead of arrays.
    His original code is using pointer notation, your's is using array notation.

    Jim

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Thank you very much!!!

  11. #11
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by HelpfulPerson View Post
    Code:
    #include <stdio.h>
    
    
    #define MAXNUMBER 11
    
    
    
    
    void addfloat(float * array1, float * array2);
    
    
    
    
    int main( )
    {
        float array1[MAXNUMBER] = {10.2,12.8,11.9,18.3,19.5,29.8,19.4,15.4,13.6,13.8};
        float array2[MAXNUMBER] = {11.2,17.5,12.6,13.1,11.3,19.3,9.4,5.4,3.6,3.8};
    
    
        addfloat(array1, array2);
    
    
       return 0;
    }
    
    
    
    
    void addfloat(float * array1,float * array2)
    {
        int i;
        float sum[MAXNUMBER];
    
    
        for (i=1; i < MAXNUMBER; i++)
        {
            sum[i] = array1[i] + array2[i];
            printf("Array1 + Array2 = %4.2f\n",sum[i]);
        }
    }
    I'll get to what I did here in a few minutes.
    Quote Originally Posted by jimblumberg View Post
    Because you don't seem to understand the concept.
    Code:
    // His original.
    void addfloat(float * array1,float * array2)
    {
    // Your modification.
    void addfloat(float array1[],float array2[])
    And then your statement:

    His original code is using pointer notation, your's is using array notation.

    Jim
    Ummm, no. If you looked at where I posted the code that I fixed, then you would see that I didn't modify any of his functions in the way you are suggesting. I did leave the way he accessed the pointer as an array inside the function the same.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  12. #12
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    I must be having a bad day but I am getting the wrong values for total. total+=sum should store the last value, correct. sum0=21,sum1=30.3 etc. I am storing it and adding it them together in total. However I keep getting701.80 and I need 271.9. What is the deal here. It is add up every number on each line over and over?
    Code:
     #include <stdio.h>
    #define MAXNUMBER 10
    
    void addfloat(float * array1, float * array2);
    
    int main( )
    {
        float array1[MAXNUMBER] = {10.2,12.8,11.9,18.3,19.5,29.8,19.4,15.4,13.6,13.8};
        float array2[MAXNUMBER] = {11.2,17.5,12.6,13.1,11.3,19.3,9.4,5.4,3.6,3.8};
        
        addfloat(array1, array2);
        
        return 0;
    }
    
    void addfloat(float * array1,float * array2)
    {
        int i;
        float sum[MAXNUMBER];
        float total[MAXNUMBER];
        
        for (i=0; i < MAXNUMBER; i++)
        {
            sum[i] = array1[i] + array2[i];
            total[i]+= sum[i];
            printf("%4.1f + %4.1f = %4.1f\n",array1[i],array2[i],sum[i]);
    
        }
        printf("-----------------------\n");
        printf("TOTAL:%14.3f",total[i]);
    }

  13. #13
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I even tried
    Code:
    total[i]= sum[0]+sum[i+1];

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    total shouldn't be an array. Are there many totals or only one? I assume only one. And it of course needs to be initialized to 0.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I could have swore I did that but it didn't work. However I just did it and it worked.

    I am just going to keep making up programs and keep practicing. C++ starts in two weeks, any advice?

    Data Structures and Data Files are still giving me issues, I really need to practice those things a lot more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding numbers (Arrays)
    By ysuleman in forum C Programming
    Replies: 2
    Last Post: 11-21-2012, 03:12 PM
  2. Adding numbers with arrays
    By Oliveira in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 10:25 PM
  3. Now I need help adding arrays
    By nifear4 in forum C Programming
    Replies: 28
    Last Post: 10-30-2008, 10:18 AM
  4. Adding arrays with pointers - help
    By chip.litch in forum C Programming
    Replies: 2
    Last Post: 10-02-2008, 11:07 PM
  5. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 04:05 PM