Question in Parallel Arrays

This is a discussion on Question in Parallel Arrays within the C++ Programming forums, part of the General Programming Boards category; I'm trying to understand how parallel arrays work..how can I print the price of an item in one column and ...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    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. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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?
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >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;
    
    would lead to
    
    price        diff
    column     column
    
    value       value
    value       value
    value       value

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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 subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 06:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 11:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 01:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 03:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21