(Advanced C++) I need help to write an algorithm step for the two-part A&B codes below (Note: I have the code; I just need the algorithms).

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

(Advanced C++) I need help to write an algorithm step for the two-part A&B codes below (Note: I have the code; I just need the algorithms).

 

Part A:

#include <iostream>
#include <algorithm>

using namespace std;

void mysort(int *arr, int n)
{
sort(arr, arr+n); //sort is an inbuilt function
}

void avgscore(int *arr, int n, float *avgval)
{
*avgval=0.0; //initializing value
for (int i=0;i<n;i++)
{
*avgval=*avgval+*(arr+i);
}
*avgval=*avgval/(1.0*n);
}

int main()
{
int n;
cout<<"Enter Number of Test Scores\n";
cin>>n;

int *arr = new int[n];

cout<<"Enter Test Scores:\n";

int i,num;
for (i=0;i<n;i++) //taking inputs
{
cin>>num;
if(num<0)
{
cout<<"No negative numbers!\n";
if (i>0)
{
i--;
}
else
{
i=-1;
}
continue;
}
*(arr+i)=num;
}


mysort(arr,n);
float avgval;
avgscore(arr, n, &avgval);

cout<<"The sorted Array is:\n";
for (i=0;i<n;i++) //displaying sorted array
{
cout<<*(arr+i)<<" ";
}
cout<<endl;
cout<<"The Average Test Score is: "<<avgval<<endl;

return 0;
}

Part B:

#include <iostream>
#include <algorithm>

using namespace std;

typedef struct Student //declaring structure
{
string name;
int score;
}Student;

bool compareTwoScores(Student a, Student b) //Function to compare two structures according to score
{
return a.score < b.score;
}

void mysort(Student *arr, int n)
{
sort(arr, arr+n, compareTwoScores); //sort is an inbuilt function
}

void avgscore(Student *arr, int n, float *avgval)
{
*avgval=0.0; //initializing value
for (int i=0;i<n;i++)
{
*avgval=*avgval+(*(arr+i)).score;
}
*avgval=*avgval/(1.0*n);
}

int main()
{
int n;
cout<<"Enter Number of Test Scores\n";
cin>>n;

Student *arr = new Student[n];

cout<<"Enter Name of Student and Test Scores:\n";

int i,num;
string myname;
for (i=0;i<n;i++) //taking inputs
{
cin>>myname;
cin>>num;
if(num<0)
{
cout<<"No negative numbers!\n";
if (i>0)
{
i--;
}
else
{
i=-1;
}
continue;
}
(*(arr+i)).name=myname;
(*(arr+i)).score=num;
}

mysort(arr,n);

cout<<"The sorted Array is:\n";
for (i=0;i<n;i++) //displaying sorted array
{
cout<<(*(arr+i)).name<<" ";
cout<<(*(arr+i)).score<<"\n";
}
cout<<endl;


float avgval;
avgscore(arr, n, &avgval);
cout<<"The Average Test Score is: "<<avgval<<endl;

return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education