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...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
C++ The txt file is not showing up in my output #include <iostream> #include <fstream> #include...
C++ The txt file is not showing up in my output #include <iostream> #include <fstream> #include <string> #include "Classroom.h" using namespace std; Classroom myLib; void loadDataset(string fileName) { ifstream myFile; myFile.open(fileName.c_str()); } void continueMessage(string message) { cout << message << endl; cout << "Press Enter to continue.." << endl; cin.get(); } int main() { string fileName = "dataset.txt"; loadDataset(fileName); myLib.print(); continueMessage; string ID = "Janet Newman"; myLib.removeStudent (ID); continueMessage("Janet Newman has been removed."); //--------------------------------------------------------------------------- myLib.print(); continueMessage("All students are listed!"); return...
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()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT