Star Search, Now With Arrays

Hello, I am requesting some assistance on a recent homework assignment in Beginning programming. I have been given the instructions below.

"Modify this program and its functions to use arrays instead of individual variables to contain the scores. This means that the functions that once received five scores will now receive as their parameters the name of the array and the size of that array. For example, one of the prototypes will be:
double getSmallest(double theArray [], int theSize);
The only place in your code where the number 5 will appear is when you declare the size of the arrays in the main. Change that lone 5 to, say, a 10, and your program should request and process 10 scores.
Also note that the getJudgeData() function and the accompanying validation function are reused exactly as they were in the previous assignment. Do not change these functions at all!"

I don't know how to use arrays, or how to insert numbers into them. Any assistance will be greatly appreciated. The below is the code from the original Star Search assignment.


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
//Star Search

#include <iostream>
using namespace std;

double getScore( double maxScore );
double lowest( double, double, double, double, double );
double highest( double, double, double, double, double );
double average( double, double, double, double, double );
bool isValid( double theScore, double maxValue );

int main()
{
    const double MAX_SCORE = 10;

    cout << "Please enter the 5 judge scores" << endl;
    const double s1 = getScore(MAX_SCORE);
    const double s2 = getScore(MAX_SCORE);
    const double s3 = getScore(MAX_SCORE);
    const double s4 = getScore(MAX_SCORE);
    const double s5 = getScore(MAX_SCORE);

    cout << "average of the four higher scores: "
         << average( s1, s2, s3, s4, s5 ) << endl;
}

double getScore( double maxScore )
{
    double score ;
    cout << "Enter the judge scores: ";
    cin >> score ;

    if( isValid( score, maxScore ) ) return score ;

    cout << "Invalid score, please enter a new one.";
    return getScore(maxScore) ;
}

double lower_of( double a, double b ) { return a<b ? a : b ; }

double lowest( double s1, double s2, double s3, double s4, double s5 )
{ return lower_of( s1, lower_of( s2, lower_of( s3, lower_of(s4,s5) ) ) ) ; }


double higher_of( double a, double b ) { return a>b ? a : b ; }

double highest( double s1, double s2, double s3, double s4, double s5 )
{ return higher_of( s1, higher_of( s2, higher_of( s3, higher_of(s4,s5) ) ) ) ; }


bool isValid( double theScore, double maxValue )
{ return theScore >= 0 && theScore <= maxValue ; }

double average( double s1, double s2, double s3, double s4, double s5 )
{
    const double lowest_score = lowest(s1,s2,s3,s4,s5);
    const double highest_score = lowest(s1,s2,s3,s4,s5);
    return ( s1+s2+s3+s4+s5 - lowest_score - highest_score) / 3 ;
}
Last edited on
I don't know how to use arrays, or how to insert numbers into them.

Tell that exactly in this wording to the person who assigned the a. m. homework to you. It is important as she/he should know, that her/his lessons was not appropriate for the target audience, at least not for the complete audience. Lean back, relax, if you did not grasp it, either it was not meant for you, or teacher(s) failed, or your parents failed, or both failed -- first and foremost, it ain't your fault.

But you also could twit them all, bypass them and use other sources for information. Like this one: http://www.cplusplus.com/doc/tutorial/arrays
and find the answers on your own. Out of luck? Failed? Look for more sources, a schoolfellow, schoolfellows or -- if all else failes -- the teacher.

Edit: a teacher's advantage? -- The teacher never fails.
AFAIK, no known teacher was/is/will be problematic ;)
Perhaps the last statement needs some small print -- https://www.youtube.com/watch?v=uR487qnNKCk
Last edited on
@NPhillips
No one failed you. You need to step up your game. Learning to program is hard, requires attention in both class and reading/programming on your own time. All of use here have done it. You can too.

The difference between arrays and lists of variables is pretty straight-forward:

1
2
3
4

const double s1 = …;
const double s2 = …;
…
1
2
3
4
double scores[5]
scores[0] = …;
scores[1] = …;
…

Typically, once you have arrays, you can deal with multiple objects using loops.

1
2
3
double scores[5];
for (int n = 0; n < 5; n++)
  scores[n] = …;

This same characteristic holds true when doing things like calculating the average, or finding the minimum/maximum:

1
2
3
double lowest( double xs[], int n );
double highest( double xs[], int n );
double average( double xs[], int n );

As a freebie, here is how you can determine the lowest:

1
2
3
4
5
6
7
8
9
double lowest( double xs[], int n )
{
  double result = xs[0];
  for (int i = 1; i < n; i++)
  {
    result = lower_of( result, xs[i] );
  }
  return result;
}

(You haven’t gotten into recursion yet, but you could be awesome and do this instead:
1
2
3
4
5
double lowest( double xs[], int n )
{
  if (n == 1) return xs[0];
  return lower_of( xs[n-1], lowest( xs, n-1 ) );
}
But that is something to play with later. :O)


BTW, is that a reference to Neelix?
I don't know how to use arrays, or how to insert numbers into them.

No better time to learn how, then.

https://www.learncpp.com/cpp-tutorial/61-arrays-part-i/
Topic archived. No new replies allowed.