Problem: 1) read an unknown number of positive integers, but 50 or less 2) print
ID: 3635713 • Letter: P
Question
Problem:1) read an unknown number of positive integers, but 50 or less
2) print out some statistics (how many numbers were read, the largest number, the
smallest number, and an average. Note that the average is a whole number.
Except for the total number of errors, invalid data has no effect on anything else.)
3) use a function called process_array to process the array
a) this function prints out the table below (including the lines and table headings)
b) the column Sum of RestEven is computed using a NESTED “FOR” LOOP.
This column is calculated by summing all of the rest of the numbers in the
array which are even, after the number you are currently processing.
For example. suppose there are 10 items in the array, subscripted 0 thru 9.
Suppose that you are on subscript 5. You are then supposed to sum the
values at subscripts 6 thru 9 which are even, since these are the numbers in
the array AFTER the number you are currently processing. (HINTS:
What is the first subscript of this nested “for” loop? It is 6. How do you
build a “for” loop that starts at subscript 6, and ends at the appropriate
place?)
SAMPLE INPUT: 100 49 -26 64 16
SAMPLE OUTPUT:
4 integers were read
There was 1 error
Largest: 100
Smallest: 16
Average: 57
-----------------------------------------------
Seq Number Sum of RestEven
-----------------------------------------------
1 100 80
2 49 80
3 64 16
4 16 0
-----------------------------------------------
This is what I got so far,I'm not able to get my average and smallest to work and my number of integers is wrong also. All of the output is suppose to be under the same function process_array, use only one void function.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
void process_array(int input_array[], int data, ofstream& fout);
int main()
{
char exit;
int input,
total_input= 0,
input_array[50];
ifstream fin;
ofstream fout;
fin.open("Problem.dat");
if (!fin)
{
cout << "Cannot open the input file." << endl << endl;
fout << "Cannot open the input file." << endl << endl;
cout << "Please press any key and enter to end execution of this program . . .";
cin >> exit;
return 1;
}
for (total_input= 0; fin >> input; total_input++)
{
if (input < 0)
{
cout << "Invald Data: " << input << endl << endl;
total_input--;
cout << "Press any key and enter to continue . . ." << endl;
cin >> exit;
continue;
}
if (total_input > 49)
{
cout << "Data overload, to many data items: " << input << endl;
cout << "Press any key and enter to continue . . ." << endl;
cin >> exit;
continue;
}
input_array[total_input]= input;
}
process_array(input_array, input, fout);
last_statement(input_array, input, fout);
cout << " Press any key and enter to continue . . .";
cin >> exit;
return 0;
}
void process_array(int input_array[], int data, ofstream& fout)
{
int array_placement= 0,
smallest_number= 0,
largest_number= 0,
average= 0,
sum= 0,
number= 0;
for (array_placement= 0; array_placement < data; array_placement++)
{
if (input_array[array_placement] < smallest_number)
smallest_number= input_array[array_placement];
if (input_array[array_placement] > largest_number)
largest_number= input_array[array_placement];
sum= sum + input_array[array_placement];
if (input_array[array_placement] == 0)
break;
else if (input_array[array_placement] < 0)
number--;
else
number++;
}
average= sum / data;
fout << number << " intergers were read" << endl;
fout << "There was " << "1" << " error" << endl << endl;
fout << "Largest: " << largest_number << endl;
fout << "Smallest: " << smallest_number << endl;
fout << "Average: " << average << endl;
fout << "---------------------" << endl;
fout << "Seq Number Sum of RestEven" << endl;
fout << "---------------------" << endl;
fout << "---------------------" << endl;
}
Explanation / Answer
Please Rate:Thanks
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void process_array(int input_array[], int data, ofstream& fout);
int main()
{
char exit;
int input, total_input = 0, input_array[50];
ifstream fin;
ofstream fout;
fin.open("Problem.dat");
if (!fin)
{
cout << "Cannot open the input file." << endl << endl;
fout << "Cannot open the input file." << endl << endl;
cout << "Please press any key and enter to end execution of this program . . .";
cin >> exit;
return 1;
}
for (total_input = 0; fin >> input; total_input++)
{
if (input < 0)
{
cout << "Invald Data: " << input << endl << endl;
total_input--;
// cout << "Press any key and enter to continue . . ." << endl;
// cin >> exit;
continue;
}
if (total_input > 49)
{
cout << "Data overload, to many data items: " << input << endl;
// cout << "Press any key and enter to continue . . ." << endl;
// cin >> exit;
continue;
}
input_array[total_input]= input;
}
fout.open("solution.dat");
process_array(input_array, total_input, fout);
//last_statement(input_array, input, fout);
//cout << " Press any key and enter to continue . . .";
cin >> exit;
return 0;
}
void process_array(int* input_array, int data, ofstream& fout)
{
int array_placement, smallest_number = 9999999, largest_number = 0, average = 0, sum = 0, number= 0;
for (array_placement = 0; array_placement < data; array_placement++)
{
if (input_array[array_placement] < smallest_number)
smallest_number= input_array[array_placement];
z
if (input_array[array_placement] > largest_number)
largest_number= input_array[array_placement];
sum= sum + input_array[array_placement];
if (input_array[array_placement] == 0)
break;
else if (input_array[array_placement] < 0)
number--;
else
number++;
}
average= sum / data;
fout << number << " intergers were read" << endl;
fout << "There was " << "1" << " error" << endl << endl;
fout << "Largest: " << largest_number << endl;
fout << "Smallest: " << smallest_number << endl;
fout << "Average: " << average << endl;
fout << "---------------------" << endl;
fout << "Seq Number Sum of RestEven" << endl;
fout << "---------------------" << endl;
fout << "---------------------" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.