Jump to content

[Solved]giving unexpected output


Recommended Posts

I wrote a simple program but its not giving outut it should give it shows addition of 8 And 2 as 14  :-/ :-/ And all other things are this kinda here's the program please point out mistake.!!

#include<stdio.h>

#include<conio.h>

int i1,i2;

int sum,div,sub,mul;

void input()

{

printf("Enter 1s number:");

scanf("%d",&i2);

printf("Enter 2nd number:");

scanf("%d",&i1);

}

void calculate()

{

    sum=i1+12;

    div=i1/12;

    mul=i1*12;

    sub=i1-12;

}

void display()

{

    printf("\nnumber's you have just input:%d %d",i1,i2);

    printf("\nsum:%d",sum);

    printf("\ndiv:%d",div);

    printf("\nmultiplication:%d",mul);

    printf("\nsubtraction:%d",sub);

}

int main()

{

    input();

  calculate();

  display();

getch();

}

Link to comment
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

no.  I meant when you invoke calculate () function it does all the four functions of adding, multiplying,division and subtraction.

i don't think so dude coz i just declared that the variable sum is the addition of i1 And i2 variable same as mul variable is multiplication of i1 And i2 And they all function when i disply them seperately!!

And if this is a mistake than how to make same program w/o mitsake?

Link to comment
Share on other sites

i don't think so dude coz i just declared that the variable sum is the addition of i1 And i2 variable same as mul variable is multiplication of i1 And i2 And they all function when i disply them seperately!!

And if this is a mistake than how to make same program w/o mitsake?

You can do one of 2 things mentioned below to solve the problem:

1) write sum,multiplication,divsion,subtraction as 4 different functions and invoke each of them separately

or

2) Create a menu type option asking user to enter choice 1,2,3,4 for addition,subtraction,multiplication,division respectively and collect the input in global variable temp.  then in calculate function, put temp in switch case and put case 1= i1+i2;break; 

case 2=i1-i2;break

case 3, case 4 etc.

I hope you understood what I am saying.  I will not provide you corrected program.  Correct it yourself :)

Link to comment
Share on other sites

You can do one of 2 things mentioned below to solve the problem:

1) write sum,multiplication,divsion,subtraction as 4 different functions and invoke each of them separately

or

2) Create a menu type option asking user to enter choice 1,2,3,4 for addition,subtraction,multiplication,division respectively and collect the input in global variable temp.  then in calculate function, put temo in switch case and put case 1= i1+i2;break; 

case 2=i1-i2;break

case 3, case 4 etc.

I hope you understood what I am saying.  I will not provide you corrected program.  Correct it yourself :)

and if i want all 4 things to be performed automatically?Than i have to create 4 different functions and have to call al 4 in main?
Link to comment
Share on other sites

:rofl: dude its more simple.If i have to make a big soft like it has calculations than its much more easy and short program ..this will be a 1 page program and tat will be a 4 page program with same function!

I made this kinda program ~2 years ago, and it was only 1/2 page program :P

I think you havent understood what I mean.

Link to comment
Share on other sites

I made this kinda program ~2 years ago, and it was only 1/2 page program :P

I think you havent understood what I mean.

u meant this na?

#include<stdio.h>

#include<conio.h>

void main()

{

int i1,i2,sum,mul,div,sub;

clrscr();

printf("enter 2 no:");

scanf("%d %d",&i1 &i2);

  sum=i1+12;

    div=i1/12;

    mul=i1*12;

    sub=i1-12;

    printf("\nnumber's you have just input:%d %d",i1,i2);

    printf("\nsum:%d",sum);

    printf("\ndiv:%d",div);

    printf("\nmultiplication:%d",mul);

    printf("\nsubtraction:%d",sub);

getch();

}

most probly u mant this i'm i wrong?or u meant same by case method i'm i wrong?

(note:i didn't compiled the program)

Link to comment
Share on other sites

@CrackUC

There are 2 major mistakes in your program.

First of all in your input function you have taken 1st number in i2 and 2nd number in i1. This is a mistake because 8-2 is not equal to 2-8.

void input()

{

printf("Enter 1s number:");

scanf("%d",&i2);

printf("Enter 2nd number:");

scanf("%d",&i1);

}

And your 2nd mistake is that in your calculate() function instead of i2 you have written 12. Thats why its showing 8+2 as 14. Its only adding 2 with 12. :P

void calculate()

{

    sum=i1+12;

    div=i1/12;

    mul=i1*12;

    sub=i1-12;

}

And if you want to write this program using local variables and functions you can either pass a reference to your local variable and work with it. Or pass variables by value and make the function return a value.

Link to comment
Share on other sites

@CrackUC

There are 2 major mistakes in your program.

First of all in your input function you have taken 1st number in i2 and 2nd number in i1. This is a mistake because 8-2 is not equal to 2-8.

And your 2nd mistake is that in your calculate() function instead of i2 you have written 12. Thats why its showing 8+2 as 14. Its only adding 2 with 12. :P

And if you want to write this program using local variables and functions you can either pass a reference to your local variable and work with it. Or pass variables by value and make the function return a value.

dafuck i missed that..call by reference hmm..i'm weak in pointers okay i'll try!!
Link to comment
Share on other sites

dafuck i missed that..call by reference hmm..i'm weak in pointers okay i'll try!!

Nothing great,  you have to just add '&' symbol before the variable name you are passing to the function.  That's it.  by adding &,  you are passing the location of the variable to the function instead of its value.  Search your text book for some examples :P
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...