Finding lowest number

Pages: 123
I have 5 scores, (and I do not know arrays well yet please no array answers) I just need help getting the formula correct and I think I can build the correct to find highest and then try to possibly rewrite the program using arrays but this is where i am. I have 5 ints score1, score2, score3, score4, score5. I am playing with the idea here is what i have at the moment. doesnt necessarily have to be a function just all the examples I see are for arrays and I dont get them yet(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double calcAvgScore(int score1, int score2, int score3, int score4, int score5)
{
    int minNum, maxNum = 0;
    if (score1 < minNum)
    {
        minNum = score1;
    }
    else if (score2 < minNum)
    {
        minNum = score2;
    }
    else if (score3 < minNum)
    {
        minNum = score3;
    }
    else if (score4 < minNum)
    {
        minNum = score4;
    }
    else if (score5 < minNum)
    {
        minNum = score5;
    }
double calcAvgScore(int score1, int score2, int score3, int score4, int score5)

The average is all the scores combined divided by the number of scores. In this case:

double average = (score1 + score2 + score3 + score4 + score5)/(double)5;

However in your code it looks like you are trying to search for the minimum and the maximum of the five scores (but the code is not right for this task).

Is this what you are asking?
Last edited on
yes, I have the average working I am working on finding lowest and highest but I dont understand arrays so an array anser will not do me much good)
I see this? But this isn't working ?
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

Example:
input -54 -76 -43 -87 -52
max == 0
min == -87

as you can see, the max should be -43 but it always comes out to zero... :(*/

int counter;
double num, max, min;
min = 0;
max = 0;
counter = 0;
while(counter < 5)
{
cin >> num;
if(num > max)
max = num;
if(num < min)
min = num;
counter ++;
}
cout << "Your max number is: " << max << endl
<< "Your min number is: " << min << endl;

system("pause");
return 0;
Line 12: You initialize max to 0, which is larger than any of your numbers (which are all negative). Initialize max to the smallest possible double.

See DBL_MIN in <float.h>
http://www.cplusplus.com/reference/cfloat/
Last edited on
no no that was from an example in the reference mine is above I can't get the formula right sorry
The problem is that you set max = 0 but no number is greater so it stays 0.
This should work.
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
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
#include <string.h>
#include <float.h>

using namespace std;

int main ()
{
  int counter;
  double num, max, min;
  min = 0;
  max = -DBL_MAX;
  counter = 0;
  while (counter < 5)
  {
    cin >> num;
    if (num > max)
      max = num;
    if (num < min)
      min = num;
    counter++;
  }
  cout << "Your max number is: " << max << endl
    << "Your min number is: " << min << endl;
  system ("pause");
  return 0;
}

1
2
3
4
5
6
7
-54
-76
-43
-87
-52
Your max number is: -43
Your min number is: -87
that is not my program sir that is an example
possibly I make score1 take the min position and then compare from there every sscore onward?
All into arrays now but still lost af(
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

double calcAvgScore(score[5]/5)
{
    int i, findLowest, findHighest; //i = counter
    double avg;


for (i = 0; i < 5; i++) {

//In first time
if (i == 0) {
findLowest = scores[i];
findHighest = scores[i];
}

//Get max
if (scores[i] > findHighest) {
findHighest = scores[i];
}

//Get max
if (scores[i] < findLowest) {
findLowest = scores[i];
}

//Sum
avg = avg + scores[i];
}


int main()
{
    string name;
    int score[5], avg = 0, quit, minNum = 0, maxNum = 0;
    while (quit != 1)
{
    quit = 0;
         cout << " Enter student name: ";
         cin >> name;

    for (int x=0; x<1; x++)
    {
    cout << " Enter score 1: ";
    cin >> score[]++;
            if (score1 < 1 || score1 > 10)
         {
             cout << " Re-enter score (between 1 - 10)" << endl;
         }


             cout << " Enter 1 to quit or 0 to enter another: ";
             cin >> quit;
}
cout << " Your avg score is: " << calcAvgScore(score[5]) << endl;
cout << " and min num is " <<  findLowest << endl;

}
   return 0;
}
Last edited on
> All into arrays now but still lost af(
I don't understand your problem.
I am trying to use findLowest and findHighest in a function to find highest and lowest scores but everything I try is failing?
I probably have alot of problems!
another update
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

using namespace std;

double calcAvgScore(int score[5])
{
    int i, findLowest, findHighest; //i = counter
    double avg;


for (i = 0; i < 5; i++) {

//In first time
if (i == 0) {
findLowest = score[i];
findHighest = score[i];
}

//Get max
if (score[i] > findHighest) {
findHighest = score[i];
}

//Get max
if (score[i] < findLowest) {
findLowest = score[i];
}

//Sum
avg = avg + score[i];
}

}
int main()
{
    string name;
    int score[5], avg = 0, quit, minNum = 0, maxNum = 0;
    while (quit != 1)
{
    quit = 0;
         cout << " Enter student name: ";
         cin >> name;

    for (int x=0; x<5; x++)
    {
    cout << " Enter score 1: ";
    cin >> score[x];
            if (score[x] < 1 || score[x] > 10)
         {
             cout << " Re-enter score (between 1 - 10)" << endl;
         }


             cout << " Enter 1 to quit or 0 to enter another: ";
             cin >> quit;
}
cout << " Your avg score is: " << calcAvgScore(score[5]) << endl;
cout << " and min num is " <<  findLowest << endl;

}
   return 0;
}
Ok, state your algorithm.
Assume you have N numbers.
+ What other variables do you need for the algorithm? What are their initial values prior to the algorithm (if necessary)
+ If you are writing a function, state the return value, extra parameters if needed
+ Does your function involve the uses of pointers & reference?
+ Try to imagine how many steps it would take to get the job done. Just show us all you've got. The more detailed the better.
+ Do you have to do all this in a single function? Or maybe two seperate functions.
+ If your algorithm fails, what the best action should you take to check the correctness of your algorithm?


Sorry if I sound indirect but you need to have a very good idea of what you have to do and understand your own plan throughly, or others' helps will be in vain.

P.S : It is not a very good idea to create a calAvgScore that does things it is not really supposed to do (findLowest, findHighest)
Last edited on
3. Use function calcAvgScore that has the contestant’s 5 scores as input parameters
a. returns the average score for that contestant.
b. Calls two functions, findLowest and findHighest which both accept the 5 scores as input parameters and return the lowest and highest scores, respectively.
4. Do not use global variables. All variables used in the functions must be passed as parameters or declared locally.
5. At the end of the program, display the winner and winning score (rounded to 2 decimal places).


Ok those are the exact destructions I have, to be honest I am completely overwhelmed with this one it has thrown in array along with multiple parameter functions both I have never done. I need score[5], counters but to be truthful I am lost I cannot make it run , well barely it isn't running correctly?
Let's start with findHighest function.

Let us know your whole plans on this function.

The more detailed, the better

P.S : I will not help you if you can't state clearly and correctly your plans, and your algorithm
ok I have t evaluate each score and see which is highest but what I am to compare to is unknown to me? I have used a format I found in an example trying to piece the program together?
Last edited on
Hint : It is like :
Function <name> : param1, param2...

Step1 : Declare variable1, variable2
Step2 : variable1 = something
Step3 : Check param1 if it is divisable by 2. If the condition is true, proceed to Step4, otherwise jump to Step5

Etc
right ?
1
2
3
4
5
double calcAvgScore(int score[5])
{
    int i, findLowest, findHighest; //i = counter
    double avg;
i had scores in separate ints but saw no way to get the lowest highest without array and then it has been downhill
Pages: 123