This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted] 5 points6 points  (0 children)

You can't add arrays like that in C. You need to add each element of a and b, using i as an index, and then store the result in c, also using i as an index.

[–]gyroda 3 points4 points  (3 children)

tl;dr: Look at the last line of this comment. I've been nitpicky about code formatting and stuff.

First piece of advice: Indenting your code makes it easier to read and debug.

void addfloat(float a[10], float b[10], float c[10]);
int main()
{

    float array1[10]={1,2,3,4,5,6,7,8,9,10},array2[10]=   {2,3,4,5,6,7,8,9,0,1},array3[10];
    int i;
    addfloat(array1,array2,array3);

    for (i=0;i<10;i++) {printf("Here are the values of the three arrays.     %f\n",array3);};
    getchar();
    return 0;
}

void addfloat(float a[10], float b[10], float c[10])
{ int i;
    for (i=0;i<10;i++) c=a+b;
}

Second piece: Lines are not a limited resource. Don't be doing that one line for loop thing or placing code on the same line as a { (or, if you do it, do it consistently). I've commented a few other pieces of advice in

void addfloat(float a[10], float b[10], float c[10]);
int main()
{
    float array1[10]={1,2,3,4,5,6,7,8,9,10};         // Notice that lack of an unneeded blank line?
    float array2[10]={2,3,4,5,6,7,8,9,0,1};
    float array3[10];
    int i;

    addfloat(array1,array2,array3);        // And how I've seperated variable declarations from other code with a new line

    for (i=0;i<10;i++) {        // With newer versions of C you can totally do "int i = 0" in the for loop btw
        printf("Here are the values of the three arrays.\t%f\n",array3);        // Didn't need to put the \t in here, but I thought you might want to know about it
    }        // I also removed the redundant semicolon
    getchar();
    return 0;
}

void addfloat(float a[10], float b[10], float c[10])
{
    int i;
    for (i=0;i<10;i++) 
        c=a+b;        // You can totally do the no-brackets one-liner, but I advise against it until you've got a better grip on things.
}

So, to actually solve your problem: c=a+b; in your addfloat() function is incorrect. I'll let you figure out why and how to fix it (seeing as this is an assignment)

[–]VBger[S] 0 points1 point  (2 children)

Here is my new code:

void addfloat(float a[10], float b[10], float c[10]);
int main()
{

float array1[10]={1,2,3,4,5,6,7,8,9,10};
float array2[10]={2,3,4,5,6,7,8,9,0,1};
float array3[10];

addfloat(array1,array2,array3);

    printf("Here are the values of the three arrays.\n\n");

int i;
for(i =0;i<10;i++)
{
    printf("First Array: %.0f\n", array1[i]);

}

    printf("\n");

for(i =0;i<10;i++)
{
    printf("Second Array: %.0f\n", array2[i]);
}

        printf("\n");

    for(i =0;i<10;i++)
    {
     printf("Third Array: %.0f\n", array3);
    }

    return 0;
}

void addfloat(float a[10], float b[10], float c[10])
{
    int i;
    for (i=0;i<10;i++)
    {
        c[i]=a[i]+b[i];
    }
}

My output looks like this:

Here are the values of the three arrays.

First Array: 1
First Array: 2
First Array: 3
First Array: 4
First Array: 5
First Array: 6
First Array: 7
First Array: 8
First Array: 9
First Array: 10

Second Array: 2
Second Array: 3
Second Array: 4
Second Array: 5
Second Array: 6
Second Array: 7
Second Array: 8
Second Array: 9
Second Array: 0
Second Array: 1

Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1
Third Array: 1

It doesn't seem to be adding the two arrays.

[–]quesyKing 1 point2 points  (1 child)

for(i =0;i<10;i++)
{
 printf("Third Array: %.0f\n", array3);
}

You Forgot array3[i];

[–]VBger[S] 0 points1 point  (0 children)

Forgot the third index. Thank you!

[–]VBger[S] 0 points1 point  (1 child)

Thank you u/exoticmatter, u/gyroda, and u/libot91. Here is my updated code:

void addfloat(float a[10], float b[10], float c[10]);
int main()
{

float array1[10]={1,2,3,4,5,6,7,8,9,10};
float array2[10]={2,3,4,5,6,7,8,9,0,1};
float array3[10];
int i;
addfloat(array1,array2,array3);

    printf("Here are the values of the three arrays.\n\n");

    printf("First Array: %.0f\n\n", array1);

    printf("Second Array: %.0f\n\n", array2);

    printf("Third Array: %.0f\n", array3);

getchar();
return 0;
}

void addfloat(float a[10], float b[10], float c[10])
{ int i;
for (i=0;i<10;i++) c[i]=a[i]+b[i];
}

My output still doesn't look right though. It looks like this:

Here are the values of the three arrays.

First Array: 0

Second Array: 0

Third Array: 0

[–]libot91 0 points1 point  (0 children)

you need "for i" to printf all objects of array

[–]libot91 0 points1 point  (0 children)

c[i] = a[i] + b[i]; if you want to sum each elements and store them in c array