How do I write a C program like the one stated below? Problem: Write a program t
ID: 3759959 • Letter: H
Question
How do I write a C program like the one stated below?
Problem:
Write a program that creates an array of integers from numbers read from an input file (input.txt) and then prints out the following to an output file called output.txt:
The original array
The maximum value in the array
The minimum value in the array
The values in the array sorted from the smallest to the largest. (do not google this write your
own code)
The total of all the values in the array
Your program should include void functions that perform the actions needed to obtain 1-5 above. Instead of returning the required output, your functions should use pointers to return the values.
Restrictions:
Your program should include a header and other comments.
You may assume that the input file is valid
The data in the file ends with a 0
You may assume that the file has no more than 20 integers
When run, your program should output the text shown in the sample output although the
numbers may be different.
The sample runs are just that - samples! They show how your program should behave. Your
program should work correctly, no matter what values are in the input file – even values different from those in the samples.
Here are the sample outputs:
Sample Run 1 Input.txt
23 45 12 68 44 31 46
0
Output.txt
The original array is: 23, 45, 12, 68, 44, 31, 46
The maximum value is 68
The minimum value is 12
The sorted array is: 12, 23, 31, 44, 45, 46, 68
The total of the values in the array is: 269
Sample Run 2 Input.txt
24
73
55
62
9
0
Output.txt
The original array is: 24, 73, 55, 62, 9
The maximum value is 73
The minimum value is 9
The sorted array is: 9, 24, 55, 62, 73
The total of the values in the array is: 223
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int readFile(char* fileName, int numbers[]) {
FILE *inputFile;
inputFile = fopen(fileName, "r");
//printf("reading file ");
int line_size = 254; //change this size accordig to your need
char str[line_size];
if (inputFile == NULL) {
printf("File not found ");
return -1;
}
//char** curArr = NULL;
int count = 0;
int num = 0;
char *token;
const char s[2] = " ";
while( fgets (str, line_size, inputFile) != NULL)
{
// printf("%s ",str);
/* get the first token */
token = strtok(str, s);
//printf("%s -first element ",token);
/* walk through other tokens */
while( token != NULL)
{
num = atoi(token);
if(num == 0)
break;
*numbers++ = num;
count++;
token = strtok(NULL, s);
//printf("####%s - ",token);
}
if(num == 0)
break;
}
fclose(inputFile);
return count;
}
void minMax(int *numbers, int count, int *min, int *max, int *total) {
int i = 0;
//sortByLastName(numbers, count);
printf("printing array - %d ", count);
*max = numbers[0];
*min = numbers[0];
int sum = 0;
for(i = 1; i < count; i++) {
if(numbers[i] > *max)
*max = numbers[i];
if(numbers[i] < *min)
*min = numbers[i];
sum += numbers[i];
}
*total = sum;
}
void sort(int *numbers, int count) {
int i,j;
int temp;
for (i = 0; i < count; i++) {
for (j = 0; j < count - 1; j++) {
if ((numbers[j] > numbers[j + 1]) ) {
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j+1] = temp;
}
}
}
}
int main() {
//In question given: You may assume that the file has no more than 20 integers
int *numbers, input[50];
//change file location here
char *filename ="D: avi\Cheg umbers1.txt";
numbers = &input[0];
int count = readFile(filename, numbers);
int i = 0;
//sortByLastName(numbers, count);
printf("The original array is: ");
for(i = 0; i < count; i++) {
printf("%d ",numbers[i]);
}
int min = 0, max = 0, total =0;
minMax(numbers, count, &min, &max, &total);
printf(" The maximum value is %d ", max );
printf("TThe minimum value is %d ", min );
printf("The total of the values in the array is:%d ", total );
numbers = &input[0];
sort(numbers, count);
printf("The sorted array is: ");
for(i = 0; i < count; i++) {
printf("%d ",numbers[i]);
}
}
---output--
The original array is: 23 45 12 68 printing array - 4
The maximum value is 68
TThe minimum value is 12
The total of the values in the array is:125
The sorted array is: 12 23 45 68
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.