Question

In: Computer Science

Write a C++ program, all the answers so far on this site have been wrong when...

Write a C++ program, all the answers so far on this site have been wrong when I compiled them.. Desired outputs at the end.

--In this exercise, you are to modify the Classified Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (terminal screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements:

a) Data to the program is input from a file of an unspecified length named Ch06_Ex20Data.txt; that is , the program does not know in advance how many numbers are in the file.

b) Save the output of the program in a file named Ch06_Ex20Out.txt.

c) Modify the void function getNumber so that it reads a number from the input file (opened in the function main), outputs the number to the output file (opened in the function main), and sends the number read to the function main. Print only 10 numbers per line. Assume all numbers are between -9999 and 9999.

d) Have the program find the sum and average of the numbers.

e) Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new definition of the function printResult should also output the sum of the numbers

Outputs in the program should look like this:

Processing Data

There are 27 evens, which includes 12 zeros

Total number of odds are: 16

The sum of numbers = 558

The average is 12

press any key to exit.

The OutFile should look like this

E:\FolderName\Lab\Ch06_Ex20> type Ch06_Ex20Out.txt

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

There are 27 evens, which includes 12 zeros

Total number of odds are: 16

The sum of numbers = 558

The average is 12

E:\FolderName\Lab\Ch06_Ex20>

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <iostream>

#include <fstream>

using namespace std;

int Size = 0;

int odds = 0;

int evens = 0;

int zeroes = 0;

// read numbers from input file and print then then return in form of given array

void getNumber(ifstream &input, ofstream &output, int *numbers)

{

    int i = 0;

    int n = 0;

    // read data and write to output file

    while (input >> numbers[i])

    {

        Size++;

        if (n == 10)

        {

            output << endl;

            n = 0;

        }

        if (numbers[i] % 2 == 0)

        {

            if (numbers[i] == 0)

                zeroes++;

            evens++;

        }

        else

        {

            odds++;

        }

        output << numbers[i] << " ";

        n++;

        i++;

    }

}

// this function returns sum of numbers in array

int find_sum(int numbers[])

{

    int sum = 0;

    for (int i = 0; i < Size; i++)

    {

        sum += numbers[i];

    }

    return sum;

}

// this function returns average of numbers in array

int average(int numbers[])

{

    return find_sum(numbers) / Size;

}

// this function prints result

void printResult(int sum, int avg)

{

    cout << "There are " << evens << " evens, which includes " << zeroes << " zeroes\n";

    cout << "Total number of odds are: " << odds << endl;

    cout << "The sum of numbers = " << sum << endl;

    cout << "The average is " << avg << endl;

}

int main()

{

    // open input file

    ifstream input("Ch06_Ex20Data.txt");

    // open output file

    ofstream output("Ch06_Ex20Out.txt");

    // if input file has not been opened

    if (!input.is_open())

    {

        cout << "Can't open input file!";

        return 0;

    }

    int numbers[1000];

    cout << "Processing Data\n";

    getNumber(input, output, numbers);

    int sum = find_sum(numbers);

    int avg = average(numbers);

    // write results to output file

    output << endl;

    output << "There are " << evens << " evens, which includes " << zeroes << " zeroes\n";

    output << "Total number of odds are: " << odds << endl;

    output << "The sum of numbers = " << sum << endl;

    output << "The average is " << avg << endl;

    printResult(sum, avg);

    input.close();

    output.close();

}

Output:

output file:


Related Solutions

Write a program to print the number of class assignments you have done so far, the...
Write a program to print the number of class assignments you have done so far, the total grades, and average grade. Prompt a 0 input to end the program. Java Using while statements in the lecture this was provided. Wording confused me, Total grade is sum of all grades.
So far in this course, all of your programs have been written to be fully contained...
So far in this course, all of your programs have been written to be fully contained inside a single method named main. The main method is where Java begins execution of your program. In this assignment, you will coding other methods in addition to the main method. These additional methods will perform specific functions and, in most cases, return results. Write all your methods in a class named Homework6Methods.java Write a public static method named getMaxOf2Ints that takes in 2...
So far we far we have been discussing the advantages of HIT, but have not considered...
So far we far we have been discussing the advantages of HIT, but have not considered its disadvantages. Discuss at least three (3) of the disadvantages or challenges that can be encountered by adoption of the HIT, such as EHR (Electronic Health Record) or HIE (health Information Exchange).
have been posting the same questions and not getting an answers for so please answer all...
have been posting the same questions and not getting an answers for so please answer all the questions : ACLS q’s The cardiac arrest rhythm associated with NO discernible electrical activity on the ECG is termed as _________?   Pulseless electrical activity   Ventricular tachycardia   Bradycardia   Asystole Bradycardia is defined as any rhythm disorder with a heart rate less than:   40 beats per minute   50 beats per minute   70 beats per minute   60 beats per minute Indications for transcutaneous pacing (TCP) include...
have been posting the same questions and not getting an answers for so please answer all...
have been posting the same questions and not getting an answers for so please answer all the questions : ACLS q’s True or False: Therapeutic hypothermia should be considered in the comatose adult after cardiac arrest.   True   False Ventricular fibrillation can be a life-threatening complication of ACS.   True   False True or False: 100% oxygen is acceptable for early intervention but not for extended periods of time.   True   False True or False: Interruptions in CPR for repeated consecutive defibrillator shocks always...
have been posting the same questions and not getting an answers for so please answer all...
have been posting the same questions and not getting an answers for so please answer all the questions : PALS Q’s True or False: A respiratory rate consistently less than 10 or greater than 60 breaths per minute in a child of any age is abnormal and suggests the presence of a potentially serious problem.   True   False True or False: Cardiopulmonary arrest is a common result of respiratory failure and shock.   True   False True or False: Immediate intubation is always...
have been posting the same questions and not getting an answers for so please answer all...
have been posting the same questions and not getting an answers for so please answer all the questions : ACLS q’s True or False: If the AED advises no shock, you should still defibrillate because defibrillation often restarts the heart with no pulse.   True   False The ACLS Survey includes assessing which of the following?   Airway, Breathing, Circulation, Deformity   Airway, Breathing, Circulation, Defibrillation   Airway, Blood Pressure, CPR, Differential Diagnosis   Airway, Breathing, Circulation, Differential Diagnosis Hyperventillation (over ventillation) can be harmful because...
have been posting the same questions and not getting an answers for so please answer all...
have been posting the same questions and not getting an answers for so please answer all the questions : PALS Q’s If bradycardia (heart rate less than 60 beats per minute) with a pulse is associated with cardiopulmonary compromise despite effective oxygenation and ventilation, what is the next step in management?   Administer atropine.   Administer epinephrine.   Consider cardiac pacing.   Perform CPR. When should you use the “pediatric bradycardia with a pulse” algorithm?   Only on bradycardic individuals with signs of shock   Only...
I have solved most of question and the answers I have so far are correct, I...
I have solved most of question and the answers I have so far are correct, I just need help finding out the lower bound and upper bound (All answers were generated using 1,000 trials and native Excel functionality.) Galaxy Co. distributes wireless routers to Internet service providers. Galaxy procures each router for $75 from its supplier and sells each router for $125. Monthly demand for the router is a normal random variable with a mean of 100 units and a...
Here is my C++ program so far. Is there anyway I can make it display an...
Here is my C++ program so far. Is there anyway I can make it display an error if the user enters a float? Thanks #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10; int main() { // Assigning literals to the varibles int a[size]; int sum=0; float avg; // For loop that will reiterate until all 10 integers are entered by the user for(int i=0; i<size; i++)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT