Question

In: Computer Science

Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...

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;
       }
}

Solutions

Expert Solution

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;

}

}


Related Solutions

C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using namespace std; // class definition for asset class Asset{ protected: int value; // value of asset in cents public: Asset(int value); // constructor int get_value(); // get the value }; Asset::Asset(int val){ // implementation of constructor value=val; } int Asset::get_value(){ // implementation of get_value return value; } // abstract class for office equipment class OfficeEquipment:public Asset{ public: OfficeEquipment(int val); // constructor virtual void use_item()...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT