More on Two-Dimension Arrays..Help!

This is a discussion on More on Two-Dimension Arrays..Help! within the C++ Programming forums, part of the General Programming Boards category; So far I've got my program to accept prices for 10 items, calculate the sum, and in function CalcAvg, calculate ...

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

    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;
    }
    "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

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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


    ----------------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?
    "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
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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?
    "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

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    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....?
    Blue

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    columns are best handled using <iomanip>
    Blue

  7. #7
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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
    "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

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. a Problem about two dimension arrays and Fibonacci
    By louis_mine in forum C Programming
    Replies: 2
    Last Post: 10-17-2004, 12:33 AM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. 2 dimension arrays
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2001, 09:36 AM

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