Sorting an array

So I need help with a sorting function. I have three files: header.h, main.cpp, and functions.cpp. Here are those:

-------------------------------------------------------------------------------
header.h:
-------------------------------------------------------------------------------
#include "header.h"
#include <iostream>
using namespace std;

int main()
{
greeting();
const int NUM_STUDENTS = 7;
Student stu[NUM_STUDENTS];
getInput(stu, NUM_STUDENTS);
int max = maxGrade(stu, NUM_STUDENTS);
int min = minGrade(stu, NUM_STUDENTS);
float avg = avgGrade(stu, NUM_STUDENTS);
print (max, min, avg);
signOff();
return 0;
}
------------------------------------------------------------------------------
functions.cpp:
------------------------------------------------------------------------------
#include <iostream>
#include "header.h"
using namespace std;

void greeting()
{
cout << endl << endl;
cout <<"Welcome to the student database.";
cout << endl << endl;
return;
}

void getInput(struct Student s[], const int SIZE)
{
for (int i=0; i<SIZE; i++)
{
cout <<"ID of student: ";
cin >> s[i].id;
cout <<"Grade of student " << s[i].id <<": ";
cin >> s[i].grade;
cout << endl;
}
return;
}

int maxGrade(struct Student s[], const int SIZE)
{
int max = 0;
for (int i = 0; i < SIZE; i++)
{
if (s[i].grade > max)
{
max = s[i].grade;
}
}
return max;
}

int minGrade(struct Student s[], const int SIZE)
{
int min = 1000;
for (int i = 0; i < SIZE; i++)
{
if (s[i].grade < min)
{
min = s[i].grade;
}
}
return min;
}

float avgGrade(struct Student s[], const int SIZE)
{
int total = 0;
float avg = 0;
for (int i = 0; i < SIZE; i++)
{
total += s[i].grade;
}
avg = total / 7;
return avg;
}

void signOff()
{
cout << endl << endl;
cout <<"Thank you for using the student database.";
cout << endl << endl;
return;
}
-------------------------------------------------------------------------------
main.cpp
-------------------------------------------------------------------------------
#include "header.h"
#include <iostream>
using namespace std;

int main()
{
greeting();
const int NUM_STUDENTS = 7;
Student stu[NUM_STUDENTS];
getInput(stu, NUM_STUDENTS);
int max = maxGrade(stu, NUM_STUDENTS);
int min = minGrade(stu, NUM_STUDENTS);
float avg = avgGrade(stu, NUM_STUDENTS);
print (max, min, avg);
signOff();
return 0;
}
-------------------------------------------------------------------------------

gradeSort() should go after print() in main. Thanks in advance for any help.
Help_with != write_for();

there are lots of clever ways to sort data. A simple one, though, is best to start out.

Can you iterate thru the array and find the smallest one?
Can you swap the smallest and first one?
wrap the above in a loop that then looks at the second location onward, finds the smallest, swaps, ... and it will be sorted.

(This is due in an hour btw) OK Update: I think what I have will work but it is not compiling. I am getting this one error:
main.cpp:26:30: error: cannot convert 'float' to 'Student*' for argument '1' to 'void sort(Student*, int)'
sort(avg, stu, NUM_STUDENTS);

Here is the function:
void sort(float avg, struct Student s[], const int SIZE)
{
int minimum = 0;
int maximum = 0;
cout <<"Students in sorted order: ";
for (int i = 0; i <= SIZE; i++)
{
if (s[i].grade < avg)
s[i].grade = minimum;
else if (s[i].grade > avg)
s[i].grade = maximum;
cout << s[i].id <<" ";
}
cout << endl;
}
hope you got it but your issue was mismatched parameters, probably a prototype and body mismatch like this

void foo(int x); //these two should match
void foo(string s, int x)
{
....
}



Topic archived. No new replies allowed.