Here is the problem I\'m trying to solve Write a program the reads a file contai
ID: 3534060 • Letter: H
Question
Here is the problem I'm trying to solve
Write a program the reads a file containg two columsn of floating-point numbers. Prompt the user for the file name. Print the average of each column.
I'm a beginner program so if what I have written is nowhere close, please let me know. As far as what I have written, my code reads both columns from (numbers.txt) but it displays them in hexadecimal (which I'm guessing would be pointers for each number) and the averages are both zero. I declard each column as an array of 19 numbers, displayed each column, and then finally tried to calculate the average for each column. You'll notice that I didn't prompt the user for the file name because I was confused on how to do that. Any help on how to fix my code would be greatly appreciated.
// P8.4
#include <iostream>
using namespace std;
#include <fstream>
#include <iostream>
int main()
{
double input_1[19] = {};
double input_2[19] = {};
//Open file
ifstream inFile;
inFile.open("numbers.txt");
while(!inFile.eof())
{
//Get input
inFile >> input_1[19] >> input_2[19];
//Print input
cout << input_1 << " " << input_2 << " "<<endl;
}
double sum_1 = 0;
double sum_2 = 0;
double average_1;
double average_2;
for (int i=0; i<19; i++)
{
sum_1 = sum_1 + input_1[i];
sum_2 = sum_2 + input_2[i];
}
average_1 = sum_1 / 19;
average_2 = sum_2 / 19;
cout<<average_1<<average_2<<endl;
//Close file
inFile.close();
system("pause");
}
Explanation / Answer
Promting the user can be done by:
string fileName;
scanf("Enter File name %d",&fileName);
inFile.open(fileName);
The error is here: rewrite below code
//Print input
cout << input_1[19] << " " << input_2[19] << " "<<endl;