Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A file contains an unknown number of integers that may be positive, negative, or

ID: 3801972 • Letter: A

Question

A file contains an unknown number of integers that may be positive, negative, or zero. Complete the following C++ program. THe program should read and process the input as follows.

- for each positive integer, compute the sum of its factors, then display the integer and the sum.
-compute the exact average of the negative integers
-continue reading and processing input until a 0 is encountered (read)
-display the average of the negative integers.

Notes:

There we be at least one zero in the file, but it may not be the last value in the file. Input will be read via Linux Redirection. The file will exist and not be empty.There will be at least one negative number in the file (do not check for division by 0). DO NOT USE SEPERATE FUNCTIONS.

Sample input file:
19 24 -3 18 11 -16 0 150

Sample output:
Sum of the factors for 19 is 20
Sum of factors for 24 is 60
Sum of factors for 18 is 39
Sum of factors for 11 is 12
Average of negative numbers = -9.5

Part of the code:

#include <iostream>
using namespace std;
int main()
{
int count = 0;
int factorcount = 0;
int data;
double sum = 0.0;
cin >> data;
While (data != 0)
{

}
cin >> data;
cout << "Average fo negative number #'s = " << sum << endl;

}

}

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include<fstream>
using namespace std;
//Main function definition
int main()
{
//Variable declaration
int count = 0;
int factorcount = 0;
int data;
double sum = 0.0;

//Open file in read mode
ifstream ifile("Numbers.txt");
//If file does not exist show error message and exit
if (ifile.fail())
   {
cout << "Error!";
exit(0);
   }//end of if

   //Loops till end of file
while (!ifile.eof())
{
//Read data from file and stores it in respective data members
ifile>>data;
//If the data is zero then stop reading data from file
if(data == 0)
break;
//If data is positive
if(data > 0)
{
//Finds the factor of data
for(int x = 1; x <= data; x++)
//Checks if the number is divisible
if(data % x == 0)
//Calculates the factor sum
factorcount += x;
//Displays data with factor sum
cout<<" Sum of factors for "<<data<<" is "<<factorcount;
}//End of if
//Otherwise data is negative
else
{
//Calculates sum
sum += data;
//Increase the counter
count++;
}//End of else
//Re-initializes the factorcount to zero for next data
factorcount = 0;
}//end of while
//Displays the average of negative numbers
cout <<" Average of negative number = " << (sum / count)<< endl;
}//End of main

Input File Contents: 19 24 -3 18 11 -16 0 150

Output:

Sum of factors for 19 is 20
Sum of factors for 24 is 60
Sum of factors for 18 is 39
Sum of factors for 11 is 12
Average of negative number = -9.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote