warm up about function swap()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idle
    New Member
    • Oct 2006
    • 7

    warm up about function swap()

    #include<stdio. h>

    void swap(int &, int &);
    void main(){

    int num1, num2;

    printf("Enter 2 numbers > ");
    scanf("%d%d", &num1, &num2);

    if(num1 > num2)
    swap(&num1, &num2);

    printf("In ascending order: %d %d \n", num1, *num2);
    }
    void swap(int *a, int b)
    {
    int hold;
    hold = a;
    *a = b;
    b = hold;
    }

    *Function swap() will swap the contents of two variables passed by
    reference to it.
    *Identify error(s) that occur in the program

    gud luck
  • ltgbau
    New Member
    • Sep 2006
    • 41

    #2
    your program after corrected
    TESTED, OK!!!

    Code:
    #include<stdio.h>
    
    void swap(int &, int &);
    void main(){
    
    int num1, num2;
    
    printf("Enter 2 numbers > ");
    scanf("%d%d", &num1, &num2);
    
    if(num1 > num2)
    swap(num1, num2);
    
    printf("In ascending order: %d %d \n", num1, num2);
    }
    void swap(int &a, int &b)
    {
    int hold;
    hold = a;
    a = b;
    b = hold;
    }

    Comment

    • koder
      New Member
      • Sep 2006
      • 23

      #3
      sir,
      above program gives many compilation errors ,listed below
      refer2.c:3: parse error before '&' token
      refer2.c: In function `main':
      refer2.c:4: warning: return type of `main' is not `int'
      refer2.c: At top level:
      refer2.c:16: parse error before '&' token
      refer2.c: In function `swap':
      refer2.c:19: `a' undeclared (first use in this function)
      refer2.c:19: (Each undeclared identifier is reported only once
      refer2.c:19: for each function it appears in.)
      refer2.c:20: `b' undeclared (first use in this function)

      have given the program name as refer2.c
      as given below is a program which i have modified from the original post,

      [HTML]#include<stdio. h>

      void swap(int *, int *);
      int main()
      {

      int num1, num2;

      printf("Enter 2 numbers > ");
      scanf("%d%d", &num1, &num2);

      if(num1 > num2)
      swap(&num1, &num2);

      printf("In ascending order: %d %d \n",num1, num2);
      return 0;
      }
      void swap(int *a, int *b)
      {
      int hold;
      hold =*a;
      *a = *b;
      *b = hold;
      }

      this is tested and working...
      :-)[/HTML]

      Comment

      • ltgbau
        New Member
        • Sep 2006
        • 41

        #4
        i think it depends on compiler :D
        but i forgot return 0; in main function :(
        Originally posted by koder
        sir,
        above program gives many compilation errors ,listed below
        refer2.c:3: parse error before '&' token
        refer2.c: In function `main':
        refer2.c:4: warning: return type of `main' is not `int'
        refer2.c: At top level:
        refer2.c:16: parse error before '&' token
        refer2.c: In function `swap':
        refer2.c:19: `a' undeclared (first use in this function)
        refer2.c:19: (Each undeclared identifier is reported only once
        refer2.c:19: for each function it appears in.)
        refer2.c:20: `b' undeclared (first use in this function)

        have given the program name as refer2.c
        as given below is a program which i have modified from the original post,

        [HTML]#include<stdio. h>

        void swap(int *, int *);
        int main()
        {

        int num1, num2;

        printf("Enter 2 numbers > ");
        scanf("%d%d", &num1, &num2);

        if(num1 > num2)
        swap(&num1, &num2);

        printf("In ascending order: %d %d \n",num1, num2);
        return 0;
        }
        void swap(int *a, int *b)
        {
        int hold;
        hold =*a;
        *a = *b;
        *b = hold;
        }

        this is tested and working...
        :-)[/HTML]

        Comment

        Working...