In: Computer Science
1)Reading input from the keyboard:
#include <iostream>
#include <array>
using namespace std;
int buildQuizArray(int quizArray[]) {
int count=0;
int temp;
for(int i=0;i<12;i++){
cout<<"Enter your score for quiz "<<(i+1)<<" (-1 to quit): ";
cin>>temp;
if(temp==-1){
break;
}
else{
quizArray[i]=temp;
count+=1;
}
}
return count;
}
void printQuizArray(string title,int quizArray[],int numberOfQuizzes){
cout<<title<<"\n----------------";
for(int i=0;i<numberOfQuizzes;i++){
cout<<"\nQuiz "<<(i+1)<<":"<<quizArray[i]<<"/10";
}
}
void sortArray(int array[],int numberOfQuizzes){
int t;
for(int i=0;i<numberOfQuizzes;i++){
for(int j=i+1;j<numberOfQuizzes;j++){
if(array[j]<array[i]){
t=array[i];
array[i]=array[j];
array[j]=t;
}
}
}
}
void copyArray(int destination[],int source[],int numberOfValues){
for(int i=0;i<numberOfValues;i++){
destination[i]=source[i];
}
}
double calcQuizAverage( int quizArray[], int numberOfQuizzes ){
double avg;
if(numberOfQuizzes<=2){
avg=10*numberOfQuizzes*100;
return avg;
}
else{
int array[12];
copyArray(array,quizArray,numberOfQuizzes);
sortArray(array,numberOfQuizzes);
int sum=0;
for(int i=0;i<numberOfQuizzes;i++){
sum+=quizArray[i];
}
avg=sum-(array[0]+array[1]);
avg/=10*(numberOfQuizzes-2);
avg*=100;
return avg;
}
}
int main()
{
const int n=12;
int quizArray[n];
int numberOfQuizzes=0;
numberOfQuizzes=buildQuizArray(quizArray);
string title="\nScores";
cout<<"Your quiz average is: "<<calcQuizAverage(quizArray,numberOfQuizzes);
printQuizArray(title,quizArray,numberOfQuizzes);
return 0;
}
OUTPUT:
2)Reading input from file:
#include <iostream>
#include <array>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void printQuizArray(string title,int quizArray[],int numberOfQuizzes){
cout<<title<<"\n----------------";
for(int i=0;i<numberOfQuizzes;i++){
cout<<"\nQuiz "<<(i+1)<<":"<<quizArray[i]<<"/10";
}
}
void sortArray(int array[],int numberOfQuizzes){
int t;
for(int i=0;i<numberOfQuizzes;i++){
for(int j=i+1;j<numberOfQuizzes;j++){
if(array[j]<array[i]){
t=array[i];
array[i]=array[j];
array[j]=t;
}
}
}
}
void copyArray(int destination[],int source[],int numberOfValues){
for(int i=0;i<numberOfValues;i++){
destination[i]=source[i];
}
}
double calcQuizAverage( int quizArray[], int numberOfQuizzes ){
double avg;
if(numberOfQuizzes<=2){
avg=10*numberOfQuizzes*100;
return avg;
}
else{
int array[12];
copyArray(array,quizArray,numberOfQuizzes);
sortArray(array,numberOfQuizzes);
int sum=0;
for(int i=0;i<numberOfQuizzes;i++){
sum+=quizArray[i];
}
avg=sum-(array[0]+array[1]);
avg/=10*(numberOfQuizzes-2);
avg*=100;
return avg;
}
}
int main()
{
const int n=12;
string t[n];
int quizArray[n];
ifstream infile;
infile.open ("quizscores.txt");
if (infile.fail() ) {
cout << "The quizscores.txt input file did not open";
exit(-1);
}
int i=0;
while(!infile.eof()){
getline(infile, t[i]);
i+=1;
}
for(int j=0;j<n;j++){
stringstream s(t[j]);
s>>quizArray[j];
}
int numberOfQuizzes=i;
string title="\nScores";
cout<<"Your quiz average is: "<<calcQuizAverage(quizArray,numberOfQuizzes);
printQuizArray(title,quizArray,numberOfQuizzes);
return 0;
}
quizscores.txt file:
5
6
7
9
3
1
4
OUTPUT:
Thank you! if you have any queries post it below in the comment
section I will try my best to resolve your queries and I will add
it to my answer if required. Please give upvote if you like
it.