Connecting Tech Pros Worldwide Forums | Help | Site Map

C - Calculating Perfect Numbers

Newbie
 
Join Date: Nov 2008
Posts: 18
#1: Nov 17 '08
Ok so I've been away from math for a long while now (and programming for that matter) and I've got this assignment that i'm kinda stuck on

i'll paste my code at the bottom... it's not complete, i'll say that now but i just need some1 to steer me in the right direction.
As i'm not entirely familiar with the calculations of the perfect numbers ( i know what it means just not entirely sure of how to code it)

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. main()
  5.  
  6. {
  7.    // initialisation des variables
  8.    int nbrMax;                        // Nbr d'entiers à saisir
  9.    int tableauNbr[nbrMax-1];          // Tableau pour contenir les nombres entrés par l'usager
  10.    int i;                             // Compteur boucle
  11.    int j, k, somme;                   // variables pour calcul nbr parfait
  12.    char condition;                    // Oui ou Non
  13.  
  14.    // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
  15.    do
  16.       {
  17.       printf ("Entrez le nombre d'entier (Max 10):\n");
  18.       scanf ("%d", &nbrMax);
  19.  
  20.       // boucle pour la saisie des nombres et remplissage le tableau.
  21.       for (i = 0; i < nbrMax; i++)
  22.           {
  23.           scanf ("%d", &tableauNbr[i]);
  24.           }
  25.  
  26.       // Calculs pour trouver les nombres parfaits
  27.       somme = 0;
  28.       for (j = 0; j < nbrMax; j++)
  29.           {
  30.             for (k = 1; k < tableauNbr[j]; k++)
  31.                 {
  32.                     if(tableauNbr[j]%k==0)
  33.                         {
  34.                         somme += k;
  35.                         }
  36.       }
  37.                 // Affichage des resultats
  38.                     if (somme = tableauNbr[j])
  39.                         printf("%d est un nombre parfait\n", tableauNbr[j]);
  40.                         else
  41.                         printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
  42.  
  43.           }    
  44.  
  45.  
  46.  
  47.  
  48.       // Condition requise pour repartir du début
  49.       printf ("\nVoulez vous faire un autre calcul, (o/n)?");
  50.       fflush (stdin);                        // Fonction pour vider stdin
  51.       condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
  52.       }
  53.    while ( condition == 'O');                // Repartir du début si O
  54.  
  55.    system("pause") ;
  56. }
  57.  

Newbie
 
Join Date: Nov 2008
Posts: 18
#2: Nov 17 '08

re: C - Calculating Perfect Numbers


how about, if i try to do the calculation for 1 integer first and then build a table around it... coz then instead of dealing with 1 i'd be dealing with a for loop on a table list of elements, right ?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#3: Nov 17 '08

re: C - Calculating Perfect Numbers


Your program looks about right except that

Expand|Select|Wrap|Line Numbers
  1.        somme = 0;
  2.        for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 17 '08

re: C - Calculating Perfect Numbers


Quote:

Originally Posted by Banfa

Your program looks about right except that

Expand|Select|Wrap|Line Numbers
  1.        somme = 0;
  2.        for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.

There's more; look here near the start of main():

Expand|Select|Wrap|Line Numbers
  1.    int nbrMax;                        // Nbr d'entiers à saisir 
  2.    int tableauNbr[nbrMax-1];          // Tableau pour contenir les nombres entrés par l'usager 
  3.  
nbrMax isn't initialized (yet).

kind regards,

Jos
Newbie
 
Join Date: Nov 2008
Posts: 18
#5: Nov 17 '08

re: C - Calculating Perfect Numbers


Quote:

Originally Posted by Banfa

Your program looks about right except that

Expand|Select|Wrap|Line Numbers
  1.        somme = 0;
  2.        for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.

i don't want to reset my sum every j loop because i need to verify if the sum = table member then it's a perfect number.
Newbie
 
Join Date: Nov 2008
Posts: 18
#6: Nov 17 '08

re: C - Calculating Perfect Numbers


that's the code i have for now... it's like the calculations are doing nothing... i can "see" what the loop is doing, i'll put some prinft to see how the variables are acting up.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. main()
  6. {
  7.    // initialisation des variables
  8.    int nbrMax=0;                        // Nbr d'entiers à saisir
  9.    int produit=0;
  10.    int tableauNbr[nbrMax];            // Tableau pour contenir les nombres entrés par l'usager
  11.    int i, j, k;                       // Compteur boucle
  12.    int somme;                   // variables pour calcul nbr parfait
  13.    char condition;                    // Oui ou Non
  14.  
  15.    // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
  16.    do
  17.       {
  18.       printf ("Entrez le nombre d'entier (Max 10):\n");
  19.       scanf ("%d", &nbrMax);
  20.  
  21.       // boucle pour la saisie des nombres et remplissage le tableau.
  22.       for (i = 0; i < nbrMax; i++)
  23.           {
  24.           scanf ("%d", &tableauNbr[i]);
  25.           }
  26.  
  27.       // Calculs pour trouver les nombres parfaits
  28.       somme = 0;
  29.       for (j = 0; j < nbrMax; j++)            {
  30.  
  31.             for (k = 1; k < tableauNbr[j]; k++)
  32.                 {
  33.                     if(!tableauNbr[j]%k)
  34.                         somme += k;
  35.                 }
  36.                 // Affichage des resultats
  37.                     if (somme == tableauNbr[j])
  38.                         printf("%d est un nombre parfait\n", tableauNbr[j]);
  39.                         else {
  40.                         printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
  41.                         // produit *= tableaNbr[j];        // tableauNbr undeclared, first use in this function error...
  42.                         }
  43.         }    
  44.  
  45.  
  46.       printf("le produit des nombres non-parfaits est: %d", produit);
  47.  
  48.       // Condition requise pour repartir du début
  49.       printf ("\nVoulez vous faire un autre calcul, (o/n)?");
  50.       fflush (stdin);                        // Fonction pour vider stdin
  51.       condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
  52.       }
  53.    while ( condition == 'O');                // Repartir du début si O
  54.  
  55.    system("pause") ;
  56. }
  57.  
/*

Résultats:
==========
Entrez le nombre d'entier (Max 10):
2
6
28
6 n'est pas un nombre parfait
28 n'est pas un nombre parfait
le produit des nombres non-parfaits est: 0
Voulez vous faire un autre calcul, (o/n)?
Press any key to continue . . .


*/
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#7: Nov 18 '08

re: C - Calculating Perfect Numbers


orangeworx-

I have merged your threads as the code is exactly the same between the two. Please confine yourself to one question, in one thread, at a time.

Thanks,

sicarie
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#8: Nov 18 '08

re: C - Calculating Perfect Numbers


Quote:

Originally Posted by sicarie

orangeworx-

I have merged your threads as the code is exactly the same between the two. Please confine yourself to one question, in one thread, at a time.

Thanks,

sicarie

orangeworx-

I definitely made a mistake there, my apologies. I have split the threads again, please let me know if I did not get a post in the right thread between this and this.

Thanks,

sicarie
Reply