finding maximum,second maximum and minimum value

I am new to c++ and have been given a task to code the program written below. The use of arrays and functions is not allowed I've to do it only using if-else,while and for loop.
library has hired you to develop a simple program for book inventory. You job is to write a C++ program that input prices of books purchased in year 2017 and compute following statistics:
1. Total number of books purchased in 2017. 2. Maximum price that library has paid for any book. Also find the number of books that has the maximum price. 3. Second Maximum Price. 4. Minimum price that library has paid for any book. Also find the number of books that has the minimum price.
So far I'm stuck here


#include <iostream>
using namespace std;
int main ()
{
int sum=0,books=0,i=1,max=0,maximum=0,max2=0,n=0;
while(books!=-1)
{
cout<<"enter price of books"<<endl;
cin>>books;
sum=(sum+i);
if(books>max)
{
max=books;

if(max2<max)

max2=books;}
}
cout<<"second maximum="<<max2-1<<endl;
cout<<"maximum price="<<max<<endl;
cout<<"total number of books="<<sum-1<<endl;
return 0;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

int main()
{
    // for now, we will make the simplifying assumption that floating point comparisons
    // of the prices of books yield exact results.

    int nbooks = 0 ; // total number of books purchased

    double max_price = 0 ; // maximum price that library has paid for any book.
    int nbooks_max_price = 0 ; // number of books with maximum price.

    double second_max_price = 0 ; // second Maximum Price.

    double min_price = 10000000000000000.0 ; // minimum price that library has paid for any book.
                                             // we start with an impossibly high value
    int nbooks_min_price = 0 ; // number of books with minimum price.

    std::cout << "enter price of books. enter a non-positive value to end input\n" ;
    double price ;
    while( std::cout << "? " && std::cin >> price && price > 0 )
    {
        ++nbooks ;

        if( price == max_price ) ++nbooks_max_price ;
        else if( price > max_price )
        {
            second_max_price = max_price ;
            max_price = price ;
            nbooks_max_price = 1 ;
        }
        else if( price > second_max_price ) second_max_price = price ;

        if( price == min_price ) ++nbooks_min_price ;
        else if( price < min_price )
        {
            min_price = price ;
            nbooks_min_price = 1 ;
        }
    }

    if( nbooks > 0 ) { /* To Do: print results */ }
}
Last edited on
I've just taken a few classes so everything just got over my head. We've not yet written a program without using "using namespace std' . Furthermore, I did try this but there's no output. If you don't mind can you just simplify it a bit more.
> I did try this but there's no output.

See line 42: if( nbooks > 0 ) { /* To Do: print results */ }
You need to add the code to print out the values.


> We've not yet written a program without using "using namespace std'

You may add using namespace std; at the top;
and optionally remove the qualification std:: ie. change std::cout to just plain cout etc.
It wouldn't change anything about this program.

> can you just simplify it a bit more.

Difficult to simplify it any more than this; particularly with this restriction in place:
"The use of arrays and functions is not allowed I've to do it only using if-else,while and for loop."

Try writing this simpler program first:
You job is to write a C++ program that inputs prices of books purchased in year 2017 and prints out the maximum price that library has paid for any book.

Once you have got that working correctly, now write:
You job is to write a C++ program that inputs prices of books purchased in year 2017 and prints out
a. the maximum price that library has paid for any book.
b. the number of books that has the maximum price.

Once you have got that working correctly, add the next small feature. And so on, and so forth;
take many small baby steps, one after the other.
Last edited on
Topic archived. No new replies allowed.