Changing to arrays

Jul 22, 2018 at 11:27pm
// I need to change this to arrays but can not figure it out


#include <iostream>
using namespace std;


// Function Prototypes
void getJudgeData(double &);
double calcScore(double, double, double, double, double);
int findHighest(double, double, double, double, double);
int findLowest(double, double, double, double, double);


int main()
{
double Score1, Score2, Score3, Score4, Score5;


// Display introduction
cout << "\nThis program calculates a performer’s final score.\n"
<< "---------------------------------------------------\n\n";


// Call function getJudgeData once for each judge
getJudgeData(Score1);
getJudgeData(Score2);
getJudgeData(Score3);
getJudgeData(Score4);
getJudgeData(Score5);
cout << "\nThe contestant’s score is ";


// Call function calcScore passing to it the five scores
cout << calcScore(Score1, Score2, Score3, Score4, Score5);


cout << endl;


return 0;
}


void getJudgeData(double &Score)
{
do
{
cout << "Enter a judge’s score: ";
cin >> Score;


if (Score < 0 || Score > 10)
{
cout << "\nError! Invalid score.\n"
<< "Judge's score must be greater than 0 and less than 10.\n";
}


} while (Score < 0 || Score > 10);
}


double calcScore(double Score1, double Score2, double Score3, double Score4,
double Score5)
{
int High,
Low;
double Avg;


// Call function findHighest and findLowest passing 5 scores to them
High = findHighest(Score1, Score2, Score3, Score4, Score5);
Low = findLowest(Score1, Score2, Score3, Score4, Score5);


if (High == static_cast<int>(Score1))
{
if (Low == static_cast<int>(Score2))
Avg = (Score3 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score3))
Avg = (Score2 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score3 + Score2 + Score5)/3;
else
Avg = (Score2 + Score3 + Score4)/3;
}
else if (High == static_cast<int>(Score2))
{
if (Low == static_cast<int>(Score1))
Avg = (Score3 + Score4 + Score5)/3; else if (Low == static_cast<int>(Score3))
Avg = (Score1 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score3 + Score1 + Score5)/3;
else
Avg = (Score1 + Score3 + Score4)/3;
}
else if (High == static_cast<int>(Score3))
{
if (Low == static_cast<int>(Score2))
Avg = (Score1 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score1))
Avg = (Score2 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score1 + Score2 + Score5)/3;
else
Avg = (Score2 + Score1 + Score4)/3;
}
else if (High == static_cast<int>(Score4))
{
if (Low == static_cast<int>(Score2))
Avg = (Score3 + Score1 + Score5)/3;
else if (Low == static_cast<int>(Score3))
Avg = (Score2 + Score1 + Score5)/3;
else if (Low == static_cast<int>(Score1))
Avg = (Score3 + Score2 + Score5)/3;
else
Avg = (Score2 + Score3 + Score1)/3;
}
else
{
if (Low == static_cast<int>(Score2))
Avg = (Score3 + Score4 + Score1)/3;
else if (Low == static_cast<int>(Score3))
Avg = (Score2 + Score4 + Score1)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score3 + Score2 + Score1)/3;
else
Avg = (Score2 + Score3 + Score4)/3;
}


return Avg;
}


int findHighest(double Score1, double Score2, double Score3, double Score4, double Score5)
{
if (Score1 > Score2 && Score1 > Score3 && Score1 > Score4 && Score1 > Score5)
return Score1;
else if (Score2 > Score1 && Score2 > Score3 && Score2 > Score4 &&
Score2 > Score5)
return Score2;
else if (Score3 > Score2 && Score3 > Score1 && Score3 > Score4 &&
Score3 > Score5)
return Score3;
else if (Score4 > Score2 && Score4 > Score3 && Score4 > Score1 &&
Score4 > Score5)
return Score4;
else
return Score5;
}


int findLowest(double Score1, double Score2, double Score3, double Score4,
double Score5)
{
if (Score1 < Score2 && Score1 < Score3 && Score1 < Score4 && Score1 < Score5)
return Score1;
else if (Score2 < Score1 && Score2 < Score3 && Score2 < Score4 &&
Score2 < Score5)
return Score2;
else if (Score3 < Score2 && Score3 < Score1 && Score3 < Score4 &&
Score3 < Score5)
return Score3;
else if (Score4 < Score2 && Score4 < Score3 && Score4 < Score1 &&
Score4 < Score5)
return Score4;
else
return Score5;
}
Jul 23, 2018 at 1:13am
double Score1, Score2, Score3, Score4, Score5;

