Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,493 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,355 people online right now. Registration is fast and FREE... Join Now!




Max Min problem in C++

 
Reply to this topicStart new topic

Max Min problem in C++, having a problem showing the Max and Min #

fwashington
23 Feb, 2007 - 12:04 PM
Post #1

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 3


My Contributions
CODE

#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

double *score;
double total = 0;
double average;
double max = 0;
double min = 0;
int TestScores;
int count = 0;


cout << "Enter the number of students test scores you want sorted ";
cin >> TestScores;
score = new double[TestScores]; // Allocate memory



// Get the Test Scores
cout << "Enter each students test scores.\n";
for (count = 0; count < TestScores; count++)

{

cout << "Student " << (count + 1) <<
": ";
cin >> score[count];

}



// Calculate the total Scores
for (count = 0; count < TestScores; count++)

{

total += score[count];

}

// Calculate the average Test Scores
average = total / TestScores;

if (count == 0)

{
    max = TestScores;
    min = TestScores;

}

if (TestScores > max)

{
    max = TestScores;

}

if (TestScores < min)

{
    min = TestScores;

}



// Display the results
cout << fixed << showpoint << setprecision(2);


cout << "Average Score: " << average <<endl;
cout << "The maximum value entered is " << max << endl;
cout << "The minimum of value entered is " << min << endl;




delete [] score;

return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Max Min Problem In C++
23 Feb, 2007 - 12:05 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Can you please describe the problem you are encountering?
User is online!Profile CardPM
+Quote Post

fwashington
RE: Max Min Problem In C++
23 Feb, 2007 - 12:16 PM
Post #3

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 3


My Contributions
QUOTE(Amadeus @ 23 Feb, 2007 - 01:05 PM) *

Can you please describe the problem you are encountering?



Well the program is doing part of what I want it to do...I ask the user to input the number of test it want an average of if you say 3 it display
Student 1 (input grade from user)
Student 2 (input grade from user)
Student 3 (input grade from user)
Then is says
Average grade(???)
Well I want it to also give me the max number and the min number
but it looks like this
Max number is 0.000
Min number is 0.000

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Max Min Problem In C++
23 Feb, 2007 - 12:24 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
I just ran the program with 3 scores, and values of 10, 12, 14. It correctly identified the average as 12,and identified the max as 3. this is because you are assigning the value of TestScores to the maximum variable. you need, in the loop that takes in the scores, to compare each incoming score to the greatest and smallest. If the incoming score is greater than the max, replace it with the incoming score. Same process (except smaller) with the min. Something like the following (please note this is for demonstration purposes only...it is not the most efficient way to accomplish the task, but does illustrate the logic):
CODE

for (count = 0; count < TestScores; count++)

{

total += score[count];
if(score[count]>max)
   max=score[count];
if(count==0)
   min=score[count];
if(score[count]<min)
   min=score[count];
  

}

// Calculate the average Test Scores
average = total / TestScores;


// Display the results
cout << fixed << showpoint << setprecision(2);


cout << "Average Score: " << average <<endl;
cout << "The maximum value entered is " << max << endl;
cout << "The minimum of value entered is " << min << endl;

User is online!Profile CardPM
+Quote Post

fwashington
RE: Max Min Problem In C++
23 Feb, 2007 - 12:39 PM
Post #5

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 3


My Contributions
QUOTE(Amadeus @ 23 Feb, 2007 - 01:24 PM) *

I just ran the program with 3 scores, and values of 10, 12, 14. It correctly identified the average as 12,and identified the max as 3. this is because you are assigning the value of TestScores to the maximum variable. you need, in the loop that takes in the scores, to compare each incoming score to the greatest and smallest. If the incoming score is greater than the max, replace it with the incoming score. Same process (except smaller) with the min. Something like the following (please note this is for demonstration purposes only...it is not the most efficient way to accomplish the task, but does illustrate the logic):
CODE

for (count = 0; count < TestScores; count++)

{

total += score[count];
if(score[count]>max)
   max=score[count];
if(count==0)
   min=score[count];
if(score[count]<min)
   min=score[count];
  

}

// Calculate the average Test Scores
average = total / TestScores;


// Display the results
cout << fixed << showpoint << setprecision(2);


cout << "Average Score: " << average <<endl;
cout << "The maximum value entered is " << max << endl;
cout << "The minimum of value entered is " << min << endl;




Ok thanks I see what I have to do and thanks for making it a little more clear than what it was

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month