Question

Write a function named numPerfect that takes as parameters: an array of integer scores between zero...

Write a function named numPerfect that takes as parameters:

an array of integer scores between zero and 100 (inclusive)

the size of the array

and returns the number of perfect scores in the array.

Also write a main function that creates and initializes an array of ints (in the appropriate range), calls the function with that array, and prints out the return value.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iostream>
#include <iomanip>
using namespace std;

// Function Declarations
bool is_perfect(int num);
int numPerfect(int arr[], int size);

int main()
{
// Declaring varaibles
int i = 0, size, num, tot;

// Getting the size of an array
cout << "Enter the size of an array :";
cin >> size;

// Creating integer array dynamically based on size
int* arr = new int[size];

/* Getting the numbers entered by the user
* and populate those values into an array
*/
while (i < size)
{
cout << "\nEnter the number#" << i + 1 << ":";
cin >> num;
if (num < 0 || num > 100)
{
cout << "Invalid .Number must be between 0-100" << endl;
continue;
}
else
{
arr[i++] = num;
}
}

tot = numPerfect(arr, size);
cout << "Total No of Perfect Numbers in the array :" << tot << endl;
return 0;
}

int numPerfect(int arr[], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{

if (is_perfect(arr[i]))
{
count++;
}
}
return count;
}

/* Function implementation which checks
* whether the user entered number is perfect or not
*/
bool is_perfect(int num)
{
// Declaring the variable
int sum = 0;


for (int i = 1; i < num - 1; i++)
{
if (num % i == 0)
sum += i;
}

/* if the user entered number is equal to the
* sum value the number is perfect number
*/
if (sum == num)
return 1;
else
return 0;
}

____________________

Output:


_____________Could you rate me well.Plz .Thank You

Add a comment
Know the answer?
Add Answer to:
Write a function named numPerfect that takes as parameters: an array of integer scores between zero...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT