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

Help, C++ Question Given an integer, n, the integers a and b are said to be fact

ID: 3589737 • Letter: H

Question

Help, C++ Question

Given an integer, n, the integers a and b are said to be factors (or divisors) of n if a*b = n.

Given an integer, n, that is greater than 0,

• n is an abundant number if the sum of its factors is greater than 2* n

• n is a deficient number if the sum of its factors is less than 2* n

• n is a perfect number if the sum of its factors is equal to 2* n

Design and implement a complete C++ program that will

• read a series of integers from a file (via Linux redirection) and display (to the screen) a table with 4 columns (n, abundant, deficient, perfect)

• for each integer read from the file, display

o the integer

o an 'X' or a blank in the appropriate column of the table (note: if the integer does not fall into any of the 3 categories, leave all columns blank)

• when all data has been read and processed, display the average of the abundant numbers with a label and 2 digits to the right of the decimal

NOTES:

• Assume that all input values in the data file will be integers (positive, negative, or zero).

• Right justify the numbers and 'X's in the table. Assume maximum number of columns needed to display a number is 12.

• When constructing data files, separate each integer with whitespace. Each line in the data file should be terminated with a linefeed.

Sample terminal session: [joj5@bobby 17FAl$ cat data4nine 4 17 24 -23 0 6 18 28 [joj5@bobby 17FAIS [joj5@bobby 17FAls g++ ex09.cpp [joj5@bobby 17FAl$./a.out

Explanation / Answer

//solution

#include<iostream>

//header file to use functions to read from file

#include<fstream>

#include <iomanip> // std::setprecision

using namespace std;

//fuction to chck if a numbr is abundant,deficient or perfect

int abundant(int n);

int deficient(int n);

int perfect(int n);

//function to find sum of factors of a number

int sumoffactors(int n);

int main()

{

//declare inputput stream object in to read from file

ifstream in;

int n;

//open file

in.open("numbers1.txt");

//chk if file is open

if (!in)

{

cout << "not able to opn input file numbrs.txt" << endl;

return -1;

}

//now read each numbr and check wheathr its abundant, deficient or perfevt and display

cout << "Input # " << "Abundant " << "Deficient " << "Perfect " << endl;

int count = 0,sum=0;

while (!in.eof())

{

in >> n;

cout << right << " " << n;

if (abundant(n))

{

cout<< " " << right << "X";

sum += n;

count++;

}

if (deficient(n))

{

cout <<" "<<right<<"X";

}

if (perfect(n))

{

cout<< " " << right << "X";

}

cout << endl;

}

cout << " " << "Averange of Abundant #s = " << std::setprecision(2)<<(float)sum / count << endl;

}

int abundant(int n)

{

int fact1, fact2;

if (n <= 0)

return 0;

else

{

if (sumoffactors(n) > 2 * n)

{

return 1; // for abandant

}

return 0;

}

}

int deficient(int n)

{

int fact1, fact2;

if (n <= 0)

return 0;

else

{

if (sumoffactors(n) < 2 * n)

{

return 1; // for dficient

}

return 0;

}

}

int perfect(int n)

{

int fact1, fact2;

if (n <= 0)

return 0;

else

{

if (sumoffactors(n) == 2 * n)

{

return 1; // for abandant

}

return 0;

}

}

int sumoffactors(int n)

{

int sum = 0;

for (int i = 1; i <=n; i++)

{

if (n % i == 0)

{

sum += i;

}

}

return sum;

}

--------------------------------------------------------------

//output

Input # Abundant Deficient Perfect
4 X
17 X
24 X
-23
0
6 X
18 X
28 X

Averange of Abundant #s = 21

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