After sitting in a computer lab all day, I have decided to take a fresh, new approach to programming. If I don't think I'm doing it right, ignore that fact and keep writing code and pray it turns out well in the end. Since I don't think I lucked out and managed to actually know what I'm doing, I've posted here for help. Please keep in mind that this is the first time I've touched C++ and I barely have a grip on what I'm doing. If someone can take a quick look over this and tell me where I'm going wrong and give me some guidance I would greatly appreciate it. (I looked at the previous post and tried to alter the signs so my code is okay?)
Ah..changing a while loop into a for loop.
The original code is as follows:
And that is when I decided I needed help from you lovely folks..
Ah..changing a while loop into a for loop.
The original code is as follows:
#include <stdio.h>
main()
{
int i, j, sum=0, cnt=0, val=0, done;
float aver;
printf("Enter a value (or -1 to stop) => ");
scanf("%d", &val);
while(val>=0)
{
sum += val;
cnt++;
printf("Sum = %d, Cnt = %d\n", sum, cnt);
printf("Enter a value (or -1 to stop) => ");
scanf("%d", &val);
}
if(cnt == 0)
aver = 0.0;
else
aver = sum/(1.0*cnt);
printf("Average is %.2f\n", aver);
return 0;
}
Ahaha..and my code, which is showing the details of the loop and the preceding printf/scanf:
do{printf("Enter the number of data items =>");
scanf(%d, &val);
} while (num>=0);
for(i=0; i>=val; i++)
{printf("Enter a value (or-1 to quit)=>");
And that is when I decided I needed help from you lovely folks..
