a. Write a pseudo-code that prompt the user to input number of sold items and then receive the sold value for each item, calculate the average value, display the items numbers associated with its values and display also the calculated average value on the screen.

b. write down the C++ code for the previous program.

Well... I tried to solve it and here we go:

* Pseudo-code:
start
get sold items,sold value
average=sold value/sold items
Display sold items,sold value
Display average

* C++:

#include<iostream.h>
void main()
{
int NumOfSold,SolValue;
float average;
cout<<"enter the number of sold items\n";
cin>>NumOfSold;
cout<<"enter the sold value for each item\n";
cin>>SolValue;
average=(float)SolValue/NumOfSold;
cout<<NumOfSold<<":"<<SolValue<<endl;
cout<<"average:"<<average;
}

I'm 100% sure that the program above is incorrect and need some modifications..
plzzzzz...put me on the right direction. Thnx

## All 4 Replies

>receive the sold value for each item
The "for each" part suggests that you need a loop unless you can guarantee that the sold value is identical for all items:

``````start
get <sold items>
for number = 1 to <sold items>
get <sold value>
sum = sum + <sold value>
Display number, ": ", <sold value>
loop
average = sum / <sold items>
Display average``````

Well you actually forgot to put in some main code.

``````#include<iostream>
using namespace std;
int main()
{
int NumOfSold,SolValue;
float average;
cout<<"enter the number of sold items\n";
cin>>NumOfSold;
cout<<"enter the sold value for each item\n";
cin>>SolValue;
average=SolValue/NumOfSold;
cout<<NumOfSold<<":"<<SolValue<<endl;
cout<<"average:"<<average;
}``````

In your code. "Using namespace std;" was not written
And thats prettymuch it. However the "(float)" Cast also isnt nesesary . Hope you got the answer

>"Using namespace std;" was not written
Because it would have been an error. Notice that <iostream.h> (the pre-standard form, before namespaces) was used instead of the standard <iostream>.

>However the "(float)" Cast also isnt nesesary
Yes, it is. Both SolValue and NumOfSold are integers. If the result of division is less than 0, the precision will be lopped off and the result would be 0. Even if the result is greater than 0, all precision will be lost. The solution is to convert at least one of the operands to floating-point so that it becomes floating-point division rather than integer division.

>receive the sold value for each item
The "for each" part suggests that you need a loop unless you can guarantee that the sold value is identical for all items:

``````start
get <sold items>
for number = 1 to <sold items>
get <sold value>
sum = sum + <sold value>
Display number, ": ", <sold value>
loop
average = sum / <sold items>
Display average``````

Many thnx...you've been very helpful :) .... anyways, i reached to the c++ code with the help of psuedo-code:

#include<iostream.h>
void main()
{
int sold_items,sold_value;
int sum=0;
float average;
cout<<"enter the number of sold items\n";
cin>>sold_items;
for(int i=1;i<=sold_items;i++)
{
cout<<"enter the value of slod items\n";
cin>>sold_value;
sum+=sold_value;
cout<<i<<" "<<sold_value<<endl;
}
average=(float)sum/sold_items;
cout<<"average:"<<average;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.