Programming C if statements - c

#include <stdio.h>
#include <math.h>
int main (void){
double a,b,c, x, y, z1, z2;
printf("Enter the coefficients of a polynomial(ax^2+bx+c): ");
scanf("%lfx^2+%lfx+%lf",&a,&b,&c);
z1=(-b+sqrt(b*b-4*a*c))/2*a;
z2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("%lf and %lf",z1,z2);
getch();
return 0;
}
I want an IF statement such that if any polynomial does not have a value, it would be 1.
Say that I want my polynomial is x^2+7x+6; I would have to input as 1x^2+7x+6 to have the output values.
How would I write an if statement that if a, b, or c doesn't have a value, then it will equal to 1?

Set values of a,b,c =1 by default..
Now create a condition that example if a user enter char rather than int or float . it does nothing {use error handling for that,}
Or take all a,b,c input as string then typecast to to double, and use error handler if error occur assign it value 1

Related

triangle area calculator, It keeps showing 0, why?

I am making a triangle area calculator, but it only show 0 instead of answer, It must be the formula 1/2 * ab sin c , can someone tell me what should I change to make it work.
#include <stdio.h>
#include <math.h>
float main ()
{
float a,b,c,pi ;
printf("Enter a : ");
scanf("%f",&a);
printf("Enter b : ");
scanf("%f",&b);
printf("Enter c :");
scanf("%f",&c);
printf("%f\n",(a*(1/2)*b*(c*pi)/180));
return 0;
}
The sub-expression (1/2) is always equal to 0 due to the integer arithmetic. You need to write for example (1.0f/2).
Apart from this the variable pi is not initialized
float a,b,c,pi ;
Also according to the C Standard the function main without parameters shall be declared like
int main( void )

There is an C function with pointers and a char return, what only returns the char value and 0 to the pointers

The objective here is to print the student's average grade as a letter (A for Aprooved, R for Reprooved, or F for Miss, or Falta, in Portuguese), with the average grade value itself, and the student's total presence in classes, using pointers to avg and misses. But after compilation, only the char return value is printed, with 0 on the others.
I tried to print the stored values in the pointers *avg and *presence, but the program takes the student grade values and present values before crashing.
#include <stdio.h>
char situation(float n1,float n2,float n3,int misses, int classes, float
*avg, float *presence);
int main()
{
float *avg, *presence, vet[3];
int f, a, x;
printf("Write the value of your notes \n");
for(x=0;x<=2;x++)
{
printf("Note %d:",x+1);
scanf("%f",&vet[x]);
}
printf("Misses: ");
scanf("%d",&f);
printf("Given: ");
scanf("%d",&a);
char outcome=situation(vet[0], vet[1], vet[2], f, a, &avg, &presence);
printf("With an average of %f and presence of %f percent, your situation is %c",avg,presence,outcome);
return 0;
}
char situation(float n1,float n2,float n3,int misses, int classes, float
*avg, float *presence)
{
char result;
*presence=((classes-misses)/classes)*100;
*avg=(n1+n2+n3)/3;
if(*presence>=0 && *presence<75)
{
result='F';
}
if(*presence>=75 && *presence <=100)
{
if(*avg>=0 && *avg<6);
{
result='R';
}
if(*avg>=6 && *avg<=10)
{
result='A';
}
if(*avg<0 || *avg>10)
{
result='x';
}
}
if(*presence<0 || *presence>100)
{
result='x';
}
return result;
}
I expected the student's total presence and the average grade as a value and letter (A, R, or F), printed on the terminal to the user, but it only returned the char result value.
Your code has one main problem:
float *avg,*presence,
When you a calling
char outcome=situation(vet[0],vet[1],vet[2],f,a,&avg,&presence);
You are passing the address to the pointer you just declared. By changing the value in the function outcome, you are changing the address that avg and presence are pointing to, not the value of the variable.
Try changing to
float avg,presence
This way, when you change the value (using the * operator) you will change the actual variable value.
You may find this that presence still be showing as 0 on the printf. This is because how C works with types in arithmetics operations. Your division has only ints on it, so the result will be a int. Given the result will always be >= 1, the result will be rounded to the int value, which is 0.
To fix that just put a float value in the division and the result will be a float.
Something like:
*presence=((classes-misses)/(classes*1.0))*100;
avg is a float pointer, you are sending it by pointer (&avg) so your function needs to be (int n1,....,float **avg,...)
Try to send avg without the &.
Give him a value before (it is better)
The compiler warning pointed you directly to the problem. You should examine the warning and come to understand what it means and why it was issued.
Your error is here:
float *avg,*presence
These should be declared as float, not float *.

