Min/Max Numbers

This is a discussion on Min/Max Numbers within the C++ Programming forums, part of the General Programming Boards category; Hi all, I just started a C++ class and have figured out that I can't quit my day job! I ...

  1. #1
    mzt
    mzt is offline
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Question Min/Max Numbers

    Hi all,

    I just started a C++ class and have figured out that I can't quit my day job! I have a simple problem - figure out the average, minimum, and maximum numbers for a list of numbers that I enter with -1 to end the listing.

    I have been struggling for 3 days on how to get the min & max numbers. I have tried a few diferent things, but I just can't think it out in my head in a logical code way!

    Now I can't even get the average to work - it WAS working before I started in again trying to do the min & max.

    Any assistance would be greatly appreciated. I don't want anyone to do it for me, but an explanation of how I can accomplish this would be great! The code I have altered at least 50 times is below.

    thanks mz

    Code:
    //"This program calculates the average score, minimum score and maximum score."
    
    #include <iostream.h>
    void main()
    
    
    {	float tscore=0.0, sumscore = 0.0, count = 0.0, average;
    //	int minscore, maxscore;
    
    	cout <<"Enter a list of positive values (ending with  -1):";
    	cin >> tscore; 
    
    //		for (maxscore=minscore=tscore; tscore > -1;); {
    //			if (tscore < minscore) 
    //				minscore = tscore;
    //				else if (tscore > maxscore) 
    //					maxscore = tscore;  }
    		while (tscore > 0.0)
    		sumscore = sumscore + tscore;
    		count = count + 1;
    		cin >> tscore;
    
    
    	average = sumscore / count;
    	cout << "The average is :"  <<average << endl;
    //	cout << "The minimum score is :"  <<minscore << endl;
    //	cout << "The maximum score is :"  <<maxscore << endl;
    
    }

    PS It is always good to try new things, if for nothing else, to see how some things just aren't for you.


  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    Re: Min/Max Numbers

    Originally posted by mzt
    Code:
    		for (maxscore=minscore=tscore; tscore > -1;); {
    //			if (tscore < minscore) 
    //				minscore = tscore;
    //				else if (tscore > maxscore) 
    //

    sorry but i didnt understand this code

    why didnt you use while instead of for??

    trythe following code

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
       int count=0;
       double score,sum,av,max,min;  sum=0.0;
       cout<<"enter student's score : "<<endl;
       cin>>score;
       max=min=score;
       while(score>0)
         {  
             if(score>max)
                max=score;
             if(score<min)
                min=score;
             sum+=score;
             count++;
             cout<<"enter student score :";
             cin>>score;
        }
           
      av=sum/count;
      cout<<"average :"<<av <<endl;
      cout<<"maximum : "<<max<<endl;
       cout<<"minimum : " <<min<<endl;
    }   
          
     return 0;
    }

    i didnt run it on my compiler but i hope it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 07:28 AM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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