Ideal numbers, arrays, validation and control

I am trying to write C code that will maximize the input of 10 elements (natural numbers) in an array, identify all perfect numbers in an array and make the product of all not perfect numbers.

Euclid proved that 2 ^ {p-1} (2 ^ p-1) is an even perfect number whenever 2 ^ p-1 is prime (Euclid, p. IX.36). For example, the first four perfect numbers are generated by the formula 2 ^ {p-1} (2 ^ p-1), and p is a prime, as follows: for p = 2: 2 ^ 1 (2 ^ 2-1) = 6 for p = 3: 2 ^ 2 (2 ^ 3-1) = 28 for p = 5: 2 ^ 4 (2 ^ 5-1) = 496 for p = 7: 2 ^ 6 (2 ^ 7-1) = 8128 . (source: Wikipedia)

When I compile the program, I get a triple or higher repetition of the declaration of the ideal number.

eg:.

... t [10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6}

'6' is the perfect number. "6" is the perfect number. "6" is the perfect number ....

I also get weird products.

eg:.

... t [10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28}

'28 'is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number. "28" is the perfect number .... The product of not perfect numbers is -1677721600

I am really new to c, I cannot understand what I am doing wrong, but I would not give out either. Some recommendations would be greatly appreciated.

#include <stdio.h> #define MAX_BOUND 9 /*Array bound*/ main() { int i, /*array index*/ t[i], d, /*divider*/ sum, /*result for perfect number validation*/ product; /*product of all non-perfect number in array*/ i = 0; printf("Enter your natural numbers. \n"); for (i = 0; i <= MAX_BOUND; i++) { printf("Number %d : ", i + 1); scanf( "%d", &t[i]); } i = 0; product = 1; for (i = 0; i <= MAX_BOUND; i++) { d = 1; sum = 0; while(d < t[i]) { if(t[i]%d == 0) sum = sum + d; d++; if(sum == t[i]) printf("%d is a perfect number. \n", t[i]); else product = product * t[i]; } } printf("The product of the non-perfect numbers is %d \n", product); getch(); } 
+4
source share
2 answers

In the array declaration, you have undefined behavior because you used the wrong size:

 main() { int i, t[i], d, /*divider*/ sum, product; i = 0; printf("Enter your natural numbers. \n"); while (i <= 9) { printf("Number %d : ", i + 1); scanf( "%d", &t[i]); i++; } 

You probably wanted to announce

 t[MAX_BOUND+1]; 

( MAX_BOUND will be incorrect since you are using the t[MAX_BOUND] element).

At the point where t declared, i has an undefined value (it is unlikely that it is 0).

With an indefinite array size, access t[i] causes even more indefinite values ​​(and again this behavior is undefined if i >= sizeof t / sizeof t[0] ).

Printed part

  if(sum == t[i]) printf("%d is a perfect number. \n", t[i]); else product = product * t[i]; 

should be moved after the cycle is used to determine the sum of the divisor. At the same time, inside the cycle, you multiply product by t[i] total t[i] - 1 time (or t[i] - 2 , if one of the intermediate sums is t[i] ), if t[i] not perfect, and t[i]/2-1 if t[i] perfect. In addition, you print t[i]/2 times for perfect numbers and once for plentiful numbers if one of the subtotals is t[i] (I ignore the theoretical possibility of odd perfect numbers, if any, they are too big for int )

Executing this result gives the correct result.

+2
source

"Weird product" (e.g. negative) caused by integer overflow . Your product is int , make it bigger, long long , for example.

You should use for loops with i , not while . Checking the code if the number is perfect should be placed in a separate function bool isPerfect(int number) .

You meant sum = 0 , not somme = 0 . The declaration t[i] also incorrect.

Corrected version (compile with gcc -std=c99 file.c ):

 #include <stdio.h> #include <stdbool.h> #define MAX 10 int t[MAX]; bool isPerfect(int number) { int sum = 0; for (int d = 1; d < number; ++d) // optimization: you can iterate until sqrt(number) { if (number % d == 0) { sum += d; } } return sum == number; } int main() { printf("Enter your natural numbers. \n"); for (int i = 0; i < MAX; ++i) { printf("Number %d: ", i + 1); scanf("%d", &t[i]); } long long product = 1; for (int i = 0; i < MAX; ++i) { if (isPerfect(t[i])) { printf("%d is a perfect number. \n", t[i]); } else { product = product * t[i]; } } printf("The product of the non-perfect numbers is %lld \n", product); return 0; } 
+3
source

All Articles