home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,957 of 33,346   
   Ike Naar to nvangogh   
   Re: calculate 'average price' problem   
   30 Mar 13 03:25:56   
   
   From: ike@iceland.freeshell.org   
      
   On 2013-03-29, nvangogh  wrote:   
   > The program is simple - C++ primer 5th edition exercise 7.1   
   > Program should take 3 input values:   
   > - a 'isbn' - std::string   
   > - number of copies sold - unsigned int   
   > - sales price per unit - double   
   >   
   > using a data structure only with 3 variables (see the code), the   
   > program must output for each isbn - number of copies sold, calculate   
   > total revenue from sales and calculate and display the average   
   > price.   
   >   
   > I have done nearly all of it, but cannot figure out how to calculate   
   > the average price in the loop. This is simple, very simple. Can you   
   > tell me what is wrong brother? All output works except the average   
   > price output.  The calculation is wrong and or in the wrong place.   
   >   
   > -btw average price must be output if the isbn is different or the   
   > same.  It must be output alongside the other output i mentioned.   
   >   
   > a) what is the correct formula to express average price here?   
   > b) where does it go in the program.   
   >   
   > please provide me with an answer rather than a 'hint' if you   
   > definately can see the solution as i have spent a lot of time on it   
   > already and have little to gain from further delays figuring it   
   > out. I simply need to complete the program to continue my study of   
   > classes in c++   
   >   
   > [snip]   
   >   
   >   if(std::cin >> total.isbn >> total.units_sold >> total.unit_price)   
   >   {   
   >      totalrevenue = total.units_sold * total.unit_price;   
   >      averageprice = totalrevenue / total.units_sold; // ok for first   
   >   
   >       sales_data trans; // object holds running sum   
   >   
   >       while(std::cin >> trans.isbn >> trans.units_sold >>   
   trans.unit_price)   
   >     {   
   >       // if isbn is same - if same book   
   >       if(total.isbn == trans.isbn)   
   >         {   
   >          // calculate total revenue   
   >           totalrevenue += (trans.units_sold * trans.unit_price);   
   >   
   >           // average sales price   
   >           averageprice += totalrevenue / trans.units_sold; // HELP   
   >           // number of copies sold   
   >           total.units_sold += trans.units_sold;   
   >         }   
   >       else{   
   >         // output previous isbn results   
   >         std::cout << "ISBN:" << total.isbn << " " << "UNITS SOLD: " <<   
   > total.units_sold << " " << "TOTAL SALES REVENUE:" << totalrevenue   
   >               << " " <<  "AVERAGE SALES PRICE:" << averageprice <<   
   > std::endl;   
   >         total = trans; // move to next book isbn by set total to   
   > current trans   
   >       }   
   >     }   
      
   If averageprice is defined as 'totalrevenue / total.units_sold', which   
   looks like a sensible definition, then this   
      
        /* update totalrevenue */   
        /* update total.units_sold */   
        averageprice = totalrevenue / total.units_sold;   
      
   is a better way to go than the strange sequence   
      
        /* update totalrevenue */   
        averageprice += totalrevenue / total.units_sold;   
        /* update total.units_sold */   
      
   that you use in your program.   
      
   Another problem appears in the 'else' branch of the 'if same book'   
   statement.   
      
   totalrevenue is the sum of all revenues of books that have the same   
   ISBN number as the book in 'total'. If you read a new ISBN number that   
   differs from the previous one, you correctly reset 'total', but you   
   should also reset totalrevenue to a sensible value, which is   
      
        total = trans;   
        totalrevenue = total.units_sold * total.unit_price;   
      
   And of course, after changing totalrevenue, averageprice needs to be   
   updated in the same 'else' branch:   
      
        averageprice = totalrevenue / total.units_sold;   
      
   Hope this helps.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca