New Skills Practiced (Learning Goals) -Problem solving and debugging. -Linux red
ID: 3824085 • Letter: N
Question
New Skills Practiced (Learning Goals)
-Problem solving and debugging.
-Linux redirection to read data from a file.
-Using a for loop.
-Nested loop.
An abundant number is an integer greater than 0 such that the sum of its proper divisors is greater than the integer. For example, 12 is abundant because 1+2+3+4+6 = 16 which is greater than 12.
A deficient number is an integer greater than 0 such that the sum of its proper divisors is less than the integer. For example, 8 is deficient because 1+2+4 = 7 which is less than 8.
A perfect number is an integer greater than 0 such that the sum of its proper divisors is equal to the integer. For example, 6 is perfect because 1+2+3 = 6.
Data File Description
A data file for this exercise will consist of several integers separated by whitespace. The first integer in the file will be greater than 0 and will indicate how many data values are to be processed. DO NOT USE FSTREAM AS INPUT WILL BE TAKEN THROUGH CIN. EXAMPLE CODE IS BELOW.
Here are 2 sample data files:
3
17 -5 246
8 0 11 -12354 894
183 -67 14 33333
Design and implement a complete C++ program that
using Linux redirection, reads the first integer in the file to determine how many data values to process
for each of the data values, determines if the integer is abundant, deficient, perfect or neither and
displays a message that includes the integer and which of the 4 categories it falls into
if the integer is abundant, counts how many factors it has and displays a message that includes its factor count
NOTES:
Make sure you choose enough test data to ensure that your program meets all the requirements.
Sample terminal session:
[keys]$ more data4seven
3
17 -5 246
[keys]$ g++ ex07.cpp
[keys]$ ./a.out < data4seven
17 is deficient
-5 is not abundant, deficient or perfect.
246 is abundant and has 8 factors
*USE A FOR LOOP. THIS DOES NOT USE FSTREAM.
HERE IS PART OF MY CODE. I NEED HELP WITH TAKING THE FIRST NUMBER (THE AMOUNT OF INTEGERS) AND THE INTEGERS THAT FOLLOW.
Please do not use fstream. Linux redirection is just using cin without the prompts.
#include <iostream>
using namespace std;
int main()
{
int n;
int number;
int count;
count = 0;
int sum;
int factorsum;
cin >> n;
for (int i=0; i < n; i++)
{
cin >> number;
if (number < 0)
{
cout << number << " is not abundant, deficient, or perfect." << endl;
}
else if (number == 0)
{
cout << number << " is deficient." << endl;
}
else if (number > 0)
{
}
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int number,i,count,factorSum=0;
count =0;
cout<<" Enter the number.";
cin>>number;
//find factors and the sum of factors of number
for(i=1;i<=number;i++)
{
if (number%i == 0 )
{
factorSum += i;
count++;
}
}
cout<<" Factors sum = "<<factorSum;
cout<<" Number has "<<count<<" factors";
// compare factorSum and number to decide whether the number is
abundant,deficient or perfect
if (number > 0 && factorSum > number)
cout<<" "<<number<<" is abundant ";
else
cout<<" "<<number<<" is not abundant ";
if (factorSum < number)
cout<<"deficient ";
else
cout<<"not deficient ";
if(factorSum == number)
cout<<" perfect";
else
cout<<"not perfect";
return 0;
}
Output:
Enter the number. 246
Factors sum = 504
Number has 8 factors
246 is abundant not deficient not perfect