AncientAnubis
AncientAnubis

Reputation: 61

Array function doesn't seem to be finding the highest value

I have a piece of code which is supposed to return the highest integer from a set of arrays each containing three integers. Currently my code isn't working. Can someone help me find my bug?

This is my code below:

#include <iostream>
using namespace std;

void HighestScore(int[], int[], int[]);

int main() {
    const int SIZE = 3;
    int Stu1[SIZE] = {70, 80, 90},
        Stu2[SIZE] = {71, 81, 91},
        Stu3[SIZE] = {72, 82, 92},
        Stu4[SIZE] = {73, 83, 93},
        Stu5[SIZE] = {74, 84, 94};


    HighestScore(Stu1,Stu2,Stu3);


    return 0;
}



void HighestScore(int St1[], int St2[], int St3[])
{
    const int SIZE =3;
    int count;

    int high1 = St1[0];
    int high2 = St2[0];
    int high3 = St3[0];
    int highest =0;

    for (count=1;count<SIZE;count++)
    {
        if(St1[count] > high1)
            {high1 = St1[count];}
        if(St2[count] >high2)
            {high2 = St2[count];}
        if(St3[count] >high3)
            {high3 = St3[count];}

    }

    if(high1>high2)
        {highest=high1;}
    else if (high1>high3)
        {highest=high1;}
    else if (high2>high1)
        {highest=high2;}
    else if (high2>high3)
        {highest=high2;}
    else if (high3>high1)
        {highest=high3;}
    else if (high3>high2)
        {highest=high3;}
    else
        {highest=-1;}

    cout << highest;
    return;
}

Upvotes: 2

Views: 99

Answers (5)

Archmede
Archmede

Reputation: 1856

I wrote a function which returns the highest number in an array. All you have to do is run that function on all of your arrays, save the highest from each array and run the function again.

#include <iostream>
using namespace std;

int HighestScore(int Stu[], int length);

int main() {
    const int SIZE = 3;
    int Stu1[SIZE] = {70, 80, 90};
    int Stu2[SIZE] = {71, 81, 91};
    int Stu3[SIZE] = {72, 82, 92};

    int highestArr[3];
    int highest;

    highestArr[0] = HighestScore(Stu1, SIZE);
    highestArr[1] = HighestScore(Stu2, SIZE);
    highestArr[2] = HighestScore(Stu3, SIZE);

    highest = HighestScore(highestArr, 3);

    cout <<  highest << std::endl;

    return 0;
}

int HighestScore(int Stu[], int length) {
    int highest = 0; // Init to 0 since mark can't be lower than 0
    for(int i = 0; i < length; i++) {
        if(highest < Stu[i]) {
            highest = Stu[i];   
        }
    }

    return highest;
}

Upvotes: 0

Jarod42
Jarod42

Reputation: 218278

With std, you may simply do:

const auto highest = std::max({
        *std::max_element(std::begin(Stu1), std::end(Stu1)),
        *std::max_element(std::begin(Stu2), std::end(Stu2)),
        *std::max_element(std::begin(Stu3), std::end(Stu3)),
    });
std::cout << highest << std::endl;

Demo

Upvotes: 0

dev8080
dev8080

Reputation: 4030

Since the array sizes are the same, you need not process each highest separately.

Let highest store the highest at the moment and get overwritten by the next highest.

Why not just:

 int HighestScore(int St1[], int St2[], int St3[])
{
const int SIZE =3;
int count;
//make sure you have #include <limits.h> in the beginning
int highest = INT_MIN; //---lowest value of an integer in C;


for (count=0;count<SIZE;count++)
{
    if(St1[count] > highest)
        highest = St1[count];
    if(St2[count] > highest) 
        highest = St2[count];
    if(St3[count] > highest) 
        highest = St3[count];
}
cout << highest;
return highest;
}

For the initial value of highest, take the lowest possible value of an integer, so that an overwrite is guaranteed.

Lowest possible value of an integer is also avaiable as INT_MIN in limits.h.

Upvotes: 2

Alican OZER
Alican OZER

Reputation: 1

Do the following;

Replace all "else if" statement with "if" statement, and remove the last "else" statement.

or,

replace your function with this,

void HighestScore(int St1[], int St2[], int St3[])
{
const int SIZE =3;
int count;

int high1 = St1[0];
int high2 = St2[0];
int high3 = St3[0];
int highest;

for (count=1;count<SIZE;count++)
{
    if(St1[count] > high1)
        {high1 = St1[count];}
    if(St2[count] >high2)
        {high2 = St2[count];}
    if(St3[count] >high3)
        {high3 = St3[count];}
}

highest=high1;
if(high2>highest)
    {highest=high2;}
if (high3>highest)
    {highest=high3;}


cout << highest;
return;

}

Upvotes: 0

Petar Petrovic
Petar Petrovic

Reputation: 414

At least one problem is in this big if part

if(high1>high2)
    {highest=high1;}
else if (high1>high3)
    {highest=high1;}
else if (high2>high1)
    {highest=high2;}
else if (high2>high3)
    {highest=high2;}
else if (high3>high1)
    {highest=high3;}
else if (high3>high2)
    {highest=high3;}
else
    {highest=-1;}

Suppose high1 = 3, high2 = 2, high3 = 10, the if part chooses high1 as highest value because of the following branch although the highest should be high3

if(high1>high2)
    {highest=high1;}

A better approach would be making a helper function find_max that return the max of an array, then using it to find the max of 3 arrays The pseudo code should be

int find_max(int a[]);

int HighestScore(int st1[], int st2[], int st3[]){
    int tmp[] = {find_max(st1), find_max(st2), find_max(st3)};
    return find_max(tmp);
}

And I would suggest using vector instead of array.

Upvotes: 1

Related Questions