Code.GeekInterview.com
 
Code Samples C++
 

Number Puzzle


Code ResourceAuthor: venki_madesh  

Difficulty Level:

Published: 11th Nov 2006   Read: 6207 times  

Filed in: C++
Add Comment


 

 

Sponsored Links


 

 

Number Puzzle enter in between 1 to 27. If you want above 27 then change the integer value in the program.

 


Sample Code
  1.  
  2. #include<iostream.h>
  3. class NumPuzzle
  4. {
  5.         public:
  6.         int x,i,j,m,count;
  7.         int puz[25][25];
  8.         void numpuzz();
  9.         void putData();
  10.    NumPuzzle();
  11.  
  12. };
  13. NumPuzzle::NumPuzzle()
  14.         {
  15.                  for(i=0;i<25;i++)
  16.                         for(j=0;j<25;j++)
  17.                                 puz[i][j]=0;
  18.                 cout<<"Enter the Odd number between 1 to 27:";
  19.                 a:
  20.                 cin>>x;
  21.                 if(x%2==0)
  22.                 {
  23.                   cout<<endl<<"Enter the Odd number:";
  24.                   goto a;
  25.                 }
  26.         }
  27. void NumPuzzle::putData()
  28. {
  29.         for(i=0;i<x;i++)
  30.         {
  31.                 for(j=0;j<x;j++)
  32.                 {
  33.                         cout<<puz[i][j];
  34.                         cout<<"    "<<"    ";
  35.                 }
  36.                 cout<<endl<<endl;
  37.         }
  38. }
  39. void NumPuzzle::numpuzz()
  40. {
  41.         m=x/2;
  42.         count=1;
  43.         while(puz[x-1][x/2]==0)
  44.         {
  45.                 if(puz[0][x/2]==0)
  46.                 {
  47.                         puz[0][x/2]=count;
  48.                         count++;
  49.                         j=0;
  50.                 }
  51.                 else if(j==0&&m<x)
  52.                 {
  53.                         m++;
  54.                         j=x-1;
  55.                         if(m==x)
  56.                         {
  57.                                 m=x-1;
  58.                                 j=1;
  59.                         }
  60.                         puz[j][m]=count;
  61.                         count++;
  62.                         if((m==(x-1))&&(j==x/2-1)&&puz[x-1][x/2-1]!=0)
  63.                         {
  64.                                 m=m+1;
  65.                                 j=x-1;
  66.                                 puz[j][m]=count;
  67.                                 count++;
  68.                         }
  69.                 }
  70.                 else if(m==x-1&&j>0)
  71.                 {
  72.                         m=0;
  73.                         j=j-1;
  74.                         puz[j][m]=count;
  75.                         count++;
  76.                 }
  77.                 else if(j<=x-1&&m>=0&&j!=0)
  78.                 {
  79.                         if(puz[j-1][m+1]==0&&m!=x)
  80.                         {
  81.                                 m=m+1;
  82.                                 j=j-1;
  83.                                 puz[j][m]=count;
  84.                                 count++;
  85.                         }
  86.  
  87.                         else if(puz[j-1][m+1]!=0||m==x)
  88.                         {
  89.                                 j=j+1;
  90.                                 puz[j][m]=count;
  91.                                 count++;
  92.                         }
  93.                 }
  94.         }
  95. }
  96. void main()
  97. {
  98.         char ch='y';
  99.         do
  100.         {
  101.                 NumPuzzle venki;
  102.                 venki.numpuzz();
  103.                 venki.putData();
  104.                 cout<<endl<<"If you want ot continue press 'y':"<<endl;
  105.                 cin>>ch;
  106.         }while(ch=='y');
  107.         cout<<endl<<" thank you ";
  108. }
  109.  
Copyright GeekInterview.com


Next Article: Function returning pointers


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

good one ya.
Comment posted by: nik on 2007-03-10T11:27:46
Thanks
Comment posted by: ataur rahaman on 2011-09-23T03:53:34

Popular Coders

# Coder NameHits
1. Priya36668
2. chowsys14956
3. bipin_vaylu13578
4. raghu.mohindru8719
5. Subashini7235
6. vimal.fire6979
7. rachittyagi6626
8. venki_madesh6208
9. prabha6192
10. vipin gupta5907

Active Coders

Refined Tags