Write a C program that reads a file of integers. Finds the smallest, largest, su
ID: 3768246 • Letter: W
Question
Write a C program that reads a file of integers. Finds the smallest, largest, sum, average (mean) and median. It also counts how many times each number is seen.
You are given a start program with code to sort an array of integers and print an array of integers.
Functions:
int main ( int argc, char * argv[] )
declare variables
fillArray(
getStats(
getModes(
sort(
writeStats
int fillArray( int array[1000] )
receives an array of integers
opens file in.txt
reads the integers from the file storing each in the array
closes the file
returns a count of how many ints in the array. If count is 1 then array[0] is only element with a valid integer
void getStats(int array[], int count, int &min, int &max, int &sum)
receives array and count
returns:
the smallest in min
largest in max
total in sum
void getModes( int array[], int count, int counts[100])
receives array count integers, initialize all elements to 0
loop thru the array and add one to the proper count counts[ array[i] ] ++;
returns the counts array with count of number 0 in counts[0], 1 in counts[1] etc.
void writeStats(int counts[100], int count, int min, int max, int sum, int median)
opens file out.txt
Writes to file with labels:
count of how many integers read
min, max, sum, average and median
Label then the numbers 0-100 followed by count of how many times they occurred 5 per line
0 - 3, 1 – 2, 2 – 5, 3 – 0, 4 – 6, 5 – 22
6 – 0, and so on
close file
Given to start from this following code:
#include <stdio.h>
void swap(int a [], int p1, int p2) {
int temp = a[p1]; a[p1] = a[p2]; a[p2] = temp;
}
void sort(int array[], int count) { //easy dumb bubble sort ascending order
int c, d;
for (c = 0; c < (count - 1); c++)
{
for (d = 0; d < count - c - 1; d++)
{
if (array[d] > array[d + 1]) /* For decreasing order use < */
{
swap(array, d, d + 1);
}
} // end d loop
} // end c loop
} // end sort
void printArray(int array[], int count){
int i=0;
printf("enter printArray count %d ", count);
while (i < count) {
printf("%d%c", array[i++],
((i+1)%10 == 0)?' ':' ');
}
}
int main(int argc, char * argv[]) {
int count = 0;
int array[20];
for (count = 0; count < 20; count++) array[count] = 20 - count;
printArray(array, count);
sort(array, count);
printArray(array, count);
getchar();
return 0;
}
Explanation / Answer
#include 02 #include 03 using namespace std; 04 05 int main() 06 { 07 ifstream inputFile; //file stream object 08 int choice; // menu choice 09 int number; // to hold a value from file 10 int total; //to hold a value from file 11 int large; // to hold largest value from file 12 int small; // to hold smallest value from file 13 int sum; // to hold sum of the values from file 14 int average; // to hold the average values from file 15 int quantity; // to hold the number of values entered 16 17 inputFile.open("TopicDin.txt"); // Open the file 18 19 do 20 { 21 //display the menu and get a choice 22 coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.