dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help with getting Lowest Number

  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Help with getting Lowest Number

    Hello. I am new to using c++ and I learn pretty fast. I am ahead of my class but this kind of stumps me. ^_^ I cannot get the smallest number to display the smallest number that the user entered. All it displays is 0. Is there any way to make it display the lowest number. Thanks in advanced.

    (Oh and I don't want to use an array. ^_^)

    Code:
    #include<iostream.h>
    
    main()
    {
    
    int num = 0;
    int x = 0;
    int sum = 0;
    int largest = -65535;
    int smallest = 65535;
    float average;
    int difference;
    
    do
    {
    	cout<<"Please enter an interger. (Enter 0 to exit):";
    	cin>>num;
    
    	sum = sum + num;
    	x++;
    
    	if(num > largest)
    	{
    		largest = num;
    	}
    
    	else
    		if(num < smallest)
    		{
    			smallest = num;
    		}
    
    }
    while(num != 0);
    
    
    x = (x-1);
    
    cout<<"You entered "<<x<<" numbers."<<endl;
    
    average = sum / float(x);
    
    cout<<"The average of the numbers you entered is "<<average<<endl;
    
    cout<<"The largest number in the series is "<<largest<<endl;
    cout<<"The smallest number in the series is "<<smallest<<endl;
    
    difference = largest - smallest;
    cout<<"The difference between the largest number and the smallest number is "<<difference<<endl;
    
    return 0;
    }

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Help with getting Lowest Number

    Well, since the user needs to press '0' to exit the loop, then 0 will be the smallest number (unless he/she somewhere along typed a negative number).

    Maybe you should add a check to avoid smallest being updated then the user choose '0'.

    - petter

  3. #3
    Join Date
    Jan 2008
    Posts
    2

    Re: Help with getting Lowest Number

    Thanks. I cannot believe I didn't notice that. ^_^

    I got it now. For now I will leave it like this and when I am done I will see if I could do it any other way. Thanks again.

    Code:
    #include<iostream.h>
    
    main()
    {
    
    int num = 0;
    int x = 0;
    int sum = 0;
    int largest = -65535;
    int smallest = 65535;
    float average;
    int difference;
    
    do
    {
    	cout<<"Please enter an interger. (Enter 0 to exit):";
    	cin>>num;
    
    	sum = sum + num;
    	x++;
    
    	if(num > largest)
    	{
    		largest = num;
    	}
    
    	if(num != 0)
    	{	
    		if(num < smallest)
    		{
    			smallest = num;
    		}
    	}
    }
    while(num != 0);
    
    
    x = (x-1);
    
    cout<<"You entered "<<x<<" numbers."<<endl;
    
    average = sum / float(x);
    
    cout<<"The average of the numbers you entered is "<<average<<endl;
    
    cout<<"The largest number in the series is "<<largest<<endl;
    cout<<"The smallest number in the series is "<<smallest<<endl;
    
    difference = largest - smallest;
    cout<<"The difference between the largest number and the smallest number is "<<difference<<endl;
    
    return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)