-
Parallel Arrays-HELP!
I'm using Bloodshed..
I'm trying to make this program accept input for prices of 10 items, calculate and print sum, then the average price of all. I need to print the difference between the item price and average in their respective columns. I have NO idea where to get started with the 2d Arrays..I started with a regular array to test it, but I get crazy output- I noticed one thing.When the inputs are less than 20. or above, the output is fine..Can anyone Help??
Code:
#define Max 9
#include <iomanip.h>
#include <stdlib.h>
main()
{ float Price[Max], Sum, Avg;int C;
for(C=0;C<Max;C++)
{cout<<"\n Enter Price "<<C<<" : ";
cin>>Price[C];
}
for(C=0;C<Max;C++)
{ Sum=Sum+Price[C];}
Avg=Sum/9.0;
cout<<"\n Sum = "<<Sum<<endl;
cout.setf(ios::floatfield, ios::showpoint);
cout.setf(ios::fixed);
cout<<setprecision(2);
cout<<Avg<<endl;
system("PAUSE");
return 0;
}
***Output
Enter Price 0 : 12.99
Enter Price 1 : 13.99
Enter Price 2 : 14.99
Enter Price 3 : 15.99
Enter Price 4 : 16.99
Enter Price 5 : 17.99
Enter Price 6 : 18.99
Enter Price 7 : 19.99
Enter Price 8 : 20.99
Sum = 1.0442e+34
1160222550475306377922293111193600.00
Press any key to continue . . .
Please be merciful :(
-
Does this work?
Code:
#define Max 9
#include <iomanip.h>
#include <stdlib.h>
main()
{
float Price[Max], Avg;
float Sum = 0;
int C;
for (C=0;C<Max;C++)
{
Price[Max] = 0;
}
for (C=0;C<Max;C++)
{
cout<<"\n Enter Price "<<C<<" : ";
cin>>Price[C];
}
for(C=0;C<Max;C++)
{
Sum=Sum+Price[C];
}
Avg=Sum/9;
cout<<"\n Sum = "<<Sum<<endl;
cout.setf(ios::floatfield, ios::showpoint);
cout.setf(ios::fixed);
cout<<setprecision(2);
cout<<Avg<<endl;
system("PAUSE");
return 0;
}
-
Don't forget to initilize your variables.
int i;
i++;
Will produce undesired results.
int i = 0;
i++;
Should give you the desired result.
-
Ahh Yes..it works like a charm..thanks a lot ;)