Need help with Parallel array with strings and double

Hello, I was given the exercise below at school. I have read the chapter many times but I cannot find a way to gather string variables and double variables at the same time from the file. Creating these functions is proving difficult. I appreciate any help in advance This is what the exercise looks like:

Write a program to read in the following file:

GPA.txt

Alvin 2.5
Arthur 4.0
Bart 1.0
Beavis 0.0
Buttercup 3.8
Donald 1.5
Homer 1.0
Mickey 3.9
Pebbles 1.4
Pikachu 2.0
Shaggy 1.2
Spiderman 2.9
Tweety 2.0
Wilma 2.3
Yogi 4.0

Store the names in an array of strings and the GPAs in an array of doubles.

Create a function that takes in the array of strings and the array of doubles and returns the name and GPA of the student with the lowest GPA.

Create a function that takes in the array of strings and the array of doubles and returns the name and GPA of the student with the highest GPA.

Create a function that takes in the array of strings and the array of doubles and returns the average GPA.

Call all three of your functions from main and display their values to the screen.
Last edited on
Each name is a string without spaces, followed by a floating point number.

1
2
3
4
5
6
while (cin >> name >> gpa)
  {
  student_names[ student_count ] = name;
  student_gpas[ student_count ] = gpa;
  student_count += 1;
  }

Hope this helps.
Thank you for your help. Would you mind posting the declaration of the variables before this step. I still get an error. this is what I did:

const int ARRAY_SIZE = 15;
string student_names[ARRAY_SIZE];
double student_gpas[ARRAY_SIZE];
int student_count = 0;
ifstream inputFile;


inputFile.open("gpa.txt");

while (cin >> name >> student_gpas)
{
student_names[ student_count ] = name;
student_gpas[ student_count ] = student_gpas;
student_count += 1;
}
/*while(count < ARRAY_SIZE && inputFile >> names[count])
count++;*/

for (count = 0; count < ARRAY_SIZE; count++)
cout << student_names[count] << endl;
What type of thing is name?
What type of thing is gpa?

Declare each variable the appropriate type of thing.
It's also possible to write it like so:
1
2
3
4
while (cin >> student_names[ student_count ] >> student_gpas[ student_count ])
  {
  student_count += 1;
  }
I wanted to ask you the same thing. You used the variable without any declarations so i could not understand " while (cin >> name >> student_gpas)
{
student_names[ student_count ] = name;
student_gpas[ student_count ] = student_gpas;
student_count += 1;
} "

I need to store the names in one arraqy and the averages in another array.
I figured it out just in case someone finds this forum and needs a similar code.
Here it is:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void highestGpa (string[], double[], const int);
void lowestGpa ( string[], double[],const int);
void averageGpa ( string[],double[], const int);


int main()
{
const int ARRAY_SIZE = 15;
string names[ARRAY_SIZE];
double gpas[ARRAY_SIZE];
int count = 0;
ifstream inputFile;


inputFile.open("gpa.txt");


while(count < ARRAY_SIZE)
{
inputFile >> names[count];
inputFile >> gpas[count];
count++;
}
highestGpa(names, gpas, ARRAY_SIZE);

lowestGpa(names, gpas, ARRAY_SIZE);

averageGpa(names, gpas, ARRAY_SIZE);

inputFile.close();

return 0;
}



void highestGpa(string names[], double gpas[], const int ARRAY_SIZE)
{
int count;
double highest;
string highname;
highest = gpas[0];



for (count = 1; count < ARRAY_SIZE; count++)
{
if (gpas[count] > highest)
{
highest = gpas[count];
highname = names[count];
}
}
cout << fixed << showpoint << setprecision(1);
cout << "The highest GPA is: " << highname << ":" << highest << endl;
}


void lowestGpa(string name[], double gpas[], const int ARRAY_SIZE)
{
int count;
double lowest = gpas [0];
string lowname;
lowest = gpas[0];

for (count = 1; count < ARRAY_SIZE; count++)
{
if (gpas[count] < lowest)
{
lowest = gpas[count];
lowname = name[count];
}
}
cout << fixed << showpoint << setprecision(2);
cout << "\nThe lowest GPA is: " << lowname << ":" << lowest << endl;
}

void averageGpa(string names[], double gpa[], const int ARRAY_SIZE)
{
int count;
double total;
double average;


for (count = 0; count < ARRAY_SIZE; count++)
{

total += gpa[count];

}

average = total / ARRAY_SIZE;


cout << fixed << showpoint << setprecision(2);
cout << "\nThe average GPA is: " << average << endl;
}
I figured it out
Only half of it. What if there are less then ARRAY_SIZE elements in the file? Well the rest will remain undefined and you get wrong results.

You used the variable without any declarations
Hm? I used the declaration you provided:
1
2
3
4
5
const int ARRAY_SIZE = 15;
string student_names[ARRAY_SIZE];
double student_gpas[ARRAY_SIZE];
int student_count = 0;
ifstream inputFile;
Why is it so difficult? Use the types you need. In this case string and double

A real solution would look like this:
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
int main()
{
const int ARRAY_SIZE = 15;
string names[ARRAY_SIZE];
double gpas[ARRAY_SIZE];
int count = 0;
ifstream inputFile;


inputFile.open("gpa.txt");


while(inputFile >> names[count] >> gpas[count])
{
count++;
if(count < ARRAY_SIZE)
 ;
else
  break;
}
highestGpa(names, gpas, count);

...

return 0;
}
Topic archived. No new replies allowed.