Home > Mobile >  Your C program must use functions to input the scores, compute the average, and find the index of
Your C program must use functions to input the scores, compute the average, and find the index of

Time:04-15

I've been working on this for hours now and I'm almost done. I can't get the program to display the correct student ID.

Also the "highIndex" function is suppose to be an "int" but I started with a double. When I try to change it, everything else seems to fall apart.

How do I get the high score to be associated with the student ID so I can output it correctly? I also need to highIndex to be an int which means some other things needs to be changed.

Any help would be much appreciated :)

Here's what I have so far

void inputAnswers(int studentIds[], double scores[]);

double getAverage(double scores[])
{
    double total = 0;
    for (int count = 0; count <= 6; count  )
    {
        total  = scores[count];
    }
    return total / 7; 
}
 
double highIndex(double score[])
{
    double highScore = score[0];
    double indexHigh = 1;

    for (int count = 0; count <= 6; count  )
    {
        if (score[count] > highScore)
        {
            highScore = score[count];
            indexHigh = count;
        }
    }
    return highScore;
}
 
int main()
{   
    const int ids = 7;
    int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
    double scores[7];
    double highScore[7];

    // Gets the test score from user
    inputAnswers(student, scores);

    // Calculates the average
    cout << "The average score is " << getAverage(scores) << endl;

    // Calculates highest score
    cout << "The high score was student " << highIndex(highScore) << " with a score of " << highIndex(scores);

    return 0;
}

// Function gets student scores
void inputAnswers(int student[], double scores[])
{
    for (int count = 0; count <= 6; count  )
    {
        cout << "Enter the score for student "<< student[count] << ": ";
        cin >> scores[count];
    }
}

CodePudding user response:

The problem around the highindex function can be solved by casting,

double highIndex(double score[])

{
    int highScore = (int)score[0];
    int indexHigh = 1;

for (int count = 0; count <= 6; count  )
{
    if ((int)score[count] > highScore)
    {
        highScore = (int)score[count];
        indexHigh = count;
    }
}
return highScore;
}

To associate students id with marks, you will need to create a class. Class are the main advantage of object oriented programming like c .

class Student{
   double scores[7];
   double highScore[7];
//past all your functions inside the class
}

Then in the main function create a object array instead of int stuents

int main()

{

const int ids = 7;
Student student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 `};`

then scores can be accessed as stuent[i].scores[i]. Format your code according to this, you can acheive your result

CodePudding user response:

As per my observation you haven't supplied any values to the Highscore array and it is not required as well. If all you need is to find average score, highscore and id of student with highscore this slight change will do the trick just adjusted 3 values from your code and is documented at corresponding lines.

#include<iostream>
using namespace std;
double getAverage(double scores[])
{
    double total = 0;
    for (int count = 0; count <= 6; count  )
    {
        total  = scores[count];
    }
    return total / 7;

}
void inputAnswers(int student[], double scores[])
{
    for (int count = 0; count <= 6; count  )
    {
        cout << "Enter the score for student "<< student[count] << ": ";
        cin >> scores[count];
    }
}

int highIndex(double score[])
{
    double highScore = score[0];
    double indexHigh = 0; //as we are assigning from position 0

    for (int count = 1; count <= 6; count  )
    {
        if (score[count] > highScore)
        {
            highScore = score[count];
            indexHigh = count;
        }
    }
   return indexHigh; //returns index of highscore
 }

int main()

{

    const int ids = 7;
    int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
    double scores[7];
    //double highScore[7]; no need

    // Gets the test score from user
    inputAnswers(student, scores);


    // Calculates the average
    cout << "The average score is " << getAverage(scores) << endl;

     // Calculates highest score
    cout << "The high score was student " << student[highIndex(scores)] << " with a score of " << scores[highIndex(scores)]; //uses returned index to find values from array

    return 0;
}

Although i strongly recommend using class or structures for such data collection of any entitiy. Happy Coding :-)

  •  Tags:  
  • c
  • Related