C++/plz help

Advertisement


Question
QUESTION: hi
i wrote the code below but i have a problem with the first function,for  example we enter m=1 and n=2 in the func1 the value of m and n changed into m=1 and n=1 and when we do func1again  the value of m and n should become m=1 and n=0 but when i compile the program in the second level it again shows "m=1&n=1" can you help me what's the problem whit it?


#include<stdio.h>
#include<string.h>

void func1(int m,int n,int w);
void func2(int m,int n);
void func3(int m,int n);

int A[50],i=0,t=0,w=0,m,n;

void main(){

  printf("Enter m&n:\n");
  scanf("%d%d",&m,&n);
  printf("m=%d,n=%d\n",m,n);

   i=0;
  A[i]=0;

  do{

       func1(m,n,w);

     func2(m,n);

         func3(m,n);

  }while(t!=1);

         printf("%d\n",n+1);

}
void func1(int m,int n,int w){---------------->/the first function that that shows again m=1 & n=1 in the second level/

  if(m>0&&n>0){
        w=m;
        printf("w1=%d\n",w);

        w=w-1;
         printf("w2=%d\n",w);

        i++;

        A[i]=w;
        n=n-1;
        printf("w=%d,A[i]=%d,m=%d,n=%d\n",w,A[i],m,n);
  }
  
}
void func2(int m,int n){

  if(n==0){
        m=m-1;
        n=1;
     }

}
void func3(int m,int n){
  if(m==0){
        m=A[i];
        i--;
        n=n+1;
        if(i==0){
         t=1;
        }
     }
}



thanx
bita

ANSWER: You can treat the arguments of a function as variables, however direct manipulation of these arguments won't change the values of the variables in the calling function. Arguments are always passed by value in C function calls. The value passed to the function is a copy of the variable at the place of the call. This copy is a local variable available to the function, and it disappears on return from the function. Any change made to the arguments internally in the function are made only to these local copies.

As an example, consider exchanging two variables. First let's illustrate what happen if the variables are passed by value:

#include < stdio.h>

    /* WRONG CODE */

void exchange( int a, int b ) ;

void main()
{         
   int a, b;

   a = 5;
   b = 7;
   printf( "From main: a = %d, b = %d\n", a, b ) ;

   exchange( a, b ) ;
   printf( "Back in main: " ) ;
   printf( "a = %d, b = %d\n", a, b ) ;
}

void exchange( int a, int b )
{
   int temp ;

   temp = a ;
   a = b ;
   b = temp ;
   printf( " From function exchange: " ) ;
   printf( "a = %d, b = %d\n", a, b ) ;
}

Run this code and observe that a and b in main are NOT exchanged! Only the copies of the arguments a and b are exchanged.

In order to modify the caller's variables, the arguments must be passed as an address (pointer). As the pointer available in the function is a copy of the address of the original variable, dereferencing the pointer would allow the function to access the original variable.

#include < stdio.h>

     /* RIGHT CODE */

void exchange ( int *a, int *b ) ;

void main()
{   
   int a, b ;

   a = 5 ;
   b = 7 ;
   printf( "From main: a = %d, b = %d\n", a, b ) ;

   exchange( &a, &b ) ;
   printf( "Back in main: " ) ;
   printf( "a = %d, b = %d\n", a, b ) ;
}

void exchange ( int* a, int* b )
{
   int temp;

   temp = *a;
   *a = *b;
   *b = temp;
   printf( " From function exchange: " ) ;
   printf( "a = %d, b = %d\n", *a, *b ) ;
}



Another issue with your code is that the loop

      do{

         func1(m,n,w);

         func2(m,n);

         func3(m,n);

      }while(t!=1);

would loop for ever! the variable t is never modified in this loop.


---------- FOLLOW-UP ----------

QUESTION: hi
thanx a lot for your answer.i have changed my code to the code below.can you check it out,and  tell me if there is another way or easier way for correcting it?
and you know i dont know why it doesn't work if for example we enter m=7 & n=7, for these numbers it gives me an awesome answer.


#include<stdio.h>
#include<string.h>

void func1(int*m,int*n,int w,int A[]);
void func2(int*m,int*n);
void func3(int*m,int*n);

int A[50],i=0,t=0,w=0,m,n;

void main(){

  printf("Enter m&n:
");
  scanf("%d%d",&m,&n);
  printf("m=%d,n=%d
",m,n);

   i=0;
  A[i]=0;

  do{

     func1(&m,&n,w,A);
  
  func2(&m,&n);

   func3(&m,&n);

  }while(t!=1);

         printf("%d
",n+1);

}
void func1(int*m,int*n,int w,int A[]){
  int q,s;
  q=*m;
  s=*n;

  if(q>0&&s>0){
     
        w=q;

        w=w-1;

        i++;
        printf("i=%d
",i);

        A[i]=w;
        s=s-1;
        *m=q;
        *n=s;

  }
}
void func2(int*m,int*n){
int q,s;
q=*m;
s=*n;

  if(s==0){
        q=q-1;
        s=1;
         *m=q;
        *n=s;
     }
}
void func3(int*m,int*n){
int q,s;
q=*m;
s=*n;
  if(q==0){

        q=A[i];
        if(i!=0){
        i--;
        s=s+1;
        if(i==0)
         t=1;
        }
        else
         t=1;
        *m=q;
        *n=s;
  }
}


thanx
Bita

ANSWER: > can you check it out,and  tell me if there is another way or easier way for correcting it?

it is not clear to me at all, what is it that your program is expected to do.

could you tell me
a. what is this program supposed to do?
b. for a particular input of m and n, what is the expected output?
c. what is the actual output you are getting?


---------- FOLLOW-UP ----------

QUESTION: sorry i forgat to tell you
it's an unrecursive Ackerman function

http://en.wikipedia.org/wiki/Ackerman_function

Answer
the Ackermann function is a general recursive function. And you can implement it with three co-routines (functions which call each other). But the program is much easier to write (and read) if you write it as a single recursive function.

#include<stdio.h>

int A( int m, int n )
{
  // A(m,n) is defined only for non-negetive m, n
  if( ( m<0 ) || (n<0) )
  {
       printf( "error in input, m & n must be non-negetive\n" ) ;
       return -1 ; // error return
  }

  // A(m,n) gives n+1 if m==0
  else if( m == 0 ) return n+1 ;

  // A(m,n) gives A( m-1, 1 ) if m>0 && n == 0
  else if( n == 0 ) return A( m-1, 1 ) ;

  // A(m,n) gives A( m-1, A( m, n-1 ) ) if m>0 && n > 0
  else return A( m-1, A( m, n-1 ) ) ;

}


int main()
{
      int m, n, result ;
      printf("Enter m&n:");
      scanf("%d%d",&m,&n);
      printf("m=%d,n=%d",m,n);

      result = A(m,n) ;
      if( result != -1 )
         printf( "A( %d, %d ) is %d\n", m, n, result ) ;

      return 0 ;
}


> why it doesn't work if for example we enter m=7 & n=7

Its value grows rapidly; even for small inputs, for example A(4,2) contains 19,729 decimal digits. For most inputs, the value would be much, much greater than the maximum value that an int can hold.

Take a look at the table of values given in the same wiki article http://en.wikipedia.org/wiki/Ackerman_function#Table_of_values and construct your input such that the result would not be intractably large.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.