Execution Problem

C, C++, Visual C++, C++.Net Topics
leesandy44
Corporal
Corporal
Posts: 10
Joined: Sat Mar 20, 2010 8:45 am

Execution Problem

Post by leesandy44 » Mon Apr 26, 2010 6:20 pm

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();
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Execution Problem

Post by Neo » Mon Apr 26, 2010 7:23 pm

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();
    }
leesandy44
Corporal
Corporal
Posts: 10
Joined: Sat Mar 20, 2010 8:45 am

Re: Execution Problem

Post by leesandy44 » Thu Apr 29, 2010 5:48 pm

that means there's a special keyword for swapping???
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Execution Problem

Post by Neo » Thu Apr 29, 2010 6:58 pm

No. Not a keyword.
We have written a function void swap(int *a, int *b) for this.
leesandy44
Corporal
Corporal
Posts: 10
Joined: Sat Mar 20, 2010 8:45 am

Re: Execution Problem

Post by leesandy44 » Thu Apr 29, 2010 7:41 pm

okay. thank you Neo. still i haven't learn much functions. thank you so much for the help. :)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Execution Problem

Post by Neo » Thu Apr 29, 2010 8:00 pm

You are always welcome. This is the purpose of ROBOT.LK.
If you have any problem, do not hesitate to submit.
leesandy44
Corporal
Corporal
Posts: 10
Joined: Sat Mar 20, 2010 8:45 am

Re: Execution Problem

Post by leesandy44 » Fri Apr 30, 2010 6:29 pm

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();
}
Last edited by Neo on Fri Apr 30, 2010 6:51 pm, edited 1 time in total.
Reason: Code placed within [CODE] blocks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Execution Problem

Post by Neo » Fri Apr 30, 2010 6:56 pm

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();
}
leesandy44
Corporal
Corporal
Posts: 10
Joined: Sat Mar 20, 2010 8:45 am

Re: Execution Problem

Post by leesandy44 » Sun May 02, 2010 1:45 pm

thank you Neo. Now its working. :)
leesandy44
Corporal
Corporal
Posts: 10
Joined: Sat Mar 20, 2010 8:45 am

Re: Execution Problem

Post by leesandy44 » Wed May 05, 2010 11:31 am

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..
Last edited by Neo on Wed May 05, 2010 5:15 pm, edited 1 time in total.
Reason: Placed the code within CODE blocks
Post Reply

Return to “C/C++ Programming”