Surface area of a hexagon

I am currently attempting to learn C, and have made this program to calculate the area of a regular hexagon:
#include <stdio.h>
#include <math.h>
void main(){
int a;
float ans;
scanf("%d", &a); // get length of side
ans = ((pow(a, (1/3)))/2)*(a*a);
printf("%f", ans);
}
However, it outputs seemingly random numbers.
Firstly your code doesn't compile (Missing semicolon) and also you should use int main() instead of void main().
Secondly your formula also wrong, the area of a regular hexagon of side length a is calculated as ((3√3)/2)*a².
Thirdly Expression like 1/3 always yield zero as both are integer, to get expected behavior make one of them float/double. like 1.0/3 or (float)1/3 etc.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}

Programming C calling function area of circle and rectangle

#include <stdio.h>
#define PI 3.14159
int Circle (int);
int Rectangle (int, int);
int main()
{
int a;
int b;
int c;
int d;
int area;
int AreaOfCircle;
int AreaOfRectangle;
int area1;
printf("Program to calculate area\n");
printf("1 - Circle\n");
printf("2 - Rectangle\n");
printf("\n");
printf("What option = \n");
scanf("%d", &a);
if(a=1)
{
area=Circle(b);
printf("Area= %d\n", area);
}
else if(a=2)
{
area1=Rectangle(c,d);
printf("Area= %d\n", area1);
}
return 0;
}
int Circle (int b)
{
int area;
printf("radius= \n");
scanf("%d", &b);
area=PI*b*b;
return area;
}
int Rectangle(int c, int d)
{
int area1;
printf("length= \n");
scanf("%d",&c);
printf("width= \n");
scanf("%d",&d);
area1=c*d;
return area1;
}
//I want to ask if my coding is ok .. but as I run it the output only ask for radius which is the calling function for circle .. but if i want to call rectangle the output also shows calculation for circle .. can someone help me to spot the mistake .. by the way this is my first coding about calling function and I just started learning coding c last month .. T-T
With C you use == to evaluate (e.g. if (x == 1)). "=" is assignment, so you'll always hit the first block.
Also, you're accepting parameters which you're then modifying, which is not good practice. Consider declaring your variables at usage time also, the "everything at the top of the block" paradigm is very dated.
This question is not about functional programming, this is an example of imperative programming.
Also, your input being poured directly into an integer is not bounds checked, consider a switch/case so you can add a default of "invalid input" and extend to different shapes in the future.
Yes bro just make if(a==1) and else if(a==1).
You've used the assignment = operator instead of the comparison == operator.
A statement like
if(a=1)
will assign a value of 1 to a and check then check for the non-zero value of a [which always evaluates to TRUE].
Instead, what you want is
if (a == 1)
which evaluates to TRUE if a contains 1. Same for other comparison(s) also.
Note: In your int Circle (int b) case you're storing the result to an int, which will truncate the result of a double/float multiplication. To get the exact value, make the area as float or double and use %f/ %lf format specifier.
Next, as per the logical part, you don't need to pass b, c, d as parameters to the called functions. Simply a local variable in the functions would do the job.

Standard Deviation Program

This was the instructions: Write a program that starts out asking the user for the mean u and
standard deviation s for the normal distribution
(see the wiki article http://en.wikipedia.org/wiki/Normal_distribution
The program then asks for an N, and then asks for N values x. For each x it
writes out f(x) to the screen.
Note that the program asks the user for u, s, and N just once. After that it
asks for N values for x, one by one. After each value x it writes out the
corresponding value of the function.
Of course, use double precision for this and use the square root and
exponential function from the standard math library.
This is my code so far but I can't get the N to work.
#include <stdio.h>
#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s,N,x,math1, math2, math3,n,;
printf("Enter Mean: \n");
scanf("%d", &u);
printf("Enter Deviation: \n");
scanf("%d", &s);
n=1/2;
math1 =1/(u*sqrt(2*PI));
math2= (x-u)/s * (x-u)/s;
math3= E * exp(n);
x = math1 * exp(math3)*exp(math2);
printf("%d \n", x);
system("Pause");
}
n=1/2;
This will be equal to 0, since 1 is an integer, 2 is an integer and 1 divided by 2 is 0 in integer math.
Try 1.0/2.0
Make sure every other division has a double on one or both sides, otherwise it will be done as integer math

Resources