In: Computer Science
on C++ language please and if you can also put note on it to better undertand it. Write a program that reads data from a data file, the value of which is provided at the end of the problem. Your program is to incorporate the following requirements:
Create an input file using the following data:
43 67 82 0 35 28 -64 7 -87 0 0 0 0 12 23 45 7 -2 -8 -3 -9 4 0
1 0 -7 23 -24 0 0 12 62 100 101 -203 -340 500 0 23 0 54 0 76
ANSWER:
I have provided the properly commented
and indented code so you can easily copy the code as well as check
for correct indentation.
I have provided the input/output image of the code so you can
easily cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
// including all header files
#include <bits/stdc++.h>
using namespace std;
// function getNumber to return inputed number from input file
// takes to argument input and outfile by reference
int getNumber(ifstream& inputfile, ofstream& outputfile){
// defining variable num
int num;
// reading number from inputfile in variable num
inputfile>>num;
// outputing num to outputfile
outputfile<<num<<" ";
// outputing number to monitor
cout<<num<<" ";
// returning num to main
return num;
}
// function classifynumber that take input as number vector
// and take defnined variables even, odd, zeros, sum by reference
// and print 10 numbers in each line and returns average
float classifyNumber(vector<int> numbers,int& even,int& odd,int& zeros,int& sum){
// looping through each number and increment count of all
for(int i=0;i<numbers.size();i++){
// if 10 th number printing newline
if(i%10==0){
cout<<endl;
}
// displaying number
cout<<numbers[i]<<" ";
// incrementing sum
sum = sum+numbers[i];
// checking for 0
if(numbers[i]==0){
// increment zeros
zeros = zeros + 1;
}
// condition for even
else if(numbers[i]%2==0){
even = even + 1;
}
// else odd
else{
odd = odd+1;
}
}
// calculating average
float average = (float)sum/numbers.size();
}
// function printResult
// takes input => output file, even, odd, zeros, sum and average
// display all results
void printResult(ofstream& outputfile,int& even, int& odd,int& zeros,int& sum ,float& average){
// outputing even to output file and monitor
cout<<endl<<"even numbers are: "<<even;
outputfile<<endl<<"even numbers are: "<<even;
// outputing odd to output file and monitor
cout<<endl<<"odd numbers are: "<<odd;
outputfile<<endl<<"odd numbers are: "<<odd;
// outputing zeros to output file and monitor
cout<<endl<<"zeros numbers are: "<<zeros;
outputfile<<endl<<"zeros numbers are: "<<zeros;
// outputing sum to output file and monitor
cout<<endl<<"sum of numbers is: "<<sum;
outputfile<<endl<<"sum of numbers is: "<<sum;
// outputing average to output file and monitor
cout<<endl<<"Average of numbers is: "<<average;
outputfile<<endl<<"Average of numbers is: "<<average;
}
// main function
int main(){
// defining input output file variables
// file variable to read
ifstream inputfile;
// file variable to write
ofstream outputfile;
// opening file to read write using open method of file
inputfile.open("input.txt");
outputfile.open("output.txt");
// defining vector to store all inputed numbers
vector<int> numbers;
// calling function getNumber till inputfile not reaches end of file
// looping till end of file
int num;
while(!inputfile.eof( )){
// calling getNumber by passing inputfile and output file as argument
num = getNumber(inputfile,outputfile);
// pushing number to vector
numbers.push_back(num);
}
// function classifynumber that take input as number vector
// and take defnined variables even, odd, zeros, sum by reference
// and print 10 numbers in each line and returns average
int even=0,odd=0, zeros=0,sum = 0;
float average = classifyNumber(numbers,even, odd,zeros,sum);
// function printResult
// takes input => output file, even, odd, zeros, sum and average
// display all results
printResult(outputfile,even, odd, zeros, sum , average);
// closing files
inputfile.close();
outputfile.close();
return 0;
}
INPUT IMAGE (input.txt)
OUTPUT IMAGE
Console Output
output.txt