c++ can also gve me the algoritm Part I: Create three arrays: (Be sure to do thi
ID: 3711105 • Letter: C
Question
c++ can also gve me the algoritm
Part I:
Create three arrays: (Be sure to do this first) You can put the data into a file (extra credit) or hard code it in main. The data is as follows:
NAMES GRADES AGES
Anne 67 18
Bob 85 25
Ralph 70 31
Tim 93 57
Barbara 85 20
Jane 83 21
Steve 99 29
Tom 100 25
Mike 89 37
Shirley 81 22
Pam 75 23
Frank 50 19
Part II: Read in the data from a file using a function (extra credit) or assign the data with three initialization statements, like:
string names [12] = {“Anne”, “Bob”, “Ralph”, ….. };
int grades[12] = {67, 85, 70, …………};
int ages[12] = {18, 25, 31, …………..};
and print the names, grades, and ages in the format above, also using a function.
Part III: Calculate the average grade in a function and print it. Then print the names and grades (not ages) and the difference from the average by calling a function.
Part IV: Print all the student names who are over the age of 25, by calling a function.
Part V: Rearrange these people in descending sequence by name (USE A SORT ALGORITHM FROM THE SORTS AND SEARCHES HANDOUT) and print the three arrays. As you sort on name, you must remember to flip the ages and grades.
Part VI: Rearrange these people in ascending order by age (USE A DIFFERENT SORT ALGORITHM FROM THE SORTS AND SEARCHES HANDOUT) and print the three arrays. . As you sort on ages, you must remember to flip the names and grades.
This program should include:
2 different sort functions
a print function which prints two arrays
a print function which prints three arrays. You can call this functionthree times in main.
Make sure all methods are writen to handle n elements, not just 12. You can pass 12 from main to n in the method/function.
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<iomanip>
using namespace std;
double calAvg(int data[], int c){
double sum = 0;
for (int i = 0; i < c; i++)
sum = sum + data[i];
return sum/c;
}
void printData(string names[], int grades[], int ages[], int c){
cout << setw(10) << "NAME" << setw(10) << "GRADES" <<setw(10) << "AGES" << endl;
for (int i = 0; i<c; i++){
cout << setw(10) << names[i] << setw(10) << grades[i] <<setw(10) << ages[i] << endl;
}
}
void printAverageDiff(string names[], int grades[], double avg , int c){
cout << setw(10) << "NAME" << setw(10) << "GRADES" <<setw(10) << "DIFFERENCE" << endl;
for (int i = 0; i<c; i++){
cout << setw(10) << names[i] << setw(10) << grades[i] <<setw(10) << avg - grades[i] << endl;
}
}
void printOver25(string names[], int ages[], int c){
cout << setw(10) << "NAME" << endl;
for (int i = 0; i<c; i++){
cout << setw(10) << names[i] << endl;
}
}
void sortByName(string names[], int grades[], int ages[], int c){
int index;
string nm;
for (int i = 0; i<c; i++){
string nm = names[i];
for (int j = i+1; i<c; i++){
if (nm > names[j]){
nm = names[j];
index = j;
}
}
string temp1 = names[i];
int temp2 = grades[i];
int temp3 = ages[i];
names[i] = names[index];
grades[i] = grades[index];
ages[i] = ages[index];
names[index] = temp1;
grades[index] = temp2;
ages[index] = temp3;
}
printData(names,grades,ages,c);
}
void sortByAge(string names[], int grades[], int ages[], int c){
int index;
int nm;
for (int i = 0; i<c; i++){
nm = ages[i];
for (int j = i+1; i<c; i++){
if (nm > ages[j]){
nm = ages[j];
index = j;
}
}
string temp1 = names[i];
int temp2 = grades[i];
int temp3 = ages[i];
names[i] = names[index];
grades[i] = grades[index];
ages[i] = ages[index];
names[index] = temp1;
grades[index] = temp2;
ages[index] = temp3;
}
printData(names,grades,ages,c);
}
int main(){
string names[100];
int grades[100];
int ages[100];
ifstream fin("input184.txt");
if (!fin){
cout << "Error opening file ";
return 0;
}
string line;
getline(fin,line);
int count = 0;
while (getline(fin,line)){
stringstream ss(line);
ss >> names[count] >> grades[count] >> ages[count];
count++;
}
printData(names,grades,ages,count);
cout << "---------------------------- ";
double avg = calAvg(grades, count);
cout << "Average:"<< avg << endl;
cout << "---------------------------- ";
printAverageDiff(names,grades, avg,count);
cout << "---------------------------- ";
printOver25(names,ages,count);
cout << "---------------------------- ";
sortByName(names,grades,ages,count);
cout << "---------------------------- ";
sortByAge(names,grades,ages,count);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.