Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Goals - Problem solving and debugging. -User-defined functions. -C++ strings

ID: 3820881 • Letter: C

Question

C++ Goals

-
Problem solving and debugging.
-User-defined functions.
-C++ strings.
-Arrays.
-Sorting.
-C++ filestreams.

Create a C++ program using functions (as described below) that will interactively prompt the user for and read the name of an input file that contains a maximum of 25 floating point numbers

-open the input file

-read the numbers and store them in an array, counting as they are read

-sort them into ascending order
-interactively prompt the user for the name of an output file and read it

-open the output file

Write the following to the output file:

-a list of the numbers (with 1 digit to the right of the decimal) in ascending order (1 per line) - label the list

-the average of the numbers (with label and 3 digits to the right of the decimal)

-the median of the numbers (with label and 3 digits to the right of the decimal)

-close all files

The program must make use of functions and pass parameters (no global variables. no goto statements)

the input file can only be opened and read ONE time

there must be a function to sort the numbers (bubblesort recommended)

there must be a function to compute the average (void or value-returning)

there must be a function to compute the median (void or value-returning)

any additional functions are optional

an array MUST be used to store the numbers from the file

NOTES:

The median is the "middle" value in a set of sorted values. If there are an even number of values in the set, the median is the average of the 2 "middle" values.

Assumptions about input: (you do not have to test for these conditions)

data file will exist, it will not be empty

maximum number of values in the file will be 25

numbers will be separated by blanks and/or linefeeds

the last line in the data file will be terminated with a linefeed (' ')

Program must be designed to read and write to filestream variables.

Include all header files for library functions used by the program.


When the program compiles and runs correctly, use the mail utility to email a copy of the program file to your lab instructor. Make sure the subject line of your email includes your name, lecture and lab section #s, and the exercise # if you wish to receive full credit.

Sample terminal session:

[keys]$ more data4eleven
13.0 7.0 1.5 216.2
155.3 22.8
[keys]$ g++ exercise11.cpp
[keys]$ ./a.out
Enter name of input file
data4eleven
Enter name of output file
output4eleven
[keys]$ more output4eleven


List of Numbers
1.5
7.0
13.0
22.8
155.3
216.2

Average = 69.300
Median = 17.900

Explanation / Answer

C++ CODE:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <limits>
using namespace std;
void sortArray(float[],int);
float getAverage(float[],int);
float getMedian(float[],int);
int main()
{
string inputfile,outputfile,line,word;
float array[25];
int numCount=0,i;
  
cout << "Enter name of input file"<< endl;
cin>>inputfile;
cout << "Enter name of output file"<< endl;
cin>>outputfile;


ifstream inFile((inputfile+".txt").c_str());
  
if (inFile)
{
  
while (getline( inFile, line ))
{
istringstream iss(line);

while(iss>>array[numCount])
{

numCount++;
}
  
  
}
inFile.close();
}
else cout << "Failed opening "+inputfile+" ";
  
sortArray(array,numCount);
  
ofstream outFile;
outFile.open((outputfile+".txt").c_str());
outFile<<"List of Numbers"<<endl;

for(i=0;i<numCount;i++)
outFile<<fixed<<setprecision(1)<<array[i]<<endl;

outFile<<endl<<endl<<"Average="<<fixed<<setprecision(3)<<getAverage(array,numCount)<<endl;
outFile<<"Median="<<fixed<<setprecision(3)<<getMedian(array,numCount)<<endl;
  
return 0;
}

void sortArray(float array[],int size){//bubble sort
int n=size,i,j;
float temp;
  
for(i=0;i<size;i++){
for(j=0;j<size-i-1;j++){
if(array[j+1]<array[j]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
  
}
}
float getAverage(float array[],int size){
double sum=0;
int i;
for(i=0;i<size;i++){
sum+=array[i];
}

return (sum/size);
}
float getMedian(float sortedArray[],int size){

  
if(size%2==0)//if even number of items
return ((sortedArray[size/2]+sortedArray[(size/2)-1])/2);//size/2-1 becuz array index from 0
else
return (sortedArray[size/2]);
}

NOTE: give filename without extension . eg for input.txt give input

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote