6. Here are 4 files: numbers1.txt , numbers2.txt and numbers3.txt and numbers4.t
ID: 3818370 • Letter: 6
Question
6. Here are 4 files: numbers1.txt , numbers2.txt and numbers3.txt and numbers4.txt . The first two contains a "header" line telling you how many data items follow. (The first one is very small, so you can easily verify you are getting the right results; the second is larger, too large to verify by hand.) The second pair are like the first two, except they are just a list of numbers (no "header", i.e., no count at the top).
a.Write a program that can calculate the average, min and max of the numbers in the first 2 files. It should read the first value (# of points), allocate an array the correct size, read the data into that array, then analyze the array and print the count, average, min & max of the array.
a.Use a function to read the data, a second function to find the average, and a third function to find the min & max. Read the count and allocate the array itself in the (...) routine (...) routine will have to pass back both the count and the pointer after it allocates the array.
b.I recommend that you print out the first few elements and the last few elements of the data file when you read them in to make sure you get all the values. Some print statements in the various routines would also be helpful while writing/testing to make sure things are working properly.
c.Make sure you delete the dynamic array properly and set your pointer back to NULL.
Modify your program to work with an array of filenames, and read and print the statistics for each file in the list. You can use an array of strings for the filenames:
string filenames[] = {"numbers1.txt", "numbers2.txt"};
c.Write a modified version of the program from part a. that can work with the files numbers3.txt and The only difference is that the file doesn't tell you the size upfront. Your program will have to read the file twice – once to count how many values there are, and then (after allocating an array), read in the data and get the min, max and average.
a.Use a function (filename) that reads the file and counts the number of values.
b.Note: if you're not careful, you can end up reading and counting the last element twice! Make sure you don't do that...remember, is your friend here...
You can use an array of strings for the filenames:
string filenames[] = {"numbers3.txt", "numbers4.txt"};
numbers1.txt
5
numbers2.txt
numbers3.txt
numbers4.txt
Name your programs read_nums.cpp (part b) and read_nums_nh.cpp (part c)
Explanation / Answer
PROGRAM CODE:
read_nums.cpp
/*
* read_nums.cpp
*
* Created on: 16-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
double calculateAvg(double data[], int size)
{
double sum = 0.0;
for(int i=0; i<size; i++)
{
sum += data[i];
}
return sum/size;
}
void find_Min_Max(double data[], int size, double &min, double &max)
{
min = data[0];
max = data[0];
for(int i=0; i<size; i++)
{
if(min>data[i])
min = data[i];
if(max<data[i])
max = data[i];
}
}
void readFile(ifstream &in, double data[], int size)
{
string line;
int count=0;
while(getline(in, line))
{
istringstream iss(line);
double number;
while(iss>>number)
{
//to print the numbers
//cout<<number<<endl;
data[count++] = number;
}
if(count == size)
break;
}
}
int main()
{
string filename[] = {"numbers1.txt", "numbers2.txt"};
for(int i=0; i<2; i++)
{
int size;
double max, min;
ifstream in(filename[i]);
string line;
getline(in, line);
size = atoi(line.c_str());
double *numbers = new double[size];
readFile(in, numbers, size);
find_Min_Max(numbers, size, min, max);
cout<<"Total number of element in "<<filename[i]<<" is "<<size<<endl;
cout<<fixed<<setprecision(4)<<"Average is "<<calculateAvg(numbers, size)<<endl;
cout<<"Minimum value is "<<min<<endl;
cout<<"maximum value is "<<max<<endl<<endl;
numbers = NULL;
}
return 0;
}
OUTPUT:
Total number of element in numbers1.txt is 5
Average is 0.5987
Minimum value is 0.1227
maximum value is 0.9209
Total number of element in numbers2.txt is 100
Average is 0.4844
Minimum value is 0.0014
maximum value is 0.9963
read_nums_nh.cpp
/*
* read_nums.cpp
*
* Created on: 16-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
double calculateAvg(double data[], int size)
{
double sum = 0.0;
for(int i=0; i<size; i++)
{
sum += data[i];
}
return sum/size;
}
void find_Min_Max(double data[], int size, double &min, double &max)
{
min = data[0];
max = data[0];
for(int i=0; i<size; i++)
{
if(min>data[i])
min = data[i];
if(max<data[i])
max = data[i];
}
}
void count(string filename, int &size)
{
ifstream in(filename);
string line;
while(getline(in, line))
{
istringstream iss(line);
double number;
while(iss>>number)
{
size++;
}
}
}
void readFile(string filename, double data[], int size)
{
ifstream in(filename);
string line;
int count=0;
while(getline(in, line))
{
istringstream iss(line);
double number;
while(iss>>number)
{
//to print the numbers
//cout<<number<<endl;
data[count++] = number;
}
if(count == size)
break;
}
}
int main()
{
string filename[] = {"numbers3.txt", "numbers4.txt"};
for(int i=0; i<2; i++)
{
int size;
double max, min;
count(filename[i], size);
double *numbers = new double[size];
readFile(filename[i], numbers, size);
find_Min_Max(numbers, size, min, max);
cout<<"Total number of element in "<<filename[i]<<" is "<<size<<endl;
cout<<fixed<<setprecision(4)<<"Average is "<<calculateAvg(numbers, size)<<endl;
cout<<"Minimum value is "<<min<<endl;
cout<<"maximum value is "<<max<<endl<<endl;
numbers = NULL;
}
return 0;
}
OUTPUT:
Total number of element in numbers3.txt is 10
Average is 0.4887
Minimum value is 0.0182
maximum value is 0.9736
Total number of element in numbers4.txt is 324
Average is 0.5134
Minimum value is 0.0000
maximum value is 0.9924
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.