#include <stdio.h>
int main()
{
printf("choose number");
c();
}
c()
{
printf("1. ax+b=0\n\n");
printf("2. ax+by+c=0\n dx+ey+f=0\n\n");
int n;
scanf("%d", &n);
if (n > 3)
wrong();
if (n == 1)
formula1();
if (n == 2)
formula2();
if (n == 3)
;
formula3();
}
wrong()
{
printf("Please choose a number between 1 and 3.\n\n");
c();
}
formula1()
{
printf("ax+b=0\n");
printf("Enter your values for a and b respectively, seperated by commas\n");
float a, b, x;
scanf("%f,%f,%f", &a, &b);
x = -b / a;
printf("x=-b/a\n");
printf("=>x=%f", x);
question();
}
formula2()
{
printf("ax+by+c=0\n\ndx+ey+f=0\n");
printf(
"Enter your values for a, b, c, d ,e and f respectively, seperated by commas\n");
float a, b, c, d, e, f, x, y;
scanf("%f,%f,%f,%f,%f,%f", &a, &b, &c, &d, &e, &f);
x = ((f * b) - (c * e)) / ((a * e) - (d * b));
y = ((c * d) - (f * a)) / ((e * a) - (d * b));
printf("=>x=%f", x);
printf("\n\n");
printf("=>y=%f", y);
question();
}
question()
{
char t;
printf("\n\nanother equation?\ny/n?\n");
if (t == 'y')
{
printf("\n\n\n\n\n");
c();
}
else if (t != 'n')
question();
}
I have this code, which in short solves 3 equations.
When you select any choice it seems to run the question method multiple times then quits due to a segmentation fault: 11
Could someone please point out where I am going wrong. Any other help with my code would be greatly appreciated
Here is one problem:
scanf("%f,%f,%f",&a, &b);
Only two arguments are supplied for the three values.
There is no input function like scanf() in question() and so if t is not 'y' or 'n' by chance, you get an endless recursion until stack size is exceeded ...
Related
I am first time poster here. Like the tittle says I need to print all odd numbers via a recursive function. The problem is that I have created a simple program that does that, but when it reaches 1(which should be the point where the program stops) the program crashes and I honestly do not see where is the problem. My professor said that I forgot to put a return somewhere, but I honestly do not know where. So if someones can point out the problem that would be great(ps. I am using Code::Blocks as my IDE).
int main() {
int a, b;
printf("Unesi neki broj: \n");
scanf("%d", &a);
b = koko(a);
printf(b);
}
int koko(int a) {
if (a == 1) {
return a;
}
if (a % 2 != 0) {
printf("Ovaj broj je neparan: %d \n", a);
}
koko(a - 1);
}
First, you have to declare the function koko; for using it in main function.
int koko(int a);
Secondly, printf(b) need to define the type to print out:
printf("%d\n", b);
Finally, Using return koko(a - 1); instead of koko(a-1) because this function has to return an int value.
Then, the complete code:
#include <stdio.h>
#include <stdlib.h>
int koko(int a);
int main() {
int a, b;
printf("Unesi neki broj: \n");
scanf("%d", &a);
b = koko(a);
printf("%d\n", b);
return 0;
}
int koko(int a) {
if (a == 1) {
return a;
}
if (a % 2 != 0) {
printf("Ovaj broj je neparan: %d \n", a);
}
return koko(a - 1);
}
enter image description here
int koko(int a)
{
if (a % 2 != 0)
{
printf("Ovaj broj je neparan: %d \n", a);
}
if (a == 1) {
return a;
}
koko(a - 1);
}
int main()
{
int a, b;
printf("Unesi neki broj: \n");
scanf("%d", &a);
b = koko(a);
printf(" koko(a) return %d", b);
return 0;
}
//Enter a 4-digit integer n from the keyboard, and write a program to divide it into two 2-digit integers a and B. Calculate and output the results of the addition, subtraction, multiplication, division and redundancy operations of the split two numbers. For example, n=-4321, if the two integers after splitting are a and b, then a=-43 and b=-21. The result of division operation requires that it be precise to 2 decimal places, and the data type is float. Redundancy and division operations need to take into account the division of 0, that is, if the split B = 0, then output the prompt information "The second operator is zero!"
//Failure to pass the test,how should i fix
#include<stdio.h>
#include<math.h>
int main()
{
int x, a, b;
printf("Please input n:\n");
scanf("%d", &x);
a = x / 100;
b = x % 100;
printf("%d,%d\n", a, b);
printf("sum=%d,sub=%d,multi=%d\n", a + b, a - b, a*b);
if (b == 0)
printf("The second operater is zero!");
else
printf("dev=%.2f,mod=%d\n", (float)a / b, a%b);
}
You forgot to check that x is a 4-digits number. So if the input is 12345 or 123 you don't satisfy the requirement.
#include <stdio.h>
int main()
{
int x, a, b;
int passed = 0;
// Enter a 4 digits number: ABCD
do {
printf("Enter X = ");
scanf("%d", &x);
passed = (x >= 1000 && x <= 9999) || (x >= -9999 && x <= -1000);
} while (!passed);
a = x / 100;
b = x % 100;
printf("Numbers: %d %d \n", a, b);
printf("Sum = %d \n", a + b);
printf("Sub = %d \n", a - b);
printf("Mul = %d \n", a * b);
if (0 == b) {
printf("Div by Zero \n");
} else {
printf("Div = %f \n", (double)a / b);
printf("Mod = %d \n", a % b);
}
return 0;
}
I wrote this code to find the roots of a quadratic equation.It seems to work unless I put a space before the scanning part. (for example in the input I write: a=1,b=2, c=1). In that case it just deletes the variable. (In the example I gave c would be deleted from the input and as such the answers I receive are 0,-2 instead of -1.). The code works if I put the space somewhere else (a =1,b= 2,c=1).
How can I fix this problem?
int main()
{
double root1 = 0, root2 = 0;
double a = 0., b = 0., c = 0.;
printf("Enter a polynomial:\n");
scanf("a=%lf,b=%lf,c=%lf", &a, &b, &c);
{
if (a == 0)
{
return 0;
}
if ((b * b - 4 * a * c) == 0)
{
root1 = -b / 2 * a;
printf("root is %f", root1);
}
else if ((b * b - 4 * a * c) < 0)
{
printf("There are no roots");
}
else {
root1 = (-b + sqrt(b * b - 4 * a * c)) / 2* a;
root2 = (-b - sqrt(b * b - 4 * a * c)) / 2* a;
printf("Roots are %f,%f", root1, root2);
}
}
return 0;
}
Use a format the allows for space in unexpected places. And check results.
// v v v v v v v v
if (scanf(" a =%lf , b =%lf , c =%lf", &a, &b, &c) == 3) Oh_Happy_day();
A space is not needed before "%lf" as that specifier will scan through leading white-space. Some like to be explicit
// x x x Optional
if (scanf(" a = %lf , b = %lf , c = %lf", &a, &b, &c) == 3) Oh_Happy_day();
Still better to use fgets() to read a line and then parse it.
there is no way to fix this problem; this is actually a problem with the function scanf(); itself. Alternately, you can use fgets(); instead.
try replacing
double a=0.,b=0.,c=0.;
printf("Enter a polynomial:\n");
scanf("a=%lf,b=%lf,c=%lf",&a,&b,&c);
with
double a=0.0,b=0.0,c=0.0;
printf("Enter a polynomial:\n");
scanf(" %lf, %lf, %lf",&a,&b,&c);
Because the space is not part of the formatting string you used in scanf.
You should add the leading space in the formatting string:
scanf(" a=%lf, b=%lf, c=%lf", &a, &b, &c);
I've a problem with a function in my calculator-program. The function returns me back only 0 values. I want that:
Input:3+4
Output:7
I have worked with pointers to use the call by reference method. Please give me some tips.
The mistake is in the function readcalc. If I write the whole syntax in the main program it works.
Here is my code.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
while (restart != 'N')
{
readcalc(&a,&op,&b);
printf("%lf%lf", a, b);
printf("\n ---CALCULATOR--- \n\n\n");
switch (op)
{
case '+':printf("%lf + %lf = %lf\n", a, b, addition(a, b)); break;
case '-':printf("%lf - %lf = %lf\n", a, b, subtraction(a, b)); break;
case '*':printf("%lf * %lf = %lf\n", a, b, multiplication(a, b)); break;
case '/':printf("%lf / %lf = %lf\n", a, b, division(a, b)); break;
default:printf("bad operator!");
}
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", &restart);
if (restart != 'Y'&&restart != 'N')
{
printf("Bad input!");
}
}
fflush(stdin);
getchar();
return 0;
}
double readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", &x, &opp, &y);
return 0;
}
double addition(double a,double b)
{
double c = 0;
c = a + b;
return c;
}
double subtraction(double a, double b)
{
double c = 0;
c = a - b;
return c;
}
double multiplication(double a, double b)
{
double c = 0;
c = a*b;
return c;
}
double division(double a, double b)
{
double c = 0;
c = a / b;
return c;
}
What can I change?
The problem with readcalc is that you pass the pointer to the pointer as argument to scanf. The variables are already pointers, so you don't have to use the address-of operator to get a pointer, as then you get pointers to the pointers.
Also be careful with the scanf format, as the "%c" format doesn't skip leading whitespace, so if you enter e.g. 1 +2 then the scanf call will not be able to read the operator or the second number.
void readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", x, opp, y); // <== no &'s
}
Your readcalc function invokes
scanf("%lf%c%lf", &x, &opp, &y);
on pointers, int *x, etc...
When you place it inline in main, it works on the addresses of the variables.
Change it to
scanf("%lf%c%lf", x, opp, y);
At a suitable warning level, you may have seen warning.
While you are there, readcalc returns a double, which you never use. Perhaps it should simply be void?
Here is a program that I'm trying to fix. I entered 1,5,6 and there should be 2 solutions and it said only 1 solution exists. I'm also trying to make it display decimal values(should I use double?). Below is my code, What am I doing wrong?
#include <stdio.h>
#include <math.h>
int main(void)
{
int inputs[3], a, b, c, d, x, x1, x2, i, lastDigit;
char *os, *noSol = "No solution\n", *cont = 'y';
while (cont == 'Y' || cont == 'y')
{
printf("This program solves a quadratic equation\n");
for (i = 1; i <= 3; i++)
{
lastDigit = i % 10;
if (i >= 4 && i <= 20)
os = "th";
if (i == 1 || lastDigit == 1)
os = "st";
else if (i == 2 || lastDigit == 2)
os = "nd";
else if (i == 3 || lastDigit == 3)
os = "rd";
else
os = "th";
printf("Enter your %d%s number: ", i, os);
scanf("%d", &inputs[i - 1]);
}
a = inputs[0];
b = inputs[1];
c = inputs[2];
while (1)
{
if (a == 0)
{
if (b == 0)
{
printf(noSol);
break;
}
else
{
x = -c / b;
printf("The equation is not quadratic and the solution is %d\n", x);
break;
}
}
else
{
d = pow(b, 2) - 4 * a * c;
if (d < 0)
{
printf(noSol);
break;
}
else if (d == 0)
{
x1 = -b / 2 * a;
printf("One solution: %d\n", x1);
break;
}
else if (d > 0)
{
x1 = (-b + sqrt(d)) / 2 * a;
x2 = (-b - sqrt(d)) / 2 * a;
printf("Two solutions: %d and %d\n", x1, x2);
break;
}
}
}
printf("Run program second time? ( Y / N )\n");
scanf("%s", &cont);
}
getch();
}
Many issues
The math part should use double (or float) instead of int.
double inputs[3], a, b, c, d, x, x1, x2;
printf() & scanf() for double, format specifier needs to change from %d to %le (or the like) to match double.
Math error: in 3 places, / 2 * a; should be / (2 * a);
char *cont = 'y' should be char cont[2] = "y"
scanf("%s", &cont); should be scanf("%1s", cont);.
Error handling: the return value of scanf() should be checked as in
if (1 != scanf("%lf", &inputs[i - 1])) { ; /* Handle error */ }
Minor math: if (d == 0) case results in a "double root", not a single solution. Practically speaking, given floating point math rounding, one does not always know d should have been mathematically exactly zero and thus the "single" root is really 2 very close roots. Further, with select values, the "Two solutions" will have the same value should sqrt(d) be much much smaller than b.