replace with:

double Score[5];

and access the individual items like this:
Score[0] = 1;
Score[1] = 2; //note the first one is in the zeroth index


Jul 23, 2018 at 1:16am
^ and the functions would look like this:
int foo(double Score1, double Score2, double Score3, double Score4, double Score5)
becomes
int foo(double Scores[])

call like:
foo(Scores);

If you want to save lines of code, you can use a for loop to iterate
1
2
3
4
for (int i = 0; i < 5; i++)
{
    Scores[i] = /* ... */;
}
Last edited on Jul 23, 2018 at 1:17am
Jul 23, 2018 at 4:05am
// this is what ive changed so far. what else needs to be changed


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



void getJudgeData(double &);
double calcScore[5];
int findHighest[5];
int findLowest[5];


int main()
{
double score[5];



cout << "\nThis program calculates a performer’s final score.\n"
<< "---------------------------------------------------\n\n";



getJudgeData(Score1);
getJudgeData(Score2);
getJudgeData(Score3);
getJudgeData(Score4);
getJudgeData(Score5);
cout << "\nThe contestant’s score is ";


cout << calcScore(Score[5];


cout << endl;


return 0;
}


void getJudgeData(double &Score)
{
do
{
cout << "Enter a judge’s score: ";
cin >> Score;


if (Score < 0 || Score > 10)
{
cout << "\nError! Invalid score.\n"
<< "Judge's score must be greater than 0 and less than 10.\n";
}


} while (Score < 0 || Score > 10);
}


double calcScore(double[5])
{
int High,
Low;
double Avg;



High = findHighest(Score1, Score2, Score3, Score4, Score5);
Low = findLowest(Score1, Score2, Score3, Score4, Score5);


if (High == static_cast<int>(Score1))
{
if (Low == static_cast<int>(Score2))
Avg = (Score3 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score3))
Avg = (Score2 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score3 + Score2 + Score5)/3;
else
Avg = (Score2 + Score3 + Score4)/3;
}
else if (High == static_cast<int>(Score2))
{
if (Low == static_cast<int>(Score1))
Avg = (Score3 + Score4 + Score5)/3; else if (Low == static_cast<int>(Score3))
Avg = (Score1 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score3 + Score1 + Score5)/3;
else
Avg = (Score1 + Score3 + Score4)/3;
}
else if (High == static_cast<int>(Score3))
{
if (Low == static_cast<int>(Score2))
Avg = (Score1 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score1))
Avg = (Score2 + Score4 + Score5)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score1 + Score2 + Score5)/3;
else
Avg = (Score2 + Score1 + Score4)/3;
}
else if (High == static_cast<int>(Score4))
{
if (Low == static_cast<int>(Score2))
Avg = (Score3 + Score1 + Score5)/3;
else if (Low == static_cast<int>(Score3))
Avg = (Score2 + Score1 + Score5)/3;
else if (Low == static_cast<int>(Score1))
Avg = (Score3 + Score2 + Score5)/3;
else
Avg = (Score2 + Score3 + Score1)/3;
}
else
{
if (Low == static_cast<int>(Score2))
Avg = (Score3 + Score4 + Score1)/3;
else if (Low == static_cast<int>(Score3))
Avg = (Score2 + Score4 + Score1)/3;
else if (Low == static_cast<int>(Score4))
Avg = (Score3 + Score2 + Score1)/3;
else
Avg = (Score2 + Score3 + Score4)/3;
}


return Avg;
}


int findHighest(double Score1, double Score2, double Score3, double Score4, double Score5)
{
if (Score1 > Score2 && Score1 > Score3 && Score1 > Score4 && Score1 > Score5)
return Score1;
else if (Score2 > Score1 && Score2 > Score3 && Score2 > Score4 &&
Score2 > Score5)
return Score2;
else if (Score3 > Score2 && Score3 > Score1 && Score3 > Score4 &&
Score3 > Score5)
return Score3;
else if (Score4 > Score2 && Score4 > Score3 && Score4 > Score1 &&
Score4 > Score5)
return Score4;
else
return Score5;
}


