# More on Two-Dimension Arrays..Help!

• 04-03-2002
ProgrammingDlux
More on Two-Dimension Arrays..Help!
So far I've got my program to accept prices for 10 items, calculate the sum, and in function CalcAvg, calculate the average. The next part of my assignment I'm totally blind on how to do ..

I need to make another function to print the price of each item and the difference between average and price of each item in column format.

HOW?? I'm pretty much a beginner, so please try to break it down if u can..thanks

Code:

```#define Max 10 #include <iomanip.h> #include<stdlib.h> float CalcAvg(float Sum, int C)  { return (Sum/Max); }   main()   {  float Price[Max],Avg;  float Sum=0;           int C, I;           for(C=0;C<Max;C++)       {Price[Max]=0;         cout<<"\n Enter Price "<<C+1<<" : "; cin>>Price[C];       Sum=Sum+Price[C];}         cout<<" Avg= "<<CalcAvg(Sum,C);         cout<<"\n Sum = "<<Sum<<endl;         Avg=CalcAvg(Sum,C); //UP TO THIS PART IS CORRECT       for(C=0;C<Max;C++)         {                      } //here is where I don't know what to do..do I even need this loop?         cout.setf(ios::floatfield, ios::showpoint);         cout.setf(ios::fixed);         cout<<setprecision(2);   system("PAUSE");       return 0; }```
• 04-03-2002
outside the for loop:

double difference;

inside the for loop:

if average < price[i];
difference = price[i] - average;
else
difference = average - price[i];

then display whatever you want.

if you need to do it in a function think about what you need to send to the function to do what is needed therein and send the appropriate information as parameters.
• 04-03-2002
ProgrammingDlux
thanks elad..my problem is not the actual calculation of finding average...instead it's how to put in column format, the price of each item and the difference between average and price..like

Quote:

----------------Price------------Difference
Item 1--------10.99------------- 2.49
Item 2--------12.99------------- 1.39
//...------------ //..------------- //...

something like that..how do I use a Parallel array given what I have already declared in my program?
• 04-03-2002
ProgrammingDlux
I'm still restless..I'm trying things, but nothing seems to work..

Code:

``` #define Max 10 #include<iomanip.h> #include<stdlib.h> float CalcAvg(float Sum, int C)  { return (Sum/Max); }   main()   {  float Price[Max],Avg;  float Sum=0;           int C, I;           for(C=0;C<Max;C++)       {Price[Max]=0;         cout<<"\n Enter Price "<<C+1<<" : "; cin>>Price[C];       Sum=Sum+Price[C];}         cout<<" Avg= "<<CalcAvg(Sum,C);         cout<<"\n Sum = "<<Sum<<endl;         Avg=CalcAvg(Sum,C); //UP TO THIS PART IS CORRECT       for(C=0;C<Max;C++)         {  float Val[Max][2]={Price[C]};  //this is where I'm having problems           cout<<Val[Max][2]<<" ";}         cout.setf(ios::floatfield, ios::showpoint);         cout.setf(ios::fixed);         cout<<setprecision(2);   system("PAUSE");       return 0; }```
If you look at the post directly above this one, that is what I want the output to look like..How do i do that?
• 04-03-2002
Betazep
float CalcAvg(float Sum, int C)
{ return (Sum/Max); }

Why do you pass C into this function when it isn't used?

Code:

```        cout<<" Avg= "<<CalcAvg(Sum,C);         cout<<"\n Sum = "<<Sum<<endl;         Avg=CalcAvg(Sum,C); //UP TO THIS PART IS CORRECT why not         Avg=CalcAvg(Sum,C);         cout<<" Avg= "<< Avg;         cout<<"\n Sum = "<<Sum<<endl;```

for(C=0;C<Max;C++)
{ float Val[Max][2]={Price[C]}; //this is where I'm having problems
cout<<Val[Max][2]<<" ";}

This loops your instantiation over and over again. Declare your variable outside of the loop.

cout.setf(ios::floatfield, ios::showpoint);
cout.setf(ios::fixed);
cout<<setprecision(2);

You are doing this at the end of your program because....?
• 04-03-2002
Betazep
columns are best handled using <iomanip>
• 04-03-2002
ProgrammingDlux
thanks for your help, but sorry to say- I'm still in the Dark!! either It's so simple that I'm just overlooking it, or I'm just a real *bleep*ing idiot..either way..I need help..if someone would like to help, please describe it as if I was a baby..please :o
• 04-04-2002
go here to read about the stream manipulators/modifiers/flags used to format output.

http://www.stud.fim.ntnu.no/~oystesk/CPP/htm/ch16.htm

Unfortunately, the tables in the book aren't reproduced here, but I think the examples will be enough to get the job done:

here's a crude start:

for(int i = 0; //etc.
{
cout.width(15);
cout.setf(ios::left);
cout >> Name[i];

cout.width(10);
cout.sef(ios::left);
cout >> Price[i];

cout >> Difference[i];
}

be sure to include the appropriate headers for the various manipulators, flags, etc. If you want to fill the space between columns there is even a manipulator to do that.