Write a C++ program that reads a list of double values from a file into an array
ID: 3846707 • Letter: W
Question
Write a C++ program that reads a list of double values from a file into an array of doubles. Ask the user to input the filename, and output the following: All the numbers, five numbers per line formatted to two decimal places using a field width of ten. Your values must be neatly displayed. See sample below. The smallest value The largest value The count and sum of all the numbers Frequency analysis of the numbers You must implement the following functions: A function that reads the data from a file and finds the number of values read. void get_numbers (double numbers[], int & count); A function that prints the numbers void print_numbers (double numbers[], int count); A function that returns the smallest value double get_smallest (double numbers[], int count); A function that returns the largest value double get_largest (double numbers[], int count); A function that returns the sum of all the values double get_total (double numbers[], int count); A function that finds the following: The number of values between 0 and 100 The number of values between 100 and 500 The number of values greater than 500 A function to display the report Your main program should only contain variable declarations and function calls.Explanation / Answer
c++ code:
#include <bits/stdc++.h>
using namespace std;
double numbers[1000];
int glob_n;
void get_numbers(double *numbers, string filename)
{
string line;
int count = 1;
ifstream myfile (filename.c_str());
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
for (int i = 0; i < tokens.size(); ++i)
{
numbers[count-1] = atof(tokens[i].c_str());
// printf("%.2f ",numbers[count-1]);
if(count%5 == 0)
{
// cout << endl;
}
count++;
}
}
glob_n = count-1;
cout << endl << "We found " << count - 1<< " values" << endl;
myfile.close();
}
else
{
cout << "Unable to open file" << endl;
exit(1);
}
}
void print_numbers(double *numbers, int count)
{
for (int i = 0; i < count; ++i)
{
printf("%.2f ",numbers[i]);
if((i+1)%5 == 0)
{
cout << endl;
}
}
}
void get_smallest(double *numbers, int count)
{
double min = numbers[0];
for (int i = 0; i < count; ++i)
{
if(min > numbers[i])
{
min = numbers[i];
}
}
cout << "Minimum number found is " << min << endl;
}
void get_largest(double *numbers, int count)
{
double max = numbers[0];
for (int i = 0; i < count; ++i)
{
if(max < numbers[i])
{
max = numbers[i];
}
}
cout << "Maximum number found is " << max << endl;
}
void get_sum(double *numbers, int count)
{
double s = 0;
for (int i = 0; i < count; ++i)
{
s = s + numbers[i];
}
cout << "sum of numbers is " << s << endl;
}
void func(double *numbers, int count)
{
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < count; ++i)
{
if(numbers[i] <= 100 and numbers[i] >=0 )
{
a++;
}
if(numbers[i] >100 and numbers[i] <= 500)
{
b++;
}
if(numbers[i] > 500)
{
c++;
}
}
cout << "Numbers between 0 to 100 " << a << endl;
cout << "Numbers between 100 to 500 " << b << endl;
cout << "Numbers more than 500 " << c << endl;
}
int main()
{
string fname;
cout << "Enter the filename!" << endl;
cin >> fname;
get_numbers(numbers,fname);
print_numbers(numbers,glob_n);
get_smallest(numbers,glob_n);
get_largest(numbers,glob_n);
get_sum(numbers,glob_n);
func(numbers,glob_n);
return 0;
}
input.txt
12.34342
23.4542344
534.56
112.342644
23.45
34.56
12.34
23.45
34.56
34.56
Sample Output:
Enter the filename!
record.txt
We found 10 values
12.34 23.45 534.56 112.34 23.45
34.56 12.34 23.45 34.56 34.56
Minimum number found is 12.34
Maximum number found is 534.56
sum of numbers is 845.62
Numbers between 0 to 100 8
Numbers between 100 to 500 1
Numbers more than 500 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.