Write a program in C language that can open a file to read. Please add comments
ID: 3826165 • Letter: W
Question
Write a program in C language that can open a file to read. Please add comments inbetween functions to show what you did, thumbs up for that.
- it needs to read lines of a file, ( example is that the file can have 3 lines with 4 numbers each )
- needs to make a struct and put it into an array. ( use typedef struct if neccessary) -- use scanf and printf.
calculate the average, median, lowest number, highest number, sum, mode.
- print that data into the screen ( command prompt)
- lastly, print and write the data onto another file.
/* I wrote a small start as to how the program should go, Not sure if typedef should be added in there or not for array.
please finish the code */
int main () {
FILE *fileinput *fileoutput
int numbers[SIZE]
int minvalue, maxvalue, sum, average, mode, median, count = 0; // not sure what else to add here or if this is correct
float average;
// this opens the file to read?
fileinput = fopen("sampleinput.txt", "r");
if (fileinput == NULL) {
printf(" File can't be read. ");
} else {
while (count < SIZE ) ???? // not sure if this is needed or not.
fscanf(fileinput, "%d", &numbers[count++]); // once again not sure.
fclose(fileinput); // closes the file?
sum = getsum(numbers);
average = (float) sum/ count
// not sure about rest. please continue this if there is anything wrong or missing.
...................
// opening the output file for writing.
fileoutput = fopen ("sampleoutput.txt "w")
if (fileoutput == NULL) {
printf(" File can't be opened to write results. ");
} else {
// writing results if the file opened..
fprintf(fileoutput, "Max Integer :%d ", maxvalue);
fprintf(fileoutput, "Min Integer :%d ", minvalue);
fprintf(fileoutput, "Sum :%d ", sum);
fprintf(fileoutput, "Average :%.2f ", average);
fprint(fileoutput, "Mode : %d ", Mode);
fprint(fileoutput, "median : %d ", median);
}
--------------------------------Add other functions for calculations here if that's neccessary.
int getMax(int arr[]) {
int i, maxvalue = arr[0];
for (i = 1; i < SIZE; i++) {
if (arr[i] > maxNumber) {
maxvalue = arr[i];
---- use printf to print data into new file. and please show a picture of it working if possible
}
}
return maxvalue;
Explanation / Answer
// Thumbsup if you like the solution //
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
// Max size could be 12
#define SIZE 12
// decaling all functions //
void funcsort(int arr[], int num);
int getMedian(int arr[], int count);
int getMode(int arr[], int count);
int getMin(int arr[], int count);
int getMax(int arr[], int count);
int getSum(int arr[], int count);
int main()
{
// using file handlers
FILE *fileinput, *fileoutput;
int numbers[SIZE];
int minvalue, maxvalue, sum, mode, median, count = 0; // not sure what else to add here or if this is correct
float average;
// this opens the file to read?
// open file to read, in case of failure return
fileinput = fopen("sampleinput.txt", "r");
if (fileinput == NULL)
{
printf(" File can't be read. ");
return 0;
}
else
{
while (count < SIZE) // since maximum values could be equal to size
fscanf(fileinput, "%d", &numbers[count++]);
sum = getSum(numbers, count);
average = (float)sum / count;
minvalue = getMin(numbers, count);
maxvalue = getMax(numbers, count);
mode = getMode(numbers, count);
median = getMedian(numbers, count);
// open file for writing the outputs to a location, open in write mode //
fileoutput = fopen("E:\sampleoutput.txt", "w");
if (fileoutput == NULL) {
printf(" File can't be opened to write results. ");
return 0;
}
else
{
// writing results if the file opened..
fprintf(fileoutput, "Max Integer :%d ", maxvalue);
fprintf(fileoutput, "Min Integer :%d ", minvalue);
fprintf(fileoutput, "Sum :%d ", sum);
fprintf(fileoutput, "Average :%.2f ", average);
fprintf(fileoutput, "Mode : %d ", mode);
fprintf(fileoutput, "median : %d ", median);
printf("All data written to file E:\sampleoutput.txt... ");
}
// close both opened files here, since all operations are done //
fclose(fileinput);
fclose(fileoutput);
}
}
void funcsort(int arr[], int num)
{
int i, j, temp;
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// middle value, so array should be sorted to get this value
int getMedian(int arr[],int count)
{
funcsort(arr, count);
// printf("New Sorted Array: ");
// for (int i = 0; i < count; i++)
// printf("%d ", arr[i]);
if (count%2==0)
return arr[(count+1) / 2];
else
return arr[count / 2];
}
// number repeated more often
int getMode(int array[],int size)
{
funcsort(array, size);
int count = 1;
int countMode = 1;
int number = array[0];
int mode = number;
for (int i = 1; i < size; i++)
{
if (array[i] == number)
{
count++;
if (count > countMode)
{
mode = number;
countMode = count;
}
}
else
{
count = 1;
number = array[i];
//mode = number;
}
}
return mode;
}
int getMin(int arr[], int count)
{
int i, minvalue = arr[0];
for (i = 1 ; i < count; i++)
{
if (arr[i] < minvalue)
{
minvalue = arr[i];
}
}
return minvalue;
}
int getMax(int arr[], int count)
{
int i, maxvalue = arr[0];
for (i = 1; i < count; i++)
{
if (arr[i] > maxvalue)
{
maxvalue = arr[i];
}
}
return maxvalue;
}
int getSum(int arr[], int count)
{
int sum = 0;
for (int i = 1; i < count; i++)
{
sum += arr[i];
}
return sum;
}
// output
Max Integer :7
Min Integer :1
Sum :47
Average :3.92
Mode : 5
median : 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.