C++/plz help

Advertisement


Question
hi
this is the code that i wrote for towers of hanoi it works for n=1 to n=63 but doesn't work for the last number that is n=64,can you help me what is the problem whit it? is it about overflowing in c?

#include<stdio.h>
#include<string.h>
void func1(int A[],int c[]);
void func2(int A[],int B[]);
void func3(int B[],int C[]);
void exit(int C[],int x);
int r,*p1,*p2,*p3,i=0,f,j=0,m,z=0,w,x,n;
void main(){
  int A[64],B[64],C[64];
  printf("Enter numbers of disks:\n");
  scanf("%d",&n);
  A[0]=n+1;
  B[0]=n+1;
  C[0]=n+1;
  m=n;
  w=n;
  while(n>0)
     for(i=1;i<=w;i++){
        A[i]=n;
        n=n-1;
     }
       for(j=1;j<=w;j++)
        B[j]=NULL;
     
     for(z=1;z<=w;z++)
        C[z]=NULL;

     z=0;
     j=0;
     i=w;
       if(w%2!=0){
     do{
         if(f==1)
         func1(A,C);         
         exit(C,x);
         if(f==1)
         break;
         func2(A,B);
         exit(C,x);
         if(f==1)
         break;
         func3(B,C);
         exit(C,x);
         }while(f!=1);
     }
     else{
        do{
         if(f==1)
         break;
         func2(A,B);         
         exit(C,x);
         if(f==1)
         break;
         func1(A,C);
         exit(C,x);
         if(f==1)
         break;
         func3(B,C);
         exit(C,x);
         }while(f!=1);
     }
         
}
void func1(int A[],int C[]){
  if(A[i]<C[z]){
     printf("AC_");
     p1=&A[i];
     z=z+1;
     C[z]=*p1;
     i=i-1;
  }

  else if(A[i]>C[z]){
     printf("CA_");
     p3=&C[z];
     i=i+1;
     A[i]=*p3;
     z=z-1;
  }
}
void func2(int A[],int B[]){
  if(A[i]<B[j]){
     printf("AB_");
     p1=&A[i];
     j=j+1;
     B[j]=*p1;
     i=i-1;
  }

  else if(A[i]>B[j]){
     printf("BA_");
     p2=&B[j];
     i=i+1;
     A[i]=*p2;
     j=j-1;
  }
}
void func3(int B[],int C[]){
  if(B[j]>C[z]){
     printf("CB_");
     p3=&C[z];
     j=j+1;
     B[j]=*p3;
     z=z-1;
  }

  else if(B[j]<C[z]){
       printf("BC_");
     p2=&B[j];
     z=z+1;
     C[z]=*p2;
     j=j-1;
  }
  r=z;
}

void exit(int C[],int x){
  int y;
  y=z;
         for(z=0;z<=r;z++){

         if(C[z]==m+1){
         m=m-1;

         if(m==0){
         x=1;
         }
         }

         else{
         x=0;
         }
         }
         z=y;
         f=x;
}

i will be delight if you help me.
thanx
Bita

Answer
64 fails because arrays A B & C use indices 0 to 63.  Entering 64 for n writes to A[64] (and B[64] and C[64]) which is past the end of storage for A, B & C.  If your array declarations are for 64, 63 is the largest n (and you should have an error check and fail when it's larger).

I suggest

#define MAX_TOWERS 64

And where n is entered:

if (n >= MAX_TOWERS)
 issue error or loop back for a new number

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.