Unexpected Output - Programming Exercise (C)
From C Primer Plus 5th Ed: "Write a program that creates two eight-element arrays of doubles and uses a loop to let the user enter values for the eight elements of the first array. Have the program set the elements of the second array to the cumulative totals of the elements of the first array. For example, the fourth element of the second array should equal the sum of the first four elements of the first array, and the fifth element of the second array should equal the sum of the first five elements of the first array. (It's possible to do this with nested loops, but by using the fact that the fifth element of the second array equals the fourth element of the second array plus the fifth element of the first array, you can avoid nesting and just use a single loop for this task.) Finally, use loops to display the contents of the two arrays, with the first array displayed on one line and with each element of the second array displayed below the corresponding element of the first array."
I've written:
#include <stdio.h>
int main(void)
{
double first[8];
double second[8];
int i;
printf("Enter 8 'doubles':\n");
i = 0;
scanf("%lf", &first[i]);
second[i] = 开发者_运维问答first[i];
i = 1;
while(i < 8)
{
scanf("%lf", &first[i]);
second[i] = first[i] + first[i-1];
i++;
}
for(i = 0; i < 8; i++)
{
printf("%.2lf ", first[i]);
}
printf("\n");
for(i = 0; i < 8; i++)
{
printf("%.2lf ", second[i]);
}
}
My output is:
Enter 8 'doubles':
1
1
1
1
1
1
1
1
1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
1.00 2.00 2.00 2.00 2.00 2.00 2.00 2.00 Press any key to continue . . .
Expected output is with 1 2 3 6 12 24 48 96
on the bottom row. Can't figure out for the life of me why it's only working for the first iteration. What have I missed?
Thanks in advance.
EDIT: No idea as to why it's down as 'homework', I tagged it as an exercise... Ah well.
EDIT2:
This code:
#include <stdio.h>
int main(void)
{
double first[8];
double second[8];
int i;
printf("Enter 8 'doubles':\n");
i = 0;
scanf("%lf", &first[i]);
second[i] = first[i];
i = 1;
while(i < 8)
{
scanf("%lf", &first[i]);
second[i - 1] = first[i] + first[i-1];
i++;
}
for(i = 0; i < 8; i++)
{
printf("%.2lf ", first[i]);
}
printf("\n");
for(i = 0; i < 8; i++)
{
printf("%.2lf ", second[i]);
}
}
This output:
1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
2.00 2.00 2.00 2.00 2.00 2.00 2.00 -925596313493178310000... etc
Well there is a problem here:
while(i < 8)
{
scanf("%lf", &first[i]);
second[i] = first[i] + first[i-1];
i++;
}
What happens when i is zero?
And your new code has an off-by-one error at:
second[i - 1] = first[i] + first[i-1];
If you think about it, the last element of second is never written to.
You need to sum each value of the first array as you loop, so why not declare a variable to hold this value, and then assign it to the corresponding element of the second variable. This makes everything much simpler.
#include <stdio.h>
int main(void)
{
double first[8], second[8], sum = 0.0;
int i = 0;
printf("Enter 8 'doubles':");
while(i < 8)
{
scanf("%lf", &first[i]);
sum += first[i];
second[i] = sum;
i++;
}
printf("\nfirst - ");
for(i = 0; i < 8; i++)
{
printf("%7.2f ", first[i]);
}
printf("\nsecond - ");
for(i = 0; i < 8; i++)
{
printf("%7.2f ", second[i]);
}
return 0;
}
The other small mistake is using %lf for the printf() format string - printf() actually only uses %f for both doubles & floats
There's a problem the first time you enter the while (i<8)
loop: i
is still zero then, so you'll be overwriting first[0]
and reading from first[-1]
which is illegal. Also:
second[i] = first[i] + first[i-1];
second[i]
will contain the sum of the last two floats entered. If you want a cumulative sum, you should probably be adding second[i-1]
.
second[i] = first[i] + first[i-1];
You entered '1' for every index of first, so you're always adding 1 + 1 (except for the first element). Looks fine to me.
精彩评论