Help with functions

Hi guys. I need to take the following code and create these functions for an assignment:

1. void ClearScreen (void) - to clear the screen

2. int LowScore (int Score) - receives the parameter score and
returns the smallest value it has received.
The function uses pass by value.

3. int HighScore (int& Score) - receives the a parameter score and
returns the largest value it has received.
The function uses pass by reference.

4. void Counter (int& Count) - increments a parameter counter by 1.
The function uses pass by reference.

5. float AverageScore (int Total, int Count)
- computes an average of total / count.
The function uses pass by value.
It returns an average.

6. void DisplayStatistics (int Low, int High, int Average, int Count)
- displays the following exam statistics:
a.) number of exams
b.) low score
c.) high score
d.) average score

and here is the code:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  // Constant 
  int const INPUT_END = -1;

  // Variable declarations
  double myInput;
  double myScoreTotal, myScoreHigh, myScoreLow, myScoreAverage;
  int myInputCount;

  // Initialize variables
  myScoreTotal = 0;
  myScoreHigh = 0;  // Set current high score
  myScoreLow = 100;  // Set current low score
  myInputCount = 0;

  // Display instructions
  cout << "Please enter either" << endl;
  cout << "1. an exam score, or " << endl;
  cout << "2. -1 to indicate that there are no additional scores." << endl;
  cout << endl;

  // Input loop
  do
  {
    cout << "Enter Exam Score: ";
    cin >> myInput;    

    if (myInput == INPUT_END) 
      break;

    if ((myInput > 100) || (myInput < 0))
    {
       cout << "Please enter a number between 0 and 100." << endl;
       continue;
    }

    // Increment totals
    myScoreTotal += myInput;

    // Increment total grades
    myInputCount++;

    // Check for high score
    if (myInput > myScoreHigh)
      myScoreHigh = myInput;

    // Check for low score
    if (myInput < myScoreLow)
      myScoreLow = myInput;

  }
  while (1);

   cout << "\033[2J\033[H";
   cout << "Exam Statistics" << endl;
   cout << endl;

   if (myInputCount == 0)
   {
     cout << "No scores were input!" << endl;
   }
   else
   {
     myScoreAverage = myScoreTotal / myInputCount;

     cout << "-----------------------------------------------" <<  endl;
     cout << "|   Number    |  High   |   Low   |  Average  |" <<   endl;
     cout << "|  of Exams   |  Score  |  Score  |   Score   |" << endl;
     cout << "-----------------------------------------------" <<   endl;
     cout << "|       "<< setw(3) << myInputCount;
     cout << "   |    " << setw(3) << myScoreHigh;
     cout << "  |    " << setw(3) << myScoreLow;
     cout << "  |  " << setw(6) << fixed << setprecision(2) << myScoreAverage << "   |" << endl;
     cout << "-----------------------------------------------" << endl;
  }

  return 0;
}


I basically just need to rewrite the code to use the functions. I really don't have any idea how to go about doing this. Can anyone help me out? I would greatly appreciate it. Thanks.
1. http://www.cplusplus.com/forum/articles/10515/

2. I do not understand?

3. I do not understand?

4. Completely and Utterly Pointless, but the most simple to do.

5. Also very easy to do.


Which one do you need help on first?
Hi, thanks for the quick reply!

My instructor basically wants me to take the following code and replace the calculations with functions. so for example:

1. I think I figured this one out. The header file would be:

 
void ClearScreen (void);


and then and the end of the code would be:

1
2
3
4
void ClearScreen (void)   
{
   cout << "\033[2J\033[H";
}


and then I just change the code from this:

 
cout << "\033[2J\033[H";


to this:

 
ClearScreen();


as for the other 5, I have no idea how to go about this. Thanks.
Last edited on
3 is pretty simple, you just have the parameter passed by reference and then do ++parameter inside the function. ;)
like this?

 
int HighScore (int& Score)


1
2
3
4
5
int HighScore (int& Score)
{
  if (myInput > myScoreHigh)
      myScoreHigh = myInput;
}


and replace

1
2
3
// Check for high score
    if (myInput > myScoreHigh)
      myScoreHigh = myInput;


with

 
HighScore();


Thanks for the help and sorry I'm such a noob lol
Oops. I meant number 4. I have no idea on 2 and 3.
Last edited on
Topic archived. No new replies allowed.