- It needs to open a file for reading (txt file for example) - It has to be able
ID: 3825250 • Letter: #
Question
- It needs to open a file for reading (txt file for example)
- It has to be able to read the lines of the file (like array of structs)
- Has to calculate the sum of all the numbers, average, highest integer and lowest integer.
-it has to print the report onto the screen (command prompt)
lastly, it needs to open the file so it can write it onto another file.
Here is what I have so far: Its just an example on how to go about it. Please start with what I have so far on how to open the file and read the file.------ Not sure if it is right at all.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *filein *fileout
int maxvalue, minvalue, average, sum, // probably a few more that are missing, you can add whatever.
filein = fopen("Numbers.txt", "r");
/* to write the result into another file, would it be this? I want to make the report print into another text file or make the output data show on another file. >>>> file = fopen("numbersoutput.txt", "w"); <<<
if (filein == NULL)
{
printf(" File can't be opened for reading. ");
}
else
{
/* I just wrote a small part of program for the sum (not sure if it is correct) , I need the average, the highest value, the lowest value to be posted into the command prompt using fprintf, fscanf if that's neccesary. */
-- Please finish the program in C language. It needs to open and read a file, do calculations within the file, show output in command prompt if possible, and write the output/ results into another file. >> filein = fopen("Numbers.txt", "r"); opens and reads the file
I know file = fopen("numbersoutput.txt", "w") would write the file? It has to be written after it does the calculations and puts them into another file.
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
/*Assume the Numbers.txt file contains 5 lines and each line contains three numbers */
int main()
{
FILE *filein, *fileout
int data[15], highest, lowest, sum, i;
int a,b,c;
float avg;
filein = fopen("Numbers.txt", "r");
if (filein == NULL){
printf("File can't be opened for reading ");
return 0;
}
i = 0;
for (
while (fscanf(filein, "%d %d %d", &a, &b, &c) != EOF){
data[i] = a;
data[i+1] = b;
data[i+2] = c;
i = i + 3;
}
sum = 0;
for (i = 0; i<15; i++)
{
sum = sum + data[i];
}
avg = sum/15;
highest = data[0];
for (i = 0; i<15; i++)
{
if (data[i] > highest)
{
highest = data[i];
}
}
highest = data[0];
for (i = 0; i<15; i++)
{
if (data[i] > highest)
{
highest = data[i];
}
}
lowest = data[0];
for (i = 0; i<15; i++)
{
if (data[i] < lowest)
{
lowest = data[i];
}
}
printf("Sum : %d ", sum);
printf("Avg : %f ", avg);
printf("Highest : %d ", highest);
printf("Lowest : %d ", lowest);
fileout = fopen("numbersoutput.txt", "w");
fprintf(fileout, "Sum : %d ", sum);
printf(fileout, "Avg : %f ", avg);
printf(fileout, "Highest : %d ", highest);
printf(fileout, "Lowest : %d ", lowest);
fclose(fileout);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.