Question

In: Computer Science

In C++ Complete the following code without the use of auto. Write a function, getAverages, that...

In C++

Complete the following code without the use of auto.

  1. Write a function, getAverages, that will prompt the user for a series of exam averages for an unspecified number of students in a class. The user should enter a negative exam average to indicate the end of data. Exam averages should be stored in the vector indicated by the reference parameter.
  2. Write a function, storeAverages, that will accept two parameters, a vector containing exam averages and a string containing the name of a file. The function should store the averages to the file specified in the filename parameter, one average per line. If a file with that filename already exists, its contents should be overwritten. The function should not be able to modify the averages stored in the vector. This function should use the range-based for loop.
  3. Write a function, curveAverages, that will accept two parameters, a vector containing exam averages to be curved and a floating-point number which should be added to each average. The function must be able to modify the averages in the vector. The function should use a regular for loop and the at() member function.
  4. Write a function, calculateClassAverage, that will accept a single parameter, the vector containing the students' exam averages. The function should compute and return the average of the class' exams. The function should not be able to modify the averages stored in the vector.
  5. Include code in the main that will allow the user to specify the exam averages, the number of points to curve each average and a file name for storing the averages. The code should curve the averages, calculate the class average, which should then be displayed to the screen, and then store the exam averages in the file specified by the user.

Solutions

Expert Solution

code:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void getAverages(vector<float>& v){
cout<<"Enter student marks average"<<endl;
while(true){ // take input until - value
float a;
cin>>a;
if(a<0)
break;
v.push_back(a); // pus data to vector
}
}

void storeAverages(const vector<float>& v, string filename){
ofstream fout;
string line;
fout.open(filename.c_str());
for(int i=0; i<v.size(); i++)
fout << v[i] << endl; // writing data to file in each new line
fout.close();
cout<<"file writen"<<endl;
}
void curveAverages(vector<float>& v, float number){
for(int i=0; i<v.size(); i++){
v.at(i)=v[i] + number; // user at and assing the value
}
}

float calculateClassAverage(const vector<float>& v){
float sum = 0;
for(int i=0; i<v.size(); i++){
sum += v[i]; // calculating average
}

return sum/v.size();
}
int main(){
vector<float> avg;
getAverages(avg); // calling methore
cout<<endl<<"Enter average data in vector is: ";
for(int i=0; i<avg.size(); i++){
cout<<avg[i]<<" ";
}
cout<<endl<<"Enter filename: ";
string filename;
cin>>filename;
storeAverages(avg,filename);// calling methore
cout<<endl<<"Enter average constant curve: ";
int curve;
cin>>curve;
curveAverages(avg,curve);// calling methore
cout<<endl<<"vector after curve: ";
for(int i=0; i<avg.size(); i++){
cout<<avg[i]<<" ";
}
float avge = calculateClassAverage(avg);
cout<<endl<<"Average after cal: ";
cout<<avge<<endl;

}

for any help ask in the comment !


Related Solutions

For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write a program to do the following: Read in two values into variables X   and y from the keyboard (they may be decimal values like 2.3). Read in a 3rd value (which is really small < 1.0 ) into variable E. Using a loop find out the value of N in the below expression. i.e. Each iteration of the loop reduced the value of Y...
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT