By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,125 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,125 IT Pros & Developers. It's quick & easy.

pls help debug my c++ program!!!

P: 2
hi..

I am a newbee to c++ programming.. the program compiles.. but when i run, the command window prompt hangs.. and there is no output.. Pls help me out..

the source code is attached.. thanks in advance...



Expand|Select|Wrap|Line Numbers
  1. # include <iostream.h>
  2. # include <cstdlib>
  3.  
  4. using namespace std; 
  5.  
  6. // The types of components that can be present in the circuit.
  7. enum component_type {resistor,capacitor,inductor};
  8. typedef int node;
  9.  
  10. // population size for the GA and the declaration of the classes gene and chromosome.
  11. int popsize=100;
  12. int s[100];
  13.  
  14. class gene
  15. {
  16.       public:
  17.               component_type type;
  18.               node n1,n2;
  19. };
  20. class chromosome
  21. {
  22.       public:
  23.              gene genes[20];
  24.  
  25.              //population initialisation
  26.              void initialisation()
  27.              {          
  28.                   chromosome inipop[popsize];
  29.  
  30.                   for (int i=1;i<=popsize;i++)
  31.                   {   
  32.                       //randomisation of the number of genes and nodes
  33.                       int number_genes=(rand()%20)+1;
  34.                       int number_nodes=3;
  35.  
  36.                       for (int j=1;j<=number_genes;j++)
  37.                       {
  38.                           int comp=(rand() % 2) ; 
  39.                           inipop[i].genes[j].type=component_type(comp);
  40.                           inipop[i].genes[j].n1=(rand()%(number_nodes+1));
  41.                           inipop[i].genes[j].n2=(rand()%(number_nodes+1));
  42.                           while (inipop[i].genes[j].n2==inipop[i].genes[j].n1)
  43.                           {
  44.                                 inipop[i].genes[j].n2=(rand()%(number_nodes+1));
  45.                           }
  46.                           number_nodes++;
  47.                       }
  48.                       s[i]=number_genes;
  49.                   }
  50.                   for (int l=1;l<=popsize;l++)
  51.                       cout<<s[l];
  52.                   for (int i=1;i<=100;i++)
  53.                   {
  54.                       for (int j=1;j<s[i];j++)
  55.                       {
  56.                           cout<<inipop[i].genes[j].type<<"\t"<<inipop[i].genes[j].n1<<"\t"<<inipop[i].genes[j].n2<<"\n";
  57.                       }
  58.                   }
  59.            }
  60.  
  61.  
  62. }a1;
  63.  
  64. int main()
  65. {
  66.     a1.initialisation();
  67.     system("PAUSE");
  68.     return 0;
  69. }
Dec 28 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
hi..

I am a newbee to c++ programming.. the program compiles.. but when i run, the command window prompt hangs.. and there is no output.. Pls help me out..

the source code is attached.. thanks in advance...



Expand|Select|Wrap|Line Numbers
  1. # include <iostream.h>
  2. # include <cstdlib>
  3.  
  4. using namespace std; 
  5.  
  6. // The types of components that can be present in the circuit.
  7. enum component_type {resistor,capacitor,inductor};
  8. typedef int node;
  9.  
  10. // population size for the GA and the declaration of the classes gene and chromosome.
  11. int popsize=100;
  12. int s[100];
  13.  
  14. class gene
  15. {
  16.       public:
  17.               component_type type;
  18.               node n1,n2;
  19. };
  20. class chromosome
  21. {
  22.       public:
  23.              gene genes[20];
  24.  
  25.              //population initialisation
  26.              void initialisation()
  27.              {          
  28.                   chromosome inipop[popsize];
  29.  
  30.                   for (int i=1;i<=popsize;i++)
  31.                   {   
  32.                       //randomisation of the number of genes and nodes
  33.                       int number_genes=(rand()%20)+1;
  34.                       int number_nodes=3;
  35.  
  36.                       for (int j=1;j<=number_genes;j++)
  37.                       {
  38.                           int comp=(rand() % 2) ; 
  39.                           inipop[i].genes[j].type=component_type(comp);
  40.                           inipop[i].genes[j].n1=(rand()%(number_nodes+1));
  41.                           inipop[i].genes[j].n2=(rand()%(number_nodes+1));
  42.                           while (inipop[i].genes[j].n2==inipop[i].genes[j].n1)
  43.                           {
  44.                                 inipop[i].genes[j].n2=(rand()%(number_nodes+1));
  45.                           }
  46.                           number_nodes++;
  47.                       }
  48.                       s[i]=number_genes;
  49.                   }
  50.                   for (int l=1;l<=popsize;l++)
  51.                       cout<<s[l];
  52.                   for (int i=1;i<=100;i++)
  53.                   {
  54.                       for (int j=1;j<s[i];j++)
  55.                       {
  56.                           cout<<inipop[i].genes[j].type<<"\t"<<inipop[i].genes[j].n1<<"\t"<<inipop[i].genes[j].n2<<"\n";
  57.                       }
  58.                   }
  59.            }
  60.  
  61.  
  62. }a1;
  63.  
  64. int main()
  65. {
  66.     a1.initialisation();
  67.     system("PAUSE");
  68.     return 0;
  69. }
try putting some output in the while loop to see how many times it's iterating

Expand|Select|Wrap|Line Numbers
  1. while (inipop[i].genes[j].n2==inipop[i].genes[j].n1)
  2.     {
  3.       inipop[i].genes[j].n2=(rand()%(number_nodes+1));
  4.      cout<<"Something"<<endl;
  5.  }
Dec 28 '06 #2

P: 2
hey..

i tried even commenting the while loop.. but the problem still exists.. :(
Dec 28 '06 #3

10K+
P: 13,264
hey..

i tried even commenting the while loop.. but the problem still exists.. :(
Debug it with cout statements. If you put a cout as the first statement of main is it outputting something?
Dec 28 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.