In: Computer Science
Add Bubble Sorting & Binary Search Functions to the following code (C++)
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
const int row = 10;
const int col = 7;
ifstream name;
ifstream grade;
ofstream out;
void readData(string stname[row], int stgrade[row][col]);
void stsum(int stgrade[row][col], int total[row]);
void staverage(int total[row], double average[row]);
void stlettergrade(double average[row],char ltrgrade[row]);
void displayData(string stname[row], int stgrade[row][col], int
total[row], double staverage[row], char ltrgrade[row]);
void swapltrgrade(char* xp, char* yp);
int main()
{
int STG[row][col] = { {0},{0} };
string STN[row] = {};
int STT[row] = { 0 };
double STA[row] = { 0.0 };
char STLG[row] = {};
readData(STN,STG);
stsum(STG, STT);
staverage(STT, STA);
stlettergrade(STA, STLG);
displayData(STN, STG, STT,STA, STLG);
name.close();
grade.close();
out.close();
system("pause");
return 0;
}
//========================Function to Read
Data===================================
void readData(string stname[row], int stgrade[row][col])
{
name.open("Name.txt");
grade.open("Grade.txt");
int r, c;
for (r = 0; r < row; r++)
{
getline(name, stname[r]);
for (c = 0; c < col; c++)
{
grade >>
stgrade[r][c];
}
}
}
//==================Funtion for Grade
Total======================================
void stsum(int stgrade[row][col], int total[row])
{
int r, c;
for (r = 0; r < row; r++)
{
for (c = 0; c
< col; c++)
total[r] = total[r] + stgrade [r][c];
}
}
//=========================Function for
Average=================================
void staverage(int total[row], double average[row])
{
int r, c;
for (r=0;r<row; r++)
{
for (c = 0; c < col; c++)
average[r] =
static_cast<double>(total[r]/col);
}
}
//========================Function for Letter
Grade==============================
void stlettergrade(double average[row], char ltrgrade[row])
{
int r;
for (r = 0; r < row; r++)
{
if (average[r]
>= 90)
ltrgrade[r] = 'A';
else if
(average[r] >= 80)
ltrgrade[r] = 'B';
else if
(average[r] >= 70)
ltrgrade[r] = 'C';
else if
(average[r] >= 60)
ltrgrade[r] = 'D';
else
ltrgrade[r] = 'F';
}
}
//=============================Function to Display
Data=======================
void displayData(string stname[row], int stgrade[row][col], int
total[row], double staverage [row], char ltrgrade[row])
{
out.open("Results.txt");
int r, c;
for (r = 0; r < row; r++)
{
out <<
stname[r] << setw(3);
for (c = 0; c
< col; c++)
out << setw(5) <<
stgrade[r][c];
out <<
setw(5) << total[r]<< setw(5) <<
staverage[r]<< setw(5) << ltrgrade[r] <<
endl;
}
}
Since you have not provided any information regarding the specifics of Bubble sort and binary search (like what is the array to be sorted using bubble sort, etc), i am providing generic implementation of both of them.
CODE
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
const int row = 10;
const int col = 7;
ifstream name;
ifstream grade;
ofstream out;
void swap(double *xp, double *yp)
{
double temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(double arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
int binarySearch(double arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
void readData(string stname[row], int stgrade[row][col]);
void stsum(int stgrade[row][col], int total[row]);
void staverage(int total[row], double average[row]);
void stlettergrade(double average[row],char ltrgrade[row]);
void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]);
void swapltrgrade(char* xp, char* yp);
int main()
{
int STG[row][col] = { {0},{0} };
string STN[row] = {};
int STT[row] = { 0 };
double STA[row] = { 0.0 };
char STLG[row] = {};
readData(STN,STG);
stsum(STG, STT);
staverage(STT, STA);
stlettergrade(STA, STLG);
displayData(STN, STG, STT,STA, STLG);
name.close();
grade.close();
out.close();
system("pause");
return 0;
}
//========================Function to Read Data===================================
void readData(string stname[row], int stgrade[row][col])
{
name.open("Name.txt");
grade.open("Grade.txt");
int r, c;
for (r = 0; r < row; r++)
{
getline(name, stname[r]);
for (c = 0; c < col; c++)
{
grade >> stgrade[r][c];
}
}
}
//==================Funtion for Grade Total======================================
void stsum(int stgrade[row][col], int total[row])
{
int r, c;
for (r = 0; r < row; r++)
{
for (c = 0; c < col; c++)
total[r] = total[r] + stgrade [r][c];
}
}
//=========================Function for Average=================================
void staverage(int total[row], double average[row])
{
int r, c;
for (r=0;r<row; r++)
{
for (c = 0; c < col; c++)
average[r] = static_cast<double>(total[r]/col);
}
}
//========================Function for Letter Grade==============================
void stlettergrade(double average[row], char ltrgrade[row])
{
int r;
for (r = 0; r < row; r++)
{
if (average[r] >= 90)
ltrgrade[r] = 'A';
else if (average[r] >= 80)
ltrgrade[r] = 'B';
else if (average[r] >= 70)
ltrgrade[r] = 'C';
else if (average[r] >= 60)
ltrgrade[r] = 'D';
else
ltrgrade[r] = 'F';
}
}
//=============================Function to Display Data=======================
void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage [row], char ltrgrade[row])
{
out.open("Results.txt");
int r, c;
for (r = 0; r < row; r++)
{
out << stname[r] << setw(3);
for (c = 0; c < col; c++)
out << setw(5) << stgrade[r][c];
out << setw(5) << total[r]<< setw(5) << staverage[r]<< setw(5) << ltrgrade[r] << endl;
}
}