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

warm up about function swap()

P: 7
#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
Oct 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: 41
your program after corrected
TESTED, OK!!!

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. void swap(int &, int &);
  4. void main(){
  5.  
  6. int num1, num2;
  7.  
  8. printf("Enter 2 numbers > ");
  9. scanf("%d%d", &num1, &num2);
  10.  
  11. if(num1 > num2)
  12. swap(num1, num2);
  13.  
  14. printf("In ascending order: %d %d \n", num1, num2);
  15. }
  16. void swap(int &a, int &b)
  17. {
  18. int hold;
  19. hold = a;
  20. a = b;
  21. b = hold;
  22. }
Oct 3 '06 #2

P: 23
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]
Oct 3 '06 #3

P: 41
i think it depends on compiler :D
but i forgot return 0; in main function :(
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]
Oct 3 '06 #4

Post your reply

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