I'm trying to print a quadratic equation.
But if one of the coefficients is zero, then avoid print it and if one of them is 1, also print it without the number one.
I started to do it with if else but there are too much combinations.
double a, b, c;
printf("The quadratic equation is\n");
if (a == 0 && b != 0 && c != 0)
printf("%.2lfX + %.2lf = 0\n", b, c);
else if (b == 0 && a != 0 && c != 0)
printf("%.2lfX^2 + %.2lf = 0\n", a, c);
else if (c == 0 && a != 0 && b != 0)
printf("%.2lfX^2 + %.2lfX = 0\n", a, b);
else if (a == 0 && b == 0 && c !=0)
printf("%.2lf = 0\n", c);
else if (b == 0 && c == 0 && a != 0)
printf("%.2lfX^2 = 0\n", a);
else if (a == 0 && c == 0 && b != 0)
printf("%.2lfX = 0\n", b);
else if (a == 0 && b == 0 && c == 0)
printf("");
else if (a == 1 && b != 1 && c != 1) {
printf("X^2 + %.2lfX + %.2lf = 0\n", b, c);
}
else if (b == 1 && a != 1 && c != 1) {
printf("%.2lfX^2 + X + %.2lf = 0\n", a, c);
}
else if (c == 1 && a != 1 && b != 1) {
printf("%.2lfX^2 + %.2lfX + 1 = 0\n", a, b);
}
else if (a == 1 && b == 1 && c != 1) {
printf("X^2 + X + %.2lf = 0\n", c);
}
else if (b == 1 && c == 1 && a != 1) {
printf("%.2lfX^2 + X + 1 = 0\n", a);
}
else if (a == 1 && c == 1 && b != 1) {
printf("X^2 + %.2lfX + 1 = 0\n", b);
}
else if (a == 1 && b == 1 && c == 1) {
printf("X^2 + X + 1 = 0\n");
}
else {
printf("%.2lfX^2 + %.2lfX + %.2lf = 0\n", a, b, c);
}
There must be a better way for doing it.
Now, I have to handle with cases like one of them is 0 and one of them is 1.
Please help me.
I'm trying to do it shorter.
You print each term in multiple parts with multiple printf() calls each. Make this into a function and you can easily extend it for larger equations.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool printTerm(double coeff, int degree, bool isFirst) {
// If this coefficient is 0, print nothing
if (coeff == 0 && degree != 0) {
return true;
}
// Handle minus sign
if (coeff < 0) {
printf(" - ");
}
// Print "+" only if some previous terms were printed
else if (!isFirst) {
printf(" + ");
}
// Print the coefficient
if (coeff != 1 || degree == 0) {
// Minus sign was already printed, so fabs() is needed here
printf("%.2lf", fabs(coeff));
}
// Print "X" and the degree
if (degree >= 1) {
printf("X");
if (degree > 1) {
printf("^%d", degree);
}
}
// Return false if something was printed
return false;
}
int main() {
double a = 4.0, b = 1.0, c = 0.0;
// Once printTerm returns false (something was printed), isFirst is set false
bool isFirst = true;
isFirst &= printTerm(a, 2, isFirst);
isFirst &= printTerm(b, 1, isFirst);
isFirst &= printTerm(c, 0, isFirst);
printf(" = 0\n");
}
You can print the terms separately after each other. Be careful when comparing floating point numbers. As mentioned in the comments, some floating point numbers are represented exactly for example 0.0 and 1.0 but others are not (so the comparison a == 1.3 will not work as one might expect).
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
double a = 1.0, b = 1.0, c = 1.0;
bool print_term(char *t, double coeff, bool needsign) {
if (coeff != 0.0) {
if (needsign && coeff > 0.0) {
printf("+");
}
if (coeff != 1.0 || strcmp("", t) == 0) // also print c if it is 1.0
printf("%.2lf%s", coeff, t);
else
printf("%s", t);
}
return coeff != 0.0;
}
int main() {
bool need_operator = false;
printf("The quadratic equation is\n");
need_operator |= print_term("X^2", a, need_operator);
need_operator |= print_term("X", b, need_operator);
need_operator |= print_term("", c, need_operator);
printf("\n");
return 0;
}
Code tested with onlinegdb.com
Related
I am try to implement a program to do this type of calculation in which i have to use only some limited system calls
fork
exec* family
str* family
ato* family
printf, sprintf
round
The program name is double.c and the calculation looks like
./double square 3
output:36 as square(double(3))
I am using exit(result) to returning the result but as the range it is capable of is 0-255 but when the number exeeds from 255 it gives wrong result I have to impement this program just using these system api calls so i think pipe and munmap cannnot be used but how i can return bigger values than using just these functions
What i tried is
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <math.h>
#include <sys/wait.h>
#include <stdlib.h>
#define EPSILON 0.001
double simple_abs(double x)
{
return x > 0 ? x : -x;
}
double simple_sqrt(double x)
{
double previous = 0;
double guess = x;
while (simple_abs(guess - previous) > EPSILON)
{
previous = guess;
guess = previous - (previous * previous - x) / (2 * previous);
}
return guess;
}
int lop(int status) {
return (status >> 8) & 0xff;
}
int square(int n) {
printf("number = %d and square = %d\n", n , n*n);
return n * n;
}
int root(int n) {
return (int) simple_sqrt((double) n);
}
int doubleVal(int n) {
return n * 2;
}
int main(int argc, char *argv[]) {
int value = atoi(argv[argc - 1]);
int result = value;
if (argc > 2){
result = doubleVal(result);
for (int i = 1; i < argc - 1; i++) {
int pid = fork();
if (pid == 0) {
if (*argv[i] == 's' && *(argv[i]+1) == 'q' && *(argv[i]+2) == 'u' && *(argv[i]+3) == 'a' && *(argv[i]+4) == 'r' && *(argv[i]+5) == 'e' && *(argv[i]+6) == '\0'){
result = square(result);
} else if (argv[i][0] == 'r' && argv[i][1] == 'o' && argv[i][2] == 'o' && argv[i][3] == 't' && argv[i][4] == '\0') {
result = root(result);
} else if (argv[i][0] == 'd' && argv[i][1] == 'o' && argv[i][2] == 'u' && argv[i][3] == 'b' && argv[i][4] == 'l' && argv[i][5] == 'e' && argv[i][6] == '\0') {
result = doubleVal(result);
} else {
printf("Unknown operation: %s\n", argv[i]);
exit(1);
}
printf("Child result: %d\n", result);
//return result;
exit(result);
} else {
int status;
wait(&status);
//result = (status >> 8) & 0xff;
result = lop(status);#lop is working as WEXITSTATUS system api
}
}
}
else {
result = doubleVal(result);
}
printf("Result: %d\n", result);
return result;
}
Is there any way to do this to get correct result because when i try ./double square 8 it give wrong result as the result exceeds from 255 .
I think I am trapped with if/else if statements. Program to define what the type of the triangle is.
I don't understand, why it doesn't work properly. And, if it wouldn't be difficult, can you show how to optimize the work of right-angled triangle, using the Pythagorean theorem? In order to not mix the right-angled triangle with other triangles.
Code:
int main() {
int a = 3;
int b = 4;
int c = 5;
int angle_A = 100;
int angle_B = 10;
int angle_C = 70;
if (a == b && a == c && c == b) {
printf("Equilateral triangle\n");
}
else if (a == c || b == c || a == b) {
printf("isosceles triangle.\n");
}
if ((pow(c, 2) == pow(b, 2) + pow(a, 2)) || (pow(a, 2) == pow(b, 2) || pow(c, 2)) || (pow(b, 2) == pow(c, 2) + pow(a, 2))) {
printf("right-angled triangle.\n");
}
if ((angle_A < 90 || angle_B < 90 || angle_C < 90) && angle_A + angle_B + angle_C == 180) {
printf("acute-angled triagle.\n");
}
if ((angle_A > 90 || angle_B > 90 || angle_C > 90) && angle_A + angle_B + angle_C == 180) {
printf("An obtuse triangle.\n");
}
return 0;
}
The output of this code:
right-angled triangle.
acute-angled triangle.
An obtuse triangle.
You have some mistakes, here are the details:
The lengths should be double not int.
You can't define both lengths and angles, as you can get the angles from the lengths.
Get the longest length of the three, then check with Pythagorean theorem.
int main() {
double t;
double a = 3;
double b = 4;
double c = 5;
if (a == b && a == c)
printf("Equilateral triangle\n");
else if (a == c || b == c || a == b)
printf("isosceles triangle.\n");
if (a < b)
{
t = a;
a = b;
b = t;
}
if (a < c)
{
t = a;
a = c;
c = t;
}
if (pow(a, 2) == pow(b, 2) + pow(c, 2))
printf("Right-angled triangle.\n");
else if (pow(a, 2) < pow(b, 2) + pow(c, 2))
printf("Acute triagle.\n");
else
printf("Obtuse triangle.\n");
return 0;
}
#include<stdio.h>
int main() {
int a = 3;
int b = 4;
int c = 5;
int angle_A = 100;
int angle_B = 10;
int angle_C = 70;
if(angle_A+angle_B+angle_C==180){
if (angle_A==60 && angle_B==60 && angle_C==60) {
printf("Equilateral triangle\n");
}
else if (a==c || b==c || a==b) {
printf("isosceles triangle.\n");
}
///////If one of the angle is 90 then its a right angled triangle///////
if (angle_A==90||angle_B==90||angle_C==90){
printf("right-angled triangle.\n");
}
////If all angles are less than 90 then its an acute angled triangle/////
if (angle_A < 90 && angle_B < 90 && angle_C < 90) {
printf("acute-angled triangle.\n");
}
////If one of the angle is greater than 90 degree then its an obtuse angled triangle////
if (angle_A > 90 || angle_B > 90 || angle_C > 90) {
printf("An obtuse triangle.\n");
}
}
return 0;
}
If an angle is 90 that's it.
Then its a right angled triangle.
For my Intro. to programming exam review I was asked to write a program which uses a function to calculate the gcd of a set of numbers. I wrote the following code, which sometimes seems to work fine, but others returns Floating Point Exception 8. I was hoping someone could shed some light.
I complied it using clang gcd.c -o gcd on the mac terminal using macOS High Sierra and the numbers that return the FP error were 5372 18960 -230048 1185 16486
This is the code:
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b){
if((a==0) || (b==0)){
return 0;
}else{
if( a % b == 0 ){
return b;
}else{
if ( b % a == 0 ){
return a;
}
}
}
while( (a != 0) && (b != 0) ){
if (abs(a)>abs(b)){
a = abs(a) % abs(b);
}
if(b>a){
b = abs(b) % abs(a);
}
}
if (a == 0){
return b;
}else{
return a;
}
}
int main(void){
int n;
printf("Please enter the number of integers in your array:\n");
scanf("%d", &n);
int a[n];
printf("Please enter the numbers in your arrray:\n");
for (int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
for (int i = 0; i < (n-1); ++i){
a[i] = gcd(a[i], a[i+1]);
}
printf("The gcd of the %d numbers is %d .\n", n, a[n-2]);
return 0;
}
Looks like couple of mistakes in your code for finding GCD.
You should update the value of a[i+1] in your for loop instead of a[i]. And the GCD will be a[n-1]th element after this change. As you iterate over the loop a[i] and a[i+1] will be original (input) values in your case. So if everything else works fine, your result will be GCD of last two elements of the array (a[n-2], a[n-1]).
for (int i = 0; i < (n-1); ++i) {
a[i+1] = gcd(a[i], a[i+1]);
}
In the while loop of gcd(), you need to make the following changes. Check for a==b conditionality and change the two if conditions to if-else conditions. In case b is a factor of a, a becomes 0 in your first if condition. Then in the second condition, you are doing % 0 which is throwing the error.
while( (a != 0) && (b != 0) ){
if (abs(a)>=abs(b){
a = abs(a) % abs(b);
}
else if(abs(b)>abs(a)){
b = abs(b) % abs(a);
}
}
first impression in the while loop below
while( (a != 0) && (b != 0) ){
if (abs(a)>abs(b)){
a = abs(a) % abs(b); // value of a is altered and reused
}
if(b>a){
b = abs(b) % abs(a); // here <-- and could very well be a 0
}
}
on a completely different note your could remove else {} blocks, if you could take a moment and see that else blocks are not really adding any value because there is a return in if
int gcd(int a, int b){
/** sanity checks */
if((a==0) || (b==0))
return 0;
/* few more obvious checks */
if( a % b == 0 )
return b;
if( b % a == 0 )
return a;
/* Real Logic */
while( (a != 0) && (b != 0) ){
if (abs(a)>abs(b)){
a = abs(a) % abs(b);
}
else if(abs(b) > abs(a) ){
b = abs(b) % abs(a);
}
}
/* Final results */
return (a == 0) ? b : a;
}
#include<stdio.h>
#include <stdlib.h>
int main()
{
long long int a;
int flag = 0;
scanf("%lld", &a);
if (a > 0)
{
while (a > 0)
{
if (a % 2 == 0 || a == 1)
{
a = a / 2;
flag = 1;
}
else
{
flag = 0;
break;
}
}
}
if (a < 0)
{
while (a <= -1)
{
if (a % 2 == 0 || a == -1)
{
a = a / 2;
flag = 1;
}
else
{
flag = 0;
break;
}
}
}
if (flag == 1)
{
printf("yes");
}
else
{
printf("no");
}
}
click this for output image
Given an integer N, the program must determine if it is a power of 2 or -2. If N is a power of 2 or -2, the program must print yes. Else the program must print no.
Boundary Condition(s):
-10^17 <= N <= 10^17
Input Format:
The first line contains the value of N.
Output Format:
The first line contains either yes or no
For the input -4503599627370496 it should print no but it prints yes. Solution, please
In case of negative numbers, you should also count which power of 2 is it. If it is even it won't work. -2 to the power 52 gives 4503599627370496 instead of -4503599627370496.
The code below solves this puzzle using bitwise shift operator:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int a, aPositive, l = 1;
scanf("%lld", &a);
aPositive = a > 0 ? a : -a;
int count = 0;
while (l < aPositive)
{
l = l << 1;
count++;
//printf("%d\t%lld\n", count, l);
}
if (l == aPositive && (a > 0 || count % 2 == 1))
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
Very similar problem has been solved here: how-to-check-if-a-number-is-a-power-of-2
bool IsPowerOfTwo(long long x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
int main()
{
bool result = IsPowerOfTwo(256);
if (result )
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
This program is designed to take 3 integers and arrange them in ascending order. When I input a combination such as "12 8 2", I get an output of "1995099040 8 12". Any help with why i'm getting this answer?
#include <stdio.h>
#include <stdlib.h>
int findBig(int d, int e, int f) {
if ((d >= e) && (d >= f)) {
return d;
} else
if ((e >= d) && (e >= f)) {
return e;
} else
if ((f >= e) && (f >= d)) {
return f;
}
}
int main(void) {
const int MAX_NUM = 3;
int userArray[MAX_NUM];
int bigNum;
int midNum;
int smallNum;
int j = 0;
int a;
int b;
int c;
printf("Please enter three integers separated by spaces:\n");
for (j = 0; j < MAX_NUM; ++j) {
scanf("%d", &userArray[j]);
}
a = userArray[0];
b = userArray[1];
c = userArray[2];
bigNum = findBig(a, b, c);
if (bigNum == a) {
midNum = findBig(0, b, c);
} else
if (bigNum == b) {
midNum = findBig(a, 0, c);
} else
if (bigNum == c) {
midNum = findBig(a, b, 0);
}
if ((midNum == a) && (bigNum == b)) {
smallNum = c;
} else
if ((midNum == b) && (bigNum == c)) {
smallNum = a;
} else
if ((midNum == c) && (bigNum == a)) {
smallNum = b;
}
printf("%d %d %d\n", smallNum, midNum, bigNum);
return 0;
}
smallNum stays with the junk value it gets when it is created, because you didn't cover all the cases for the values possible for bigNum and midNum, so it doesn't get assigned a value in certain cases - like the one you experienced the problem with - and stays uninitialized.
Make sure to check all the possible cases:
if((midNum == a) && (bigNum == b)){ smallNum = c; }
else if((midNum == b) && (bigNum == c)){ smallNum = a; }
else if((midNum == c) && (bigNum == a)){ smallNum = b; }
else if((midNum == b) && (bigNum == a)){ smallNum = c; }
else if((midNum == c) && (bigNum == b)){ smallNum = a; }
else if((midNum == a) && (bigNum == c)){ smallNum = b; }
Also, consider using built-in functions, to save time and code - you have the max and min function right there to help you.
To find the maximum of three numbers you may use max(max(a, b), c) - so we can shorten the code to
smallNum = min(min(a, b), c);
bigNum = max(max(a, b), c);
midNum = (a + b + c) - smallNum - bigNum;
(This will halve the code and render findBig unnecessary)
There are 6 cases to consider for determining smallNum.
if((midNum == a) && (bigNum == b) || ((midNum == b) && (bigNum == a))){
smallNum = c;
}
else if((midNum == b) && (bigNum == c) || ((midNum == c) && (bigNum == b))){
smallNum = a;
}
else if((midNum == c) && (bigNum == a) || ((midNum == a) && (bigNum == c))){
smallNum = b;
}
The junk is due to the missing cases.
You get garbage output because your code invokes undefined behavior:
for the input 12 8 2, the case (midNum == b) && (bigNum == a) is not handled in the code and smallNum stays uninitialized.
You can simplify your code by rearranging the array with 3 tests and swaps:
#include <stdio.h>
int main(void) {
const int MAX_NUM = 3;
int userArray[MAX_NUM];
int x;
printf("Please enter three integers separated by spaces:\n");
for (int j = 0; j < MAX_NUM; ++j) {
if (scanf("%d", &userArray[j]) != 1)
exit(1);
}
if (userArray[0] > userArray[1]) {
x = userArray[0]; userArray[0] = userArray[1]; userArray[1] = x;
}
if (userArray[1] > userArray[2]) {
x = userArray[1]; userArray[1] = userArray[2]; userArray[2] = x;
}
if (userArray[0] > userArray[1]) {
x = userArray[0]; userArray[0] = userArray[1]; userArray[1] = x;
}
printf("%d %d %d\n", userArray[0], userArray[1], userArray[2]);
return 0;
}
I agree with the other answers - your code doesn't take into account all 6 possibilities in which the values could be ordered.
A possible way to fix this: first, modify your "find maximum" function so it returns an index instead of a value:
int findBig(int d, int e, int f) {
if ((d >= e) && (d >= f)) {
return 0; // index of largest element is 0
} else
if ((e >= d) && (e >= f)) {
return 1; // index of largest element is 1
} else
if ((f >= e) && (f >= d)) {
return 2; // index of largest element is 2
}
}
Then, find which number is big, small and median:
bigIndex = findBig(a, b, c);
smallIndex = findBig(-a, -b, -c); // a hack to find the minimum instead of maximum
if (bigIndex == smallIndex)
{
// Edge case - all numbers are equal
// Doesn't matter which index is which; just choose 3 different ones
bigIndex = 0;
midIndex = 1;
smallIndex = 2;
}
else
{
// Choose the index that is neither big nor small
midIndex = 3 - bigIndex - smallIndex;
}
Then print them:
printf("%d %d %d\n", userArray[smallIndex], userArray[midIndex], userArray[bigIndex]);
Here it helps that you have an array. Otherwise, it would still be possible to convert an index to a value bit it would be ugly:
printf("%d %d %d\n",
smallIndex == 0 ? a : smallIndex == 1 ? b : c,
midIndex == 0 ? a : midIndex == 1 ? b : c,
bigIndex == 0 ? a : bigIndex == 1 ? b : c);