The program needs to be written in C. I made an attempt on the problem but not s
ID: 3825226 • Letter: T
Question
The program needs to be written in C. I made an attempt on the problem but not sure if it correct. Hopefully is gives a good start on writing the program
- 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: ------ Not sure if it is right at all.
#include <stdio.h>
int main()
{
FILE *filein *fileout
int maxnumber, average, minnumber, number1, number2, number 3, number 4, sum, average, high interger, low interger;
filein = open("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
{
fscanf(filein, "%d", &number1);
fscanf(filein, "%d", &number2);
fscanf(filein, "%d", &number3);
fscanf(filein, "%d", &number4);
sum = number1 + number2 + number 3 + number4;
printf(" the sum of the numbers %d , %d, %d and %d, is %d", number1, number2, number3, number4, m, sum);
........................
// writing result into a file?
//writing a max number?
/* I just wrote a small part of program for the sum (not sure if it is correct) , I need the average, the highest number, the lowest number 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.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
Input file should be in same directory along with program file
/*-------------------------------------------------*/
/* */
/* This program reads values from a data file and */
/* calls a function to determine the maximum value */
/* with a function. */
#include<iostream> //Required for cin, cerr.
#include<fstream> //Required for ifstream.
#include<string> //Required for string.
using namespace std;
// Define function prototypes.
double maxval (const double x[], int n);
double minval (const double x[], int n);
double sum (const double x[], int n);
int main()
{
// Declare objects.
const int N = 100;
int npts=0;
double y[N], temp;
string filename;
ifstream lab;
// Prompt user for file name and open data file.
cout << "Enter the name of the data file:";
cin >> filename;
lab.open(filename.c_str());
if(lab.fail())
{
cerr << "Error opening input file ";
exit(1);
}
// Read a data value from the file.
lab >> temp;
// While there is room in the array and
// and end of file was not encountered,
// assign the value to the array and
// input the next value.
while (npts < N && !lab.eof() )
{
y[npts] = temp; // Assign data value to array.
++npts; // Increment npts.
lab >> temp; // Input next value
}
// Find and print the maximum value.
cout << "Maximum value: " << maxval(y,npts) << endl;
// Find and print the maximum value.
cout << "Minimum value: " << minval(y,npts) << endl;
double total = sum(y, npts);
cout<<"Total value : "<<total<<endl;
double average = total/npts;
cout<<"Average value : "<<average<<endl;
// Close file and exit program.
lab.close();
return 0;
}
/*-----------------------------------------------------*/
/* This function returns the maximum */
/* value in the array x with n elements. */
double maxval (const double x[], int n)
{
// Declare local objects.
double maxVal;
// Determine maximum value in the array.
maxVal = x[0];
for (int k=1; k<n; ++k)
{
if (x[k] > maxVal)
maxVal = x[k];
}
// Return maximum value. /
return maxVal;
}
/*-----------------------------------------------------*/
/*-----------------------------------------------------*/
/* This function returns the minimum */
/* value in the array x with n elements. */
double minval (const double x[], int n)
{
// Declare local objects.
double minVal;
// Determine minimum value in the array.
minVal = x[0];
for (int k=1; k<n; ++k)
{
if (x[k] < minVal)
minVal = x[k];
}
// Return minimum value. /
return minVal;
}
/*-----------------------------------------------------*/
/*-----------------------------------------------------*/
/* This function returns the sum of */
/* value in the array x with n elements. */
double sum(const double x[], int n)
{
// Declare local objects.
double total;
total = x[0];
for (int k=1; k<n; ++k)
{
total = total + x[k];
}
// Return total value. /
return total;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.