Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hey I am writing a simple program that adds fractions.
but whenever i run the program it will not perform the operations.
It only scans the input, but no output.
please spot my error! >>>>>>CANNOT USE FLOATS<<<<<<<<
#include <stdio.h>
int main ( void )
{
int lcd, d1, d2, num1, num2, sum;
printf("Enter the first number:");
printf("Enter the numerator:");
scanf("%d", num1);
printf("Enter the denominator:");
scanf("%d", &d1);
printf("Enter the second number:");
printf("Enter the numerator:");
scanf("%d", &num2);
printf("Enter the denominator:");
scanf("%d", &d2);
for (lcd = d1; lcd % d2 != 0; lcd+= d1);
num1 *= lcd / d1;
num2 *= lcd / d2;
sum = num1 + num2;
printf("%d", sum);
return 0;
}
scanf must receive pointers
scanf("%d", num1) -> scanf("%d", &num1)
Because scanf use call by reference. What is that? please see the following code.
#include <stdio.h>
void foo(int i)
{
i = 1;
}
void bar(int *pi)
{
*pi = 1;
}
int main()
{
int a = 2;
foo(a);
printf("%d", a); /* output is 2 */
bar(&a);
printf("%d", a); /* output is 1 */
}
In foo(a), we're using call by value. That means a is copied when we call foo. the code, i = 1, of foo changes only the copy of a, and does NOT change real value of a.
In bar(&a), we're using call by reference. Can you find the difference? Yes, it's bar(&a), not bar(a). "&" operator gets the pointer of a, and we call bar with the pointer. So pi refers to a, and *pi = 1 changes real value of a successfully.
A real-life example is here: printf and scanf.
int i = 0;
printf("output number : %d\n", i);
scanf("%d", &i); /* input number */
printf doesn't need to change its arguments, so it use call-by-value.
But, scanf receives user's input and change its arguments into user's input. so it use call-by-reference. thus, we should use &i, not i.
Does it need to be that complex? Why not just divide the numerator by the denominator?
Also, your working with integers. I think you want floats.
#include <stdio.h>
int main ( void )
{
int d1, d2, num1, num2;
printf("Enter the first number:");
printf("Enter the numerator:");
scanf("%d", &num1);
printf("Enter the denominator:");
scanf("%d", &d1);
printf("Enter the second number:");
printf("Enter the numerator:");
scanf("%d", &num2);
printf("Enter the denominator:");
scanf("%d", &d2);
printf("%f",
((float) num1 / (float) d1) + ((float) num2 / (float) d2)
);
return 0;
}
If you want to compute the non simplified sum of two fractions, just using the following formula
a/b + c/d = (a*d + c*b)/(b*d)
Therefore, the following code could be used to achieve your goal
#include <stdio.h>
int main ( void )
{
int den1, den2, num1, num2, sum_num, sum_den;
printf("Enter the first number:\n");
printf("\tEnter the numerator:");
scanf("%d", &num1);
printf("\tEnter the denominator:");
scanf("%d", &den1);
printf("Enter the second number:\n");
printf("\tEnter the numerator:");
scanf("%d", &num2);
printf("\tEnter the denominator:");
scanf("%d", &den2);
sum_num = num1 * den2 + num2 * den1;
sum_den = den1 * den2;
printf("sum = %d/%d\n", sum_num, sum_den);
return 0;
}
Related
I wrote up a simple calculator that utilizes functions.
#include<stdio.h>
void operation_menu();
int getNumber(int,int);
int sum(int,int);
int diff(int,int);
int mult(int,int);
double quot(double,int);
int choice,num1,num2;
int main(){
getNumber(num1,num2);
operation_menu();
return 0;
}
int getNumber(int num1,int num2){
printf("Welcome to Simple Calculator!\n");
printf("Please input a number to begin:\n");
scanf("%d", &num1);
printf("Great! Now input the second number:\n");
scanf("%d", &num2);
}
void operation_menu(){
printf("Select an operation: \n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("The sum of %d and %d is %d.", num1,num2,sum(num1, num2));
break;
case 2:
printf("The difference of %d and %d is %d.", num1,num2,diff(num1, num2));
break;
case 3:
printf("The product of %d and %d is %d.", num1,num2,mult(num1, num2));
break;
case 4:
printf("The quotient of %d and %d is %.2lf.", num1,num2,quot(num1, num2));
break;
default:
printf("Please try again.");
}
}
int sum(int num1,int num2){
int answer;
answer=num1+num2;
return answer;
}
int diff(int num1,int num2){
int answer;
answer=num1-num2;
return answer;
}
int mult(int num1,int num2){
int answer;
answer=num1*num2;
return answer;
}
double quot(double num1,int num2){
double answer;
answer=num1/num2;
return answer;
}
The program worked correctly without breaking up my main() into several smaller functions, but after creating functions for each operation, input, and menu, I receive the wrong outputs. I figured this by printing num1 and num2 after using scanf and sure enough, I get the wrong outputs.
The function getNumber changes its local variables (parameters) num1 and num2. After exiting the function these local variables are not alive. The global variables with the same names stay unchanged.
You need to pass the global variables by reference through pointers to them. For example
int getNumber(int *num1,int *num2){
printf("Welcome to Simple Calculator!\n");
printf("Please input a number to begin:\n");
scanf("%d", num1);
printf("Great! Now input the second number:\n");
scanf("%d", num2);
}
The function is called like
getNumber( &num1, &num2 );
Pay attention to that there is no need to declare the variables num1, num2 and choice as global. You should declare them in the scope where they are used for example within the function operation_menu.
Also the both parameters of the function quot should have the type int. The function can be defined like
double quot( int num1,int num2){
double answer;
answer = ( double )num1/num2;
return answer;
}
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 1 year ago.
I was doing some amateur C practice. As you can see I'm trying to make a slightly more advanced calculator that allows the user to decide what they want to do. Even though I clearly defined what to write in if() function, it wont work at all. I would type in "add" or "mul" in the console but it always returns, "type a given mathematical function"
which is an outcome that I put in there to tell the user that they've given the wrong input. So far I've tried adding and removing the quotation mark in if() and replaced == with =. None of these work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply ");
scanf_s("%d", &mfunc);
if (mfunc == "add") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == "sub") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == "div") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == "mul") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
you can't compare "add" with myfunc. add is a string (array of chars of size 4), myfunc is an int. In C you can't compare int to string. you can compare string to string so you can ask from the user to input his func by write "add", but it is a bit more complex for you right now (you need to scan it to a proper array of chars and compare the strings using function which might be case sensitive, etc.). also, you can use instruction for the user - some kind of map so the user could insert 0 for add, 1 for sub and so on..
also, I used "define" witch is macro (an pre-processor directive) to more readable code;
#include <stdio.h>
#include <stdlib.h>
#define ADD 0
#define SUB 1
#define DIV 3
#define MUL 4
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply \n");
printf("to add insert 0,\nto subtract insert 1,\n");
printf("to divide insert 2,\nto multiply insert 3\n");
scanf("%d", &mfunc);
if (mfunc == ADD) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == SUB) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == DIV) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == MUL) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
if you really want the user to input string you should be do that:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int num1;
int num2;
char mfunc[256];
printf("type in a mathematical function: add, subtract, divide or multiply \n");
gets(mfunc);
if (strcmp(mfunc, "ADD") == 0||strcmp(mfunc, "add") == 0) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (strcmp(mfunc, "SUB") == 0||strcmp(mfunc, "sub") == 0) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if ((strcmp(mfunc, "DIV") == 0||strcmp(mfunc, "div") == 0)) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if ((strcmp(mfunc, "MUL") == 0||strcmp(mfunc, "mul") == 0)) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
I know I used gets(), see paul-kapustin in this Q.
How to read a line from the console in C?
Program 1
int main() {
int p, t;
float r;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", prt / 100);
return 0;
}
Program 2
#include<stdio.h>
int main() {
int p, t;
float r, interest;
interest = prt / 100;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", interest);
return 0;
}
Let's break down the task of program 2.
You're trying to find out the value of "interest" where interest = ( p * r * t ) / 100.
First, you need the value of 3 variables which are 'p', 'r', 't'.
You're taking those values from user (using scanf).
You'll be able to get the value of "interest" after taking all those inputs.
So you'll have to assign values in the variable "interest" according to the equations right after you're done with taking those inputs.
That means at the end of the program.
Another thing, when you're writing an equation, you'll have to write down corresponding operators too.
#include<stdio.h>
int main() {
int p, t;
float r, interest;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
interest = (p * r *t) / 100.0;
printf("The simple interest is %f", interest);
return 0;
}
This question already has answers here:
I get implicit declaration of function strncmp
(2 answers)
Closed 3 years ago.
Creating a simple code that scans two numbers, asks the user if they would like to add or multiply them, and then performs the operation and prints the output.
#include <stdio.h>
int main(){
int num1;
int num2;
char oper[] = "";
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Would you like to add or multiply these numbers? ");
scanf("%s", &oper);
if(strcmp(oper, "multiply") == 0){
int prod = num1 * num2;
printf("The product is %d", prod);
}
else if(strcmp(oper, "add") == 0){
int sum = num1 + num2;
printf("The sum is %d", sum);
}
else{
printf("Why would you input something that you knew wouldn't work?");
}
return 0;
}
You are using strcmp function without declaring them. You need to include the header file which contains strcmp function declaration.
Use:
#include <string.h>
What is going wrong here? I am getting the error Use of undeclared identifier 'answer'
Here's my code:
if (CalculatorChoice == 1) do {
int a;
int b;
int sum;
char answer;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
if (CalculatorChoice == 1)
do {
/* ... */
char answer;
/* ... */
} while(answer=='y');
You have declared the variable answer inside the loop block, but it is accessed from the while condition, which is outside the loop block.
You can move the declaration outside the loop body to fix this:
if (CalculatorChoice == 1) {
char answer;
do {
/* ... */
} while(answer=='y');
}
Your version has variable declared inside block, is not accessible out of block
This code changes position
if (CalculatorChoice == 1)
{
char answer;
do {
int a;
int b;
int sum;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
}