Posted April 22, 201212 yr comment_30807 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(); }
April 22, 201212 yr Author comment_30808 And guys its said to not use global variables?And if i not global variable how to make this program with local variables??
April 22, 201212 yr comment_30817 @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
April 22, 201212 yr Author comment_30819 @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?
April 22, 201212 yr comment_30822 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.
April 22, 201212 yr Author comment_30823 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?
April 22, 201212 yr comment_30828 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
April 22, 201212 yr Author comment_30829 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?
April 22, 201212 yr comment_30830 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.
April 22, 201212 yr comment_30835 If its just addition, subtraction, multiplication and Division of the entered numbers, then the program can be made in a much simpler way.
April 22, 201212 yr Author comment_30837 @shivam thats not the problem dude i just used sum only still its giving sum if 4 and 3 as 16
April 22, 201212 yr Author comment_30838 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..!!!
April 22, 201212 yr comment_30839 i wwant via function way dude..!!! But if you can make it simple, then why complicated ?
April 22, 201212 yr Author comment_30843 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!
April 22, 201212 yr comment_30846 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.
April 22, 201212 yr Author comment_30849 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)
April 22, 201212 yr comment_30852 @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.
April 22, 201212 yr comment_30853 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.
April 22, 201212 yr Author comment_30856 @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!!
April 22, 201212 yr comment_30857 @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.
April 22, 201212 yr comment_30858 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
April 22, 201212 yr Author comment_30859 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?
April 22, 201212 yr comment_30860 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
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.