C Programming Problem Create two single dimension arrays that contain 10 floatin
ID: 665882 • Letter: C
Question
C Programming Problem
Create two single dimension arrays that contain 10 floating-point numbers in each array. Create a third single dimension array to hold a sum. Your main program will take the two arrays of float and pass them to the function addfloat() Inside the function, add the first array to the second array and put the total into the third array. Print the values of all three arrays back in the main program. My code wasn't working, I found this one elsewhere, but line 6 where it says 'for (d = 0; d < 10; d++)' keeps giving me errors. Anyone have any idea why?
#include <stdio.h>
void addfloat(float a[], float b[], float c[]);
int d = 0;
for (d = 0; d < 10; d++)
{
c[d]=a[d]+b[d];
}
int main()
{
float RCT1[10] = {3.22, 9.99, 23.45, 1.50, 2.63, 11.50, 13.49, 100.88, 8.00, 33.19};
float RCT2[10] = {9.99, 8.33, 29.99, 14.07, 27.39, 12.28, 1.79, 47.99, 10.00, 13.99};
float TOT[10];
int a;
addfloat(RCT1, RCT2, TOT);
printf(" Receipt 1 total: ");
for (a = 0; a < 10; a++)
{
printf("%2.2f", RCT1[a]);
}
printf(" Receipt 2 total: ");
for (a = 0; a < 10; a++)
{
printf("%2.2f", RCT2[a]);
}
printf(" Total of receipts combined; ");
for (a = 0; a < 10; a++)
{
printf("%2.2f", TOT[a]);
}
return 0;
}
Here's the errors im geting.
main.c|6|error: expected identifier or '(' before 'for'|
main.c|6|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token|
main.c|6|error: expected '=', ',', ';', 'asm' or '__attribute__' before '++' token|
Explanation / Answer
#include <stdio.h>
void addfloat(float a[], float b[], float c[]);
int d = 0;
for (d = 0; d < 10; d++)
{
c[d]=a[d]+b[d]
}
int main()
{
float RCT1[10] = {3.22, 9.99, 23.45, 1.50, 2.63, 11.50, 13.49, 100.88, 8.00, 33.19};
float RCT2[10] = {9.99, 8.33, 29.99, 14.07, 27.39, 12.28, 1.79, 47.99, 10.00, 13.99};
float TOT[10];
int a;
addfloat(RCT1, RCT2, TOT);
printf(" Receipt 1 total: ");
for (a = 0; a < 10; a++)
{
printf("%2.2f", RCT1[a]);
}
printf(" Receipt 2 total: ");
for (a = 0; a < 10; a++)
{
printf("%2.2f", RCT2[a]);
}
printf(" Total of receipts combined; ");
for (a = 0; a < 10; a++)
{
printf("%2.2f", TOT[a]);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.