Passing an array of numbers to a function

I am trying to find if the numbers from a file stored in an array are perfect numbers

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

bool isPerfect(int numbers []);

int main (){
ifstream inputFile;
int num, k=0;
bool perfect;
int numbers[140];
inputFile.open("numbers.txt");

if (!inputFile.is_open()) {
cout << "Error";
}

inputFile >> num;

while (num != -1) {
numbers[k]=num;


perfect = isPerfect (numbers);
if (perfect== true){
cout<<num<< " Yes"<<endl;
}
else if (perfect==false){
cout<<num<<" No"<<endl;
}
k=k+1;
inputFile>>num;
}
return 0;

}

bool isPerfect(int numbers[]){
int n,i=1,sum=0;
while(i<numbers[n]){
if(numbers[n]%i==0)
sum=sum+i;
i++;
}
if(sum==numbers[n]){

return true;
}
else {
return false;
}
}
What is your question? What are you having issues with? The title doesn't tell us much, since it's just stating something that can be done...
I am trying to find if the numbers from a file stored in an array are perfect numbers. I am having issues with the isPerfect function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>

bool is_perfect( int number )
{
    if( number < 1 ) return false ; // perfect numbers are positive

    // there are no odd perfect numbers (at least none that that could be held in int)
    // see: https://en.wikipedia.org/wiki/Perfect_number#Odd_perfect_numbers
    if( number%2 == 1 ) return false ;

    int aliquot_sum = 3 ; // sum of proper divisors
                          // initialised to 3 (even number, so one and two are proper divisors)

    // check for divisibility with all integers from 3 up to n/2
    for( int i = 3 ; i <= number/2 ; ++i )
        if( number%i == 0 ) aliquot_sum += i ; // divisible, add to sum of divisors

    return aliquot_sum == number ; // return true if it is a perfect number
}

void print_results( const int array[], int cnt )
{
    for( int i = 0 ; i < cnt ; ++i )
    {
        std::cout << array[i] << ' ' ;
        if( is_perfect( array[i] ) ) std::cout << "Yes\n" ;
        else std::cout << "No\n" ;
    }
}

int main()
{
    const int SENTINEL = -1 ; // sentinel to signal end of input

    const int ARRAY_SZ = 140 ;
    int array[ARRAY_SZ] {} ; // initialise to all zeroes (not required, but often a good idea)

    const char file_name[] = "numbers.txt" ;
    int cnt_numbers_read = 0 ; // count of numbers actually read from the file

    std::ifstream file(file_name) ; // open file for input

    // read up to a max of ARRAY_SZ numbers and place them in the array
    // exit loop if a. ARRAY_SZ numbers have been read
    //              b. there are no more numbers in the file (input failed)
    //              c. the number that we read is the sentinel value
    int number ;
    while( /*a*/ cnt_numbers_read < ARRAY_SZ && /*b*/ file >> number && /*c*/ number != SENTINEL )
        array[cnt_numbers_read++] = number ; // note: postfix increment of cnt_numbers_read

    print_results( array, cnt_numbers_read ) ;
}
Topic archived. No new replies allowed.