Page 1 of 2

Execution Problem

Posted: Mon Apr 26, 2010 6:20 pm
by leesandy44
can anyone help me to write a c program to swap two numbers? i wrote the following program. at the compilation its not making any errors, but at the execution its not giving the correct output. actually its not swapping the numbers. Please help me to find the error.

Code: Select all

#include<conio.h>
void main()
{
   int a; int b; int temp;
   a=0, b=0;
   clrscr();
   printf("Input two numbers\n');
   scanf("%d%d",&a,&b);
   temp=a;
   b=a;
   b=temp;
   printf("%d%d",a,b,);
   getch();
}

Re: Execution Problem

Posted: Mon Apr 26, 2010 7:23 pm
by Neo
The corrected program can be found below.

Code: Select all

    #include<conio.h>
    void main()
    {
       int a; int b; int temp;
       a=0, b=0;
       clrscr();
       printf("Input two numbers\n');
       scanf("%d%d",&a,&b);
       temp=a;
       a=b;
       b=temp;
       printf("%d%d",a,b,);
       getch();
    }
Note a=b;, you used b=a so it is a simple bug.
When you define variables in a single line you could use as below (you may also set initial values).

Code: Select all

int a = 0, b = 0, temp;

However the swap can be written more meaningfully using pointers as below.

Code: Select all

    #include <stdio.h>

    void swap(int *a, int *b){

       int temp;

       temp = *a;
       *a = *b;
       *b = temp;
    }

    void main()
    {
       int a = 0, b = 0, temp;
       clrscr();
       printf("Input two numbers\n');
       scanf("%d%d", &a, &b);

       swap( &a, &b);

       printf("%d%d",a,b,);
       getch();
    }

Re: Execution Problem

Posted: Thu Apr 29, 2010 5:48 pm
by leesandy44
that means there's a special keyword for swapping???

Re: Execution Problem

Posted: Thu Apr 29, 2010 6:58 pm
by Neo
No. Not a keyword.
We have written a function void swap(int *a, int *b) for this.

Re: Execution Problem

Posted: Thu Apr 29, 2010 7:41 pm
by leesandy44
okay. thank you Neo. still i haven't learn much functions. thank you so much for the help. :)

Re: Execution Problem

Posted: Thu Apr 29, 2010 8:00 pm
by Neo
You are always welcome. This is the purpose of ROBOT.LK.
If you have any problem, do not hesitate to submit.

Re: Execution Problem

Posted: Fri Apr 30, 2010 6:29 pm
by leesandy44
Neo I wrote the following program. But it has a compilation error. It says "Misplaced else". could you please help me to correct this???

Code: Select all

#include<conio.h>
void main()
{
    int m;
    m=0;
    clrscr();
    printf("input module mark");
    scanf("%d"\n",&m);
    if(m>=50);
    {
       printf("you are pass");
    }
    else
    {
        printf("you are fail");
     }
    getch();
}

Re: Execution Problem

Posted: Fri Apr 30, 2010 6:56 pm
by Neo
Remove the semicolon in red colour. This will tell the compiler that if command ends with semicolon so the else is not valid.

#include<conio.h>
void main()
{
int m;
m=0;
clrscr();
printf("input module mark");
scanf("%d"\n",&m);
if(m>=50);
{
printf("you are pass");
}
else
{
printf("you are fail");
}
getch();
}

Re: Execution Problem

Posted: Sun May 02, 2010 1:45 pm
by leesandy44
thank you Neo. Now its working. :)

Re: Execution Problem

Posted: Wed May 05, 2010 11:31 am
by leesandy44
please help me to find the error of the following program...

Code: Select all

#include<conio.h>
void main()
{
	int i,j;
	for(j=1;j<10;j++);
	{
		if(j%3==0)
		{
			continue
		}
		printf("j=%d",j);
	}
	getch();
}

i wanna know more about continue keyword. please send me some sites or some details..