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

Design a C++ program using functions (as described below) that will Interactivel

ID: 3803482 • Letter: D

Question

Design 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 (see get_data function in lecture notes) 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 your name, lecture and lab section #s and exercise # 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 To receive full credit for this exercise, 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

Explanation / Answer

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
using namespace std;

int read(int i)
{
return i;
}

char read(char c)
{
return c;
}

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 readFile(double arr[])
{
cout << "Enter inout file name: " ;
char filename[100];
cin >> filename;
ifstream infile(filename);
int i = 0;
double value;
while(infile >> value)
{
arr[i++] = value;
}
infile.close();
return i;
}

double average(double arr[], int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
{
sum+= arr[i];
}
return sum/n;
}

double median(double arr[], int n)
{
if (n % 2 == 0)
{
return (arr[n/2] + arr[n/2 +1])/2;
}
return arr[n/2];
}

void write_output(double arr[], int n)
{
cout << "Enter output file: ";
char output[100];
cin >> output;
ofstream outfile(output);
outfile << "your name, lecture, etc etc" << endl;
for(int i = 0; i < n; i++)
{
outfile << "Element: " << (i+1) << " " << fixed << setprecision(1) << arr[i] <<endl;
}
outfile << "Average: " << fixed << setprecision(3) << average(arr, n) << endl;
outfile << "Median: " << fixed << setprecision(3) << median(arr, n) << endl;
outfile.close();
}

int main()
{
double numbers[25];
int size = readFile(numbers);
bubblesort(numbers, size);

write_output(numbers, size);
return 0;
}

// Sample run
$ cat tesst.txt

12.3
65.3
96 85 69
253

$ g++ test.cpp
$ ./a.out
Enter inout file name: tesst.txt
Enter output file: output.txt
$ cat output.txt
your name, lecture, etc etc
Element: 1 12.3
Element: 2 65.3
Element: 3 69.0
Element: 4 85.0
Element: 5 96.0
Element: 6 253.0
Average: 96.767
Median: 90.500