int findLowest(double Score1, double Score2, double Score3, double Score4,
double Score5)
{
if (Score1 < Score2 && Score1 < Score3 && Score1 < Score4 && Score1 < Score5)
return Score1;
else if (Score2 < Score1 && Score2 < Score3 && Score2 < Score4 &&
Score2 < Score5)
return Score2;
else if (Score3 < Score2 && Score3 < Score1 && Score3 < Score4 &&
Score3 < Score5)
return Score3;
else if (Score4 < Score2 && Score4 < Score3 && Score4 < Score1 &&
Score4 < Score5)
return Score4;
else
return Score5;
}
Jul 23, 2018 at 11:11am
You need to reread jonnin's and Ganado's posts. They've already told you what else needs to be changed.

Jul 23, 2018 at 1:06pm
all the stuff you replaced with arrays needs to change.

for example (pick any random line really)
else if (Score4 < Score2 && Score4 < Score3 && Score4 < Score1 &&

would be
else if (Score[3] < Score[1] && Score[3] < Score[2] && Score[3] < Score[0] &&

it may be in your best interest due to your existing code to allocate an extra slot in Score and keep the old names consistent. if you did that you could just put [] around your variable name number part (eg score3 becomes score[3] instead of score[2]) and make it much easier to edit the code. I recommend, whichever way you do it, to carefully do search and replaces... eg replace all Score[3] with Score[2] (or 3 if you add the extra space) and let the text editor do this work for you.

the same will be true for any other variables you changed.
then you can try to compile it and chase down the errors and fix them, which should just be your function calls.

and, for more than a couple lines of code, use code tags (<> on the side bar) for code so we can read it easier. Im used to hacking in a plain text editor, most folks here are used to syntax highlighting and indents and stuff.
Last edited on Jul 23, 2018 at 1:07pm
Jul 23, 2018 at 1:20pm
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
#include <iostream>
#include <algorithm>
#include <iterator>

constexpr std::size_t NUM_SCORES = 5 ;
// https://en.cppreference.com/w/cpp/language/type_alias
using score_array = double[NUM_SCORES] ; // alias for the type 'array of NUM_SCORES double'

double get_score()
{
    constexpr double MIN_SCORE = 1.0 ;
    constexpr double MAX_SCORE = 10.0 ;

    std::cout << "enter judge's score [" << MIN_SCORE << " to " << MAX_SCORE << "]: " ;

    double score ;
    if( std::cin >> score )
    {
        if( score >= MIN_SCORE && score <= MAX_SCORE ) return score ; // valid score, return it
        else std::cout << "invalid score. value is out of range\n" ;
    }
    else // non numeric input
    {
        std::cout << "please enter a number\n" ;

        // https://en.cppreference.com/w/cpp/io/basic_ios/clear
        std::cin.clear() ; // clear the failed state

        // https://en.cppreference.com/w/cpp/io/basic_istream/ignore
        std::cin.ignore( 1000, '\n' ) ; // throw the bad input away.
    }

    return get_score() ;
}

void get_all_scores( score_array& scores ) // note: array is passed by reference
{
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( double& s : scores ) s = get_score() ;
}

double average_score( score_array& scores )
{
    // sort the array in ascending order
    // https://en.cppreference.com/w/cpp/iterator/begin
    // https://en.cppreference.com/w/cpp/iterator/end
    // https://en.cppreference.com/w/cpp/algorithm/sort
    std::sort( std::begin(scores), std::end(scores) ) ;

    // return the average of the three middle scores
    // https://en.cppreference.com/w/cpp/language/static_assert
    static_assert( NUM_SCORES == 5, "the next line requires modification" ) ;
    return ( scores[1] + scores[2] + scores[3] ) / 3.0 ;
}

int main()
{
    score_array scores ;
    get_all_scores(scores) ;

    std::cout << "scores: " ;
    for( double s : scores ) std::cout << s << ' ' ;
    std::cout << '\n' ;

    std::cout << "average score: " << average_score(scores) << '\n' ;
}
Topic archived. No new replies allowed.