Question

In: Computer Science

on C++ language please and if you can also put note on it to better undertand...

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:

  • Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file.
  • Save the output of the program in a file.
  • Write a function called getNumber which reads a number from the input file, outputs the number to the output file and the monitor and sends the input number to the function main. Make sure, both input and out file are being opened in the main program.
  • Write a function called classifyNumber which counts number of even, odd, and zero (occurrences of zero) available in the input file.
  • Have the program find the sum and average of the numbers and print only 10 numbers per line.
  • Write a function called printResult so that it outputs (in output file and monitor) even number of counts, odd number of counts, number of zeros, sum of the numbers and average of the numbers. Make sure output file is being opened in the function main.

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

Solutions

Expert Solution

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


Related Solutions

Can you please check the grammar and also put them in proper paragraphs? From the results...
Can you please check the grammar and also put them in proper paragraphs? From the results above, we can confidently state that the results obtained are reliable and accurate. The absolute quantification of DNA using a calibration curve in QPCR is an accurate method. And the quantity in each DNA sample can be quantified successfully using the standard curve equation (y = -3.383x + 50.979). The quality of the data obtained and interpreted using standard/calibration curves depends on the quality...
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
TO BE DONE IN C LANGUAGE BEFORE READING THE QUESTION PLEASE NOTE THAT YOUR FUNCTION SHOULD...
TO BE DONE IN C LANGUAGE BEFORE READING THE QUESTION PLEASE NOTE THAT YOUR FUNCTION SHOULD EXACTLY PRODUCE THE GIVEN OUTPUT AND THE TIME COMPLEXITY SHOULD BE O(N^2) YOU NEED TO KNOW ABOUT COCKTAIL SHAKER SORT ALGORITHM: Cocktail-shaker sort is a modification of Bubble sort. Cocktail-shaker sort traverses the data structure in one direction, pushing the largest element ahead towards one end of the data structure, and then in the opposite direction, pushing the smallest element towards the other end,...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
PLEASE CAN YOU DO IN C LANGUAGE. A Sudoku puzzle uses a 9 × 9 grid...
PLEASE CAN YOU DO IN C LANGUAGE. A Sudoku puzzle uses a 9 × 9 grid in which each column and row, as well as each of the nine 3 × 3subgrids, must contain all of the digits 1 ⋯ 9. Figure 4.26 presents an example of a valid Sudoku puzzle. This consists of designing a multithreaded application that determines whether the solution to a Sudoku puzzle is valid. There are several different ways of multithreading this application. One suggested...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: Person. Define Class Person Write the class header Class Variables Class Person is to have the following data members: firstName of type String, lastName of type String representing a person’s first and last names and ssn of type int representing a social security number. Each data member has an access specifier of type private. Constructors Class Person...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: TV. Define Class TV Write the class header Class Variables Class TV is to have the following instance variables: 1.channel of type int 2. volumeLevel of type int 3. isOn of type boolean Each data member is to have an access specifier of private. Constructor Class TV is to have a single, no-arg constructor that initializes the...
JAVA, trying to better learn and use JAVA //Please note throughout if possible looking to better...
JAVA, trying to better learn and use JAVA //Please note throughout if possible looking to better understand the process. Write the following class: Box Define Class Box Write the class header Class Variables Class Box is to have the following private data members: height of type double width of type double length of type double Constructors Class Box is to have two constructors with the following specifications: a no-arg constructor that initializes each double data member to zero a constructor...
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT