CrackUC Posted April 22, 2012 Report Posted April 22, 2012 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(); } Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 And guys its said to not use global variables?And if i not global variable how to make this program with local variables?? Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 @crackUC, there is a huge mistake in your program. When you invoke calculate() function, it does all the 4 - i.e addition,division,multiplication,subtraction. Hope you can correct this by yourself Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 @crackUC, there is a huge mistake in your program. When you invoke calculate() function, it does all the 4 - i.e addition,division,multiplication,subtraction. Hope you can correct this by yourself4? where i wrote 4? Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 i took i1 And i2 as variables! Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 4? where i wrote 4? no. I meant when you invoke calculate () function it does all the four functions of adding, multiplying,division and subtraction. Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 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? Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 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 Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 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? Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 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? Yes. Quote
NiharG Posted April 22, 2012 Report Posted April 22, 2012 If its just addition, subtraction, multiplication and Division of the entered numbers, then the program can be made in a much simpler way. Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 @shivam thats not the problem dude i just used sum only still its giving sum if 4 and 3 as 16 Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 If its just addition, subtraction, multiplication and Division of the entered numbers, then the program can be made in a much simpler way.i wwant via function way dude..!!! Quote
NiharG Posted April 22, 2012 Report Posted April 22, 2012 i wwant via function way dude..!!! But if you can make it simple, then why complicated ? Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 But if you can make it simple, then why complicated ? 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! Quote
NiharG Posted April 22, 2012 Report Posted April 22, 2012 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 I think you havent understood what I mean. Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 I made this kinda program ~2 years ago, and it was only 1/2 page program 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) Quote
Anindya1989 Posted April 22, 2012 Report Posted April 22, 2012 @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. 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. Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 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. @Anindya1989, hawk eye ! @ujwal, lol. Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 @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. 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!! Quote
Anindya1989 Posted April 22, 2012 Report Posted April 22, 2012 @Anindya1989, hawk eye ! I'm good at software testing . And this was a tiny program. The smallest programs i deal with in office have atleast 10000 lines. Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 I'm good at software testing . And this was a tiny program. The smallest programs i deal with in office have atleast 10000 lines. OMG.! I do computer programming for fun Quote
CrackUC Posted April 22, 2012 Author Report Posted April 22, 2012 I'm good at software testing . And this was a tiny program. The smallest programs i deal with in office have atleast 10000 lines. dafuck 10,000 lines? And what u test means what kinda programs ur company makes? Quote
shivam94 Posted April 22, 2012 Report Posted April 22, 2012 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 Quote
Recommended Posts
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.