I have to organize my array with a bubble sort to display who works the most hours to who works the least amount of hours. After that, I need to display the employee who makes the most amount of pay in the company. However, I'm not sure how I would go about using a bubble sort with pointer variables and a structure member. At the moment I have a generic code for my bubble sort in the arraySort function but it needs improvement. Thank you!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct incomeInfo {
string id;
string name;
int hours;
double hRate;
double regPay = 0;
double otPay = 0;
};
const int ARRAY_SIZE = 25;
incomeInfo income[ARRAY_SIZE];
void getIncome(incomeInfo[], int&);
void compute(incomeInfo *, int);
void display(incomeInfo[], int);
void summary(incomeInfo[], int);
void sortArray(incomeInfo[], int);
void mostPay(incomeInfo[], int);
int main()
{
incomeInfo income[ARRAY_SIZE];
int count = 0;
getIncome(income, count);
compute(income, count);
display(income, count);
summary(income, count);
sortArray(income, count);
mostPay(income, count);
return 0;
}
void getIncome(incomeInfo income[], int &count)
{
ifstream inputFile;
char line[50];
inputFile.open("Payroll.txt");
if (inputFile.fail())
{
cout << "\n\n\tError openning file: " << "\n\n\t";
system("pause");
exit(1);
}
else
{
while (!inputFile.eof())
{
inputFile.getline(line, 50, ',');
income[count].id = line;
inputFile.getline(line, 50, ',');
income[count].name = line;
inputFile.getline(line, 50, ',');
income[count].hours = atoi(line);
inputFile.getline(line, 50, ',');
income[count].hRate = atof(line);
count++;
}
}
inputFile.close();
return;
}
void compute(incomeInfo *ptrI, int count)
{
for (int i = 0; i<count; i++)
if (ptrI->hours <= 40)
{
ptrI->regPay = ptrI->hours * ptrI->hRate;
ptrI++;
}
else if (ptrI->hours > 40)
{
ptrI->regPay = 40 * ptrI->hRate;
ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
ptrI++;
}
return;
}
void display(incomeInfo income[], int count)
{
cout << fixed << showpoint << setprecision(2);
cout << setw(15) << left << "ID" << setw(16) << "Name";
cout << left << setw(8) << "Hours" << setw(14) << "Hourly Rate" << setw(14) << "Regular Pay" << setw(14) << "Overtime Pay" <<endl;
for (int i = 0; i < count; i++)
{
cout << setw(14) << left << income[i].id << setw(15) << income[i].name;
cout << right << setw(6) << income[i].hours << setw(12) << income[i].hRate;
cout << setw(14) << income[i].regPay << setw(14) << income[i].otPay << endl;
}
return;
}
void sortArray(incomeInfo income[], int count)
{
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
}
void summary(incomeInfo income[], int count)
{
cout << endl << endl << "Total payroll amount for the company = $";
cout << (income[0].regPay + income[0].otPay) + (income[1].regPay + income[1].otPay) + (income[2].regPay + income[2].otPay) + (income[3].regPay + income[3].otPay) + (income[4].regPay + income[4].otPay) << endl;
}
void mostPay(incomeInfo[], int count)
{
cout << endl << endl << "Employee who earns most money: ";
}
The logic for the bubble sort would be the same, except you would be using an array of pointers. Only the if statement needs to change, so that it compares using the values in the structures that the array pointers point to.
Your code won't compile in its current state since you reference 'size' and 'array' variables that do not exist, but here's what a bubble sort looks like:
void sortArray(incomeInfo income[], int count) {
for (int i = 0; i < count; i++)
for (int j = 1; j < count; j++)
if (income[j - 1].hours > income[j].hours)
std::swap(income[j - 1], income[j]);
}
Related
I am trying to open the txt file I made for the rest of my code to successfully run. I am using c++ and xcode is the environment. When I run my code, I get this:
error in opening studentScore8.txt
And below that is the correct formatting for the table but weird numbers are there. I am confident that the location for the file is correct, and I am not seeing any issues in my code besides
printGrade(oneScore, average);
in the main function, which xcode is saying that the parameters oneScore and average are not initialized, but I thought I already did that above.
Does anyone know what the issue could be? This is a lab for my comp sci class, and my teacher doesn't seem to know what the issue is either. I am thinking it has something to do with xcode or my computer itself, but I am not sure. The entire code is below.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define inFile "/Users/spacecowboy/Desktop/cis/cis/lab 8/studentScore8.txt"
const int MAX_SIZE = 4;
void readStuData (ifstream& rss, int scores[], int id[], int& count, bool& tooMany);
float mean(int scores[], int count);
void printTable(int scores[], int id[], int count);
void printGrade(int oneScore, float average);
int main() {
ifstream rss;
// void printTable(scores, id, count);
// float mean(int scores, int count);
float average;
bool tooMany;
int scores[MAX_SIZE];
int oneScore;
int id[MAX_SIZE];
int count;
rss.open("studentScore8.txt");
if (rss.fail()){
cout << "error in opening studentScore8.txt" << endl;
}
readStuData(rss, scores, id, count, tooMany);
mean(scores, count);
printGrade(oneScore, average);
printTable(scores, id, count);
rss.close();
return 0;
}
void readStuData(ifstream& rss, int scores[], int id[], int& count,bool& tooMany){
int stuID;
int stuScore;
int i;
for (i = 0; i < MAX_SIZE; ++i) {
rss >> stuID;
rss >> stuScore;
id[i] = stuID;
scores[i] = stuScore;
++count;
}
if (count > MAX_SIZE){
tooMany = true;
}
else {
tooMany = false;
}
}
float mean(int scores[], int count)
{
float sum = 0;
int i;
for (i = 0; i < count; ++i) {
sum = sum + scores[i];
}
return sum / count;
}
void printGrade(int oneScore, float average){
ifstream rss;
rss >> oneScore;
if (((oneScore >= average -10) && (oneScore <= average + 10))) {
cout << "Satisfactory";
}
else if (oneScore == average + 10){
cout << "Satisfactory";
}
else if (oneScore > average + 10){
cout << "Outstanding";
}
else {
cout << "Unsatisfactory";
}
}
void printTable(int scores[], int id[], int count){
int i;
cout << endl;
cout << setw(10) << left << "Student ID" << "|";
cout << setw(6) << right << "Score" << "|" ;
cout << setw(14) << left << "Grade" << endl;
cout << setfill('-') << setw(30) << "" << endl;
for(i = 0; i < MAX_SIZE; ++i){
cout << left << setw(10) << id[i] << "|";
cout << left << " ";
cout << setw(2) << scores[i];
cout << right << " " << "|";
printGrade(scores[i], mean(scores, count));
cout << endl;
}
cout<< setfill('-') << setw(30) << "" << endl;
}
I have an assignment to sort a list of names alphabetically by last name. However, there are three names with the same surname and I can't get the first names to alphabetize with the surnames. Have to code own bubble sort or other sorting algorithm. I chose bubblesort because it's one of the only ones we've learned so far. Any help is appreciated. Everything works except the correct assortment.
Here is my code:
// my name
// Program 6
// This program will show a list of names in a variety of orders.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int size = 25;
void showContacts_FNLN(string fnameArray[], string lnameArray[], int size);
void showContacts_LNFN(string lnameArray[], string fnameArray[], int size);
void reverseContacts_FNLN(string fnameArray[], string lnameArray[], int size);
void reverseContacts_LNFN(string fnameArray[], string lnameArray[], int size);
void searchLasname(string lnameArray[], string fnameArray[], int size);
void searchFirname(string fnameArray[], string lnameArray[], int size);
void bubbleSort(string lnameArray[], string fnameArray[], int size);
int main(int argc, const char * argv[])
{
int count = 0;
int ans;
ifstream nameData;
string lnameArray[size];
string fnameArray[size];
string lname, fname;
nameData.open("names.txt");
while(nameData >> fname >> lname)
{
fnameArray[count] = fname;
lnameArray[count] = lname;
count ++;
}
bubbleSort(lnameArray, fnameArray, size);
while(ans != 9)
{
cout << "What would you like to do?" << endl;
cout << "1) display contacts by first name and last name" << endl;
cout << "2) display contacts by last name and first name" << endl;
cout << "3) display contacts by first name and last name in reverse order" << endl;
cout << "4) display contacts by last name and first name in reverse order" << endl;
cout << "5) search for contact by last name" << endl;
cout << "6) search for contact by first name" << endl;
cout << "9) exit" << endl;
cout << "Enter: ";
cin >> ans;
cout << endl;
switch(ans)
{
case 1: showContacts_FNLN(fnameArray, lnameArray, size); // shows contacts in FN-LN order
break;
case 2: showContacts_LNFN(lnameArray, fnameArray, size); // LN-FN order
break;
case 3: reverseContacts_FNLN(fnameArray, lnameArray, size); // reversed FN-LN order
break;
case 4: reverseContacts_LNFN (fnameArray, lnameArray, size); // reversed LN-FN order
break;
case 5: searchLasname(lnameArray, fnameArray, size); // searches based on LN
break;
case 6: searchFirname(fnameArray, lnameArray, size); // searches based on FN
break;
case 9: cout << "Goodbye!" << endl;
break;
}
}
nameData.close();
return 0;
}
void showContacts_FNLN(string fnameArray[], string lnameArray[], int size)
{
for(int i=0; i<size; i++)
{
cout << fnameArray[i] << " " << lnameArray[i] << endl;
}
cout << endl;
}
void showContacts_LNFN(string lnameArray[], string fnameArray[], int size)
{
for(int i=0; i<size; i++)
{
cout << lnameArray[i] << ", " << fnameArray[i] << endl;
}
cout << endl;
}
void reverseContacts_FNLN(string fnameArray[], string lnameArray[], int size)
{
for(int i=(size-1); i>=0; i--)
{
cout << fnameArray[i] << " " << lnameArray[i] << endl;
}
cout << endl;
}
void reverseContacts_LNFN(string fnameArray[], string lnameArray[], int size)
{
for(int i=(size-1); i>=0; i--)
{
cout << lnameArray[i] << ", " << fnameArray[i] << endl;
}
cout << endl;
}
void searchLasname(string lnameArray[], string fnameArray[], int size)
{
int c = 0;
string slnam;
cout << "Enter a last name: ";
cin >> slnam;
for(int i=0; i<size; i++)
{
if(slnam==lnameArray[i])
{
cout << lnameArray[i] << ", " << fnameArray[i] << endl;
c++;
}
}
if (c == 0)
{
cout << "There is no match.";
cout << endl;
}
cout << endl;
}
void searchFirname(string fnameArray[], string lnameArray[], int size)
{
int c = 0;
string sfnam;
cout << "Enter a first name: ";
cin >> sfnam;
for(int i=0; i<size; i++)
{
if(sfnam==fnameArray[i])
{
cout << fnameArray[i] << " " << lnameArray[i] << endl;
c++;
}
}
if (c==0)
{
cout << "There is no match." << endl;
}
cout << endl;
}
void bubbleSort(string lnameArray[], string fnameArray[], int size)
{
string tmp, tmp2;
//int count=0;
for( int i = 1; i <= size - 1; i++ )
{
for( int j = 0; j < size - i; j++ )
{
//count++;
if( lnameArray[j] > lnameArray[j+1] )
{
tmp = lnameArray[j];
lnameArray[j] = lnameArray[j+1];
lnameArray[j+1] = tmp;
}
if(lnameArray[j] == lnameArray[j+1])
{
if(fnameArray[j] > fnameArray[j+1])
{
tmp = lnameArray[j];
lnameArray[j] = lnameArray[j+1];
lnameArray[j+1] = tmp;
tmp2 = fnameArray[j];
fnameArray[j] = fnameArray[j+1];
fnameArray[j+1] = tmp2;
}
}
}
}
//cout << "count = " << count << endl;
}
Here you go:
void bubbleSort(string lnameArray[], string fnameArray[], int size)
{
for( int i = 0; i < size - 1; i++ )
{
for( int j = 0; j < size - i - 1; j++ )
{
string name1 = lnameArray[j] + fnameArray[j];
string name2 = lnameArray[j + 1] + fnameArray[j + 1];
if(name1.compare(name2) > 0)
{
string tmp = lnameArray[j];
lnameArray[j] = lnameArray[j + 1];
lnameArray[j + 1] = tmp;
tmp = fnameArray[j];
fnameArray[j] = fnameArray[j + 1];
fnameArray[j + 1] = tmp;
}
}
}
}
Instead of having another 'if' for those that have similar last names, I combined the last name and the first name instead then used it for comparison.
I also noticed in your code in for( int j = 0; j < size - i; j++ ) that you forgot to add - 1 after size - i. Once j == size - i - 1 (assuming that i == 0 currently) then you use j + 1 to access an index, this will cause a segmentation fault since you're accessing an index beyond its range.
Your sort doesn't work because you change the position of the last names only (except for the case where two last names are equal.)
You should use a struct to store this information together in one array. Then you can simplify your code a lot:
struct Person {
Person() = default;
Person(string firstname, string lastname) : firstname(std::move(firstname)), lastname(std::move(lastname)) {}
string firstname;
string lastname;
};
void showContacts_FNLN(Person personArray[], int size) {
for (int i = 0; i < size; i++) {
cout << personArray[i].firstname << " " << personArray[i].lastname << endl;
}
cout << endl;
}
void bubbleSort(Person personArray[], int size) {
for (int i = 1; i <= size - 1; i++) {
// The condition must size - i - 1 because otherwise personArray[j+1] is faulty
for (int j = 0; j < size - i - 1; j++) {
if (personArray[j].lastname > personArray[j+1].lastname
|| (personArray[j].lastname == personArray[j+1].lastname &&
personArray[j].firstname > personArray[j+1].firstname)) {
auto tmp = std::move(personArray[j]);
personArray[j] = std::move(personArray[j+1]);
personArray[j+1] = std::move(tmp);
}
}
}
}
int main(int argc, const char * argv[]) {
int count = 0;
ifstream nameData;
Person personArray[size];
string lname, fname;
nameData.open("names.txt");
while(nameData >> fname >> lname) {
personArray[count] = Person(fname, lname);
count++;
}
nameData.close();
bubbleSort(personArray, size);
showContacts_FNLN(personArray, size);
return 0;
}
I removed some non-essential parts from this example to keep it very short but you should get the idea. (Note that my code makes use of C++11 move semantics, if this confuses you, just remove them.)
Some more suggestions:
Do not use using namespace std;. (The internet will tell you why.)
If you can, use std::array, which eliminates the need to always pass the size of the arrays and allows you to use a C++11 range for loop.
I need to write a program that cleans up two arrays with 12 scores and gets rid of the "bonus scores" (where the denominator is 0). I've looked at similar problems, but I'm at a point in which there's three main problems that either no one seems to have or haven't been addressed. the first one is when declaring the new (not bonus) arrays in main it requires that I enter a constant value, however; the size of the arrays must change for the clean_data function to work.
the second one is when calling clean_data in main it marks "deno" and "num" as an error saying that "argument of type int * is incompatible with parameter of type int**", and the last one says that the function clean_data cannot convert argument 1 from int[12] to int*[].
I've been poking at this code for 3 days and looked at my professor's notes and textbook and can't find a way to fix it. please help.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
//prototypes for functions
void read_data(int nume[], int deno[], int size);
void report_data(int nume[], int deno[], int size);
void report_overall_grade(int nume[], int deno[], int size);
//Data cleanup
void clean_data(int *nume[], int *deno[], int size, int *notBonusNum[], int *notBonusDen[], int &newSize);
int main()
{
//declare constat, arrays, and variables;
const int NUM_GRADES = 12;
int nume[NUM_GRADES];
int deno[NUM_GRADES];
int newSize = 12;
int notBonusNum[newSize];
int notBonusDen[newSize];
//call functions
read_data(nume, deno, 12);
cout << "Original scores: " << endl;
cout << fixed << showpoint << setprecision(1);
report_data(nume, deno, 12);
system("pause");
system("cls");
report_overall_grade(nume, deno, 12);
//Data cleanup
clean_data(nume, deno, 12, notBonusNum, notBonusDen, newSize);
cout << "There are " << newSize << " scores that aren't bonuses:" << endl;
report_data(notBonusNum, notBonusDen, newSize);
system("pause");
return 0;
}
//function definitions
void read_data(int nume[], int deno[], int size)
{
//open file
ifstream inputFile;
inputFile.open("scores.txt");
if (inputFile.fail())
{
cout << "File does not exist." << endl;
system("pause");
exit(1);
}
//read data
int count;
for (count = 0; count < size; count++)
{
inputFile >> nume[count];
inputFile >> deno[count];
}
inputFile.close();
}
void report_data(int nume[], int deno[], int size)
{
int count;
int number = 1;
for (count = 0; count < size; count++)
{
if (deno[count] > 0)
{
int numerator = nume[count];
int denominator = deno[count];
double percent = (double)numerator / denominator * 100;
cout << "Score " << number << " is " << numerator << " / " << denominator << " = " << percent << "%." << endl;
}
else
{
cout << "Score " << number << " is " << nume[count] << " / " << deno[count] << " = Bonus Points!" << endl;
}
number++;
}
}
void report_overall_grade(int nume[], int deno[], int size)
{
int count;
int totalNum = 0;
int totalDeno = 0;
double overallGrade;
for (count = 0; count < size; count++)
{
totalNum += nume[count];
totalDeno += deno[count];
}
cout << "Total points earned: " << totalNum << endl;
cout << "Total points possible: " << totalDeno << endl;
overallGrade = (double)totalNum / totalDeno * 100;
cout << "Overall grade: " << overallGrade << endl;
}
//Data cleanup
void clean_data(int *nume[], int *deno[], int size, int *notBonusNum[], int *notBonusDen[], int &newSize)
{
int count;
for (count = 0; count < size; count++)
{
int count2 = 0;
if (deno[count] != 0)
{
notBonusNum[count2] = nume[count];
notBonusDen[count2] = deno[count];
newSize--;
count2++;
}
}
}
Count all the records from the file "8.dat". To read each individual recording perform dynamic memory capture.
Sort the records to different keys:
Item number (ascending);
The cost (descending);
Number of stock (descending).
Use selection sort
Total sorting will be done 12 times, each time the array is sorted in its original condition.
For each case count of comparisons to and permutations.
Below code implements insertion sort. Twice, without saying so much.
I need to use selection sort. How to do selection sort?
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
struct PRICE
{
int number;
char name[20];
int cost;
int quantity;
} *pm;
int Menu();
void PrintPRICE(PRICE);
void sort_cost(PRICE*, int);
void sort_quantity(PRICE*, int);
long file_size(const char*);
int main()
{
int count = 0;
const char *fname = "D:\8.dat";
FILE* file = fopen(fname, "r");
if (file != NULL)
{
long size = file_size(fname);
count = size / sizeof PRICE;
pm = new PRICE[count];
fread(pm, sizeof PRICE, count, file);
fclose(file);
}
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
int ch = Menu();
switch (ch)
{
case 1:
{
sort_cost(pm, count);
cout << endl;
cout << " Result: " << endl;
cout << "-----------------------" << endl;
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
break;
}
case 2:
{
sort_quantity(pm, count);
cout << " Result: " << endl;
cout << "-----------------------" << endl;
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
break;
}
default: break;
}
delete [] pm;
_getch();
}
void PrintPRICE(PRICE price)
{
cout << " Product: " << price.name << endl;
cout << " Number of orden: " << price.number << endl;
cout << " Product cost: " << price.cost << endl;
cout << " Quantity in stock: " << price.quantity << endl;
cout << "-----------------------------------n" << endl;
}
long file_size(const char* filename)
{
FILE *Pfile = NULL;
Pfile = fopen(filename, "rb");
fseek(Pfile, 0, SEEK_END);
long size = ftell(Pfile);
fclose(Pfile);
return size;
}
void sort_cost(PRICE* array, int count)
{
int change = 0;
int comparesion = 0;
for (int i=1; i<count; i++)
{
PRICE key = array[i];
int j = i - 1;
comparesion++;
while (i>=0 && array[i].cost>key.cost)
{
array[j + 1] = array[j];
j = j - 1;
change++;
}
array[j + 1] = key;
}
cout << "n Quantity change: " << change << endl;
cout << " Quantity comparesion: " << comparesion << endl;
}
void sort_quantity(PRICE* array, int count)
{
int change = 0;
int comparesion = 0;
for (int i=1; i<count; i++)
{
PRICE key = array[i];
int j = i - 1;
comparesion++;
while (j>=0 && array[i].quantity>key.quantity)
{
array[j + 1] = array[j];
j = j - 1;
change++;
}
array[j + 1] = array[j];
}
cout << "n Quantity change: " << change << endl;
cout << " Quantity comparesion: " << comparesion << endl;
}
int Menu()
{
int n;
cout << " 1 - Sort by cost" << endl;
cout << " 2 - Sort by quantity" << endl;
cout << "n Your choice: "; cin >> n;
return n;
}
source code for the selection sort
void selectSort(int arr[], int n)
{
int pos_min,temp;
for (int i=0; i < n-1; i++)
{
pos_min = i;
for (int j=i+1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min=j;
}
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
Please help me with my homework. I've got this program working just fine in debug mode, but as soon I use release mode it crashes with an abort().
I know it probably has something to do with memory allocation, but I don't understand pointers well enough.
Requirement is that I have to use an *array to dynamically allocate memory.
"Your program should work for any number of students. When the program
starts, it should ask the user for the number of students to be
processed. Then it should dynamically allocate an array of that size
(array of student/score structures)."
I then need to, "Call a function to input the student name/score pairs and store them in the array."
So should I create the array in main or inside of the function? How can I return/pass the *array without messing up memory allocation?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Student
{
string name; //student's name
int score; //student's score
};
//function prototypes
void inputNameScore(Student*, int&);
void sortArray(Student* , int);
double avgScore(Student* , int);
void displayTable(Student* , int, double);
int main()
{
Student* arrayOfStudentPtr; //pointer of type student to receive returned array pointer
int numberOfStudents; //number of students to be entered by user
double average; //total score average
cout << "Please enter the number of students: ";
cin >> numberOfStudents;
arrayOfStudentPtr = new Student[numberOfStudents]; //dynamic array of type Student assigned to pointer
inputNameScore(arrayOfStudentPtr, numberOfStudents);
sortArray(arrayOfStudentPtr, numberOfStudents);
average = avgScore(arrayOfStudentPtr, numberOfStudents);
displayTable(arrayOfStudentPtr, numberOfStudents, average);
return 0;
}
void inputNameScore(Student* arrayOfStudentPtr, int& numberOfStudents)
{
for (int i = 0; i < numberOfStudents; i++)
{
cout << endl << "Enter the name for student " << i + 1 << ": ";
cin.ignore();
getline(cin, arrayOfStudentPtr[i].name);
cout << endl << "Enter the student's score: ";
cin >> arrayOfStudentPtr[i].score;
while (arrayOfStudentPtr[i].score > 105 || arrayOfStudentPtr[i].score < 0)
{
cout << "Student's score can't be negative or greater than 105." << endl;
cout << endl << "Enter the student's score: ";
cin >> arrayOfStudentPtr[i].score;
}
}
}
void sortArray(Student* arrayOfStudentPtr, int numberOfStudents)
{
Student Temp; //holds a student struct object
bool swap; //swap is initialized to false at the start of each loop. If it is still false at end of loop we know there is nothing else to sort
do
{
swap = false;
for (int i = 0; i < numberOfStudents; i++)
{
if (arrayOfStudentPtr[i].score > arrayOfStudentPtr[i + 1].score)
{
Temp = arrayOfStudentPtr[i];
arrayOfStudentPtr[i] = arrayOfStudentPtr[i + 1];
arrayOfStudentPtr[i + 1] = Temp;
swap = true;
}
}
} while (swap);
}
double avgScore(Student* arrayOfStudentPtr, int numberOfStudents)
{
int total; //total of all grades
double average; //average of all grades
total = 0;
for (int i = 0; i < numberOfStudents; i++)
{
total = arrayOfStudentPtr[i].score;
}
average = total / numberOfStudents;
//average = static_cast<double>(total) / numberOfStudents;
return average;
}
void displayTable(Student* arrayOfStudentPtr, int numberOfStudents, double average)
{
cout << endl << setw(31) << left << "Name" << setw(6) << right << "Score" << endl;
cout << "-------------------------------------" << endl;
for (int i = 0; i < numberOfStudents; i++)
{
cout << setw(31) << left << arrayOfStudentPtr[i].name << setw(6) << right << arrayOfStudentPtr[i].score << endl;
}
cout << "-------------------------------------" << endl;
cout << setw(31) << left << "Average: " << setw(6) << right << endl;
}
The following code will work.
void sortArray(Student* arrayOfStudentPtr, int numberOfStudents)
{
Student Temp; //holds a student struct object
bool swap; //swap is initialized to false at the start of each loop. If it is still false at end of loop we know there is nothing else to sort
do
{
swap = false;
for (int i = 0; i < numberOfStudents-1; i++)
{
if (arrayOfStudentPtr[i].score > arrayOfStudentPtr[i + 1].score)
{
Temp = arrayOfStudentPtr[i];
arrayOfStudentPtr[i] = arrayOfStudentPtr[i + 1];
arrayOfStudentPtr[i + 1] = Temp;
swap = true;
}
}
} while (swap);
}