# Thread: Question in Parallel Arrays

1. ## Question in Parallel Arrays

I'm trying to understand how parallel arrays work..how can I print the price of an item in one column and the difference between average and the price in the next column- for all ten items?

Code:
```#define Max 10
#include <iomanip.h>
#include <stdlib.h>

main()
{  float Price[Max],Avg;int Diff; float Sum=0; int C; int Price[10];

for(C=0;C<Max;C++)
{Price[Max]=0;}
for(C=0;C<Max;C++)
{ cout<<"\n Enter Price "<<C+1<<" : ";
cin>>Price[C];}
for(C=0;C<Max;C++)
{ Sum=Sum+Price[C];}

Avg=Sum/9;

for(C=0;C<Max;C++)         this is the part where I don't know how to print price in one column and the difference between average and price in the next column..how?
{cout<<Price[C][Avg];}

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;
}```
Thanks

2. declare an array of known size to hold the names of the items:
char item[10]30];

declare an array of same size as first array to hold the price of the item
double price[10];

calculate average price or get it ffrom some other source

for each item in the array calculate the difference between the items price and the average price and store it in the average price price array.
double difference[10];

use a loop to display results:

for(i = 0; i < 10i++)
{
cout << item[i] << " has a price of " << price[i] << " which is " <<
difference[i] << " from the average price." << endl;

3. I still don't understand is that example being used along with what I have or are those totally new variables? Does anyone have the time to explain it a little more clearly please?

4. >for(C=0;C<Max;C++)
> {Price[Max]=0;}

This seems not right to me. I guess you meant

Code:
```for(C=0;C<Max;C++)
{Price[C]=0;}```
You could declare a 2D array:

float Item [Max][2];

Where first column is the price and second column the difference between price and average.

Code:
```// get prices
for (index = 0; index < MAX; index++)
{
price = get_price ();
Item [index][0] = price;
}

// calculate average
....

// calculate differences
for (index = 0; index < MAX; index++)
{
diff = Item [index][0] - avg;
Item [index][1] = diff;
}```
Code:
```for (index = 0; index < MAX; index++)
cout << Item [index][0] << " "<< Item [index][1] << endl;

price        diff
column     column

value       value
value       value
value       value```

5. when someone uses the term parallel array I think of two different arrays rather than a 2D array, alhtough functionally they are equivalent.

Code:
```double price[4];
double sum = 0;
for(i = 0; i < 4; i++)
{
cout << "enter price" << endl;
cin >> price[i];
sum += price[i];
}

double avg = sum/4;

long double difference[4];
for(i = 0; i < 4; i++)
{
difference[i] = fabs(price[i] - avg);
}

for(i = 0; i < 4; i++)
{
cout << "item " << i + 1 << " has price of " << price << " which
differs from the average price by " << difference << endl;
}```
array price and difference are parallel because element i in both arrays refer to the same "item".

Popular pages Recent additions