here is the question:
Write a program that reads names and gpa’s from a text file. The file looks like:
James 3.9
Margaret 3.5
Charles 1.2
Jennifer 4.0
Your program sorts the students ascending by gpa, and prints the sorted list including names. To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers. Sort the numbers. Whenever you swap 2 numbers also swap their matching names in the names array. Include a structure chart as in the model lab.
Here is the error i keep getting:
Error 1 error C2664: 'gpa_Sorter' : cannot convert parameter 1 from 'double [2000]' to 'double' c:\users\goncalvesa2\documents\visual studio 2010\projects\lab2problem2\lab2problem2\lab2problem2.cpp 31 1 Lab2Problem2
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 2000
using namespace std;
ifstream in;
int readArray(string* a);
void gpa_Sorter(double studentGPA, int size);
int main()
{
string a[MAX];
double studentGPA[MAX];
in.open("TextFile1.txt");
if(!in.is_open ())
{
cout << "Failed to open "<< endl;
cout << "Now exiting program...\n";
}
if (in.fail())
{
cout << "opening file failed." << endl;
}
int size = readArray(a);
gpa_Sorter(studentGPA, size);
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
}
return 0;
}
int readArray(string a)
{
double studentGPA[MAX];
int i = 0;
while(i < MAX && !in.eof())
{
in >> a[i];
in >> studentGPA[i];
i++;
}
if (i > MAX)
{
cout << "the file truncated at " << MAX << endl;
}
return i;
}
void gpa_Sorter(double studentGPA[], int size)
{
for (int pass = 0; pass < size - 1; pass++)
{
for( int cell = 0; cell < size - pass - 1; cell++)
{
if(studentGPA[cell] > studentGPA[cell] + 1)
{
double temp = studentGPA[cell];
studentGPA[cell] = studentGPA[cell + 1];
studentGPA[cell + 1] = temp;
}
}
}
}