Trying to run a program in C - c

Can someone help me to run this program? I tried this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double Cateto1;
double Cateto2;
double hipotenusa;
printf("dame el primer cateto: ");
scanf("%1f", Cateto1);
fflush(stdout);
printf("dame el segundo cateto: ");
scanf("%1f", &Cateto2);
fflush(stdout);
hipotenusa = sqrt ((Cateto1*Cateto1)+(Cateto2*Cateto2));
printf("hipotenusa= %2f",hipotenusa);
system("pause");
}
I can build it but I can't run it... it gives me:
RUN FAILED (exit value -1.073.741.790, total time: 17s)

scanf("%lf", Cateto1);
↑ ↑
| You are missing a '&' character here
The width specifier for doubles is l, not 1
The first argument to scanf must be "%lf" (as the letter L) to specify that the corresponding output variable is a pointer to double instead of a float. '1' (One) has no meaning for scanf.
The second argument to scanf here is expected to be a pointer to double, and you're giving it a double instead.
I suppose it is a simple typo since you got it right the second time.

Here is the mistake:
scanf("%1f", Cateto1);
Change it to:
scanf("%1f", &Cateto1);

There are a couple of errors:
The syntax of the scanf expression was wrong: "%1f" should be "%lf"
You need to pass the address of Cateto1 (&Cateto1) to scanf
You don't need the fflush
You don't need the system call
Here's the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double Cateto1;
double Cateto2;
double hipotenusa;
printf("dame el primer cateto: ");
scanf("%lf", &Cateto1);
printf("dame el segundo cateto: ");
scanf("%lf", &Cateto2);
hipotenusa = sqrt ((Cateto1*Cateto1)+(Cateto2*Cateto2));
printf("hipotenusa= %2f\n",hipotenusa);
}

There is an Error in your code. Instead of
scanf("%1f", Cateto1);
you should write:
scanf("%1f", &Cateto1);

Simple Mistake
scanf("%1f", &Cateto1); // '&' was missing in all scanf statements

#include <stdio.h>
#include <math.h>
int main(void)
{
double Cateto1;
double Cateto2;
double hipotenusa;
printf("dame el primer cateto: ");
scanf("%lf", &Cateto1);
//fflush(stdout);
printf("dame el segundo cateto: ");
scanf("%lf", &Cateto2);
//fflush(stdout);
hipotenusa = sqrt ((Cateto1*Cateto1)+(Cateto2*Cateto2));
printf("hipotenusa= %2f\n",hipotenusa);
//system("pause");
return 0;
}

Related

Input of double is improper eventhoug converter is

I've been struggling with the following program that helps me cheat in my maths class calculating the quadratic equation:
/*Improved version of my maths cheat code -w-,
this program has the same functionality as the last but with
cleaner and more correct code. Notes taken from the post made in
codereview.stackechange to make it less of a dumpster fire.
The program is not functional already.*/
#include<stdio.h>
#include<math.h>
double a,b,c,d;
//Get values of the equation
void get_values (void){
printf("Dame el valor de a: ");
scanf("%g", &a);
printf("Dame el valor de b: ");
scanf("%g", &b);
printf("Dame el valor de c: ");
scanf("%g", &c);
}
//Calculates the discriminante
double discriminante (double a, double b, double c){
double result;
result = b*b-4*a*c;
return result;
}
//Prints the result of the equation based on the square root
//of the discriminate
void display (double d){
if (d>0){
printf("El resultado es: (%g±%g)/%g\n", -b, sqrt(d), a*2);
}
else if (d<0){
printf("El resultado es: (%g±%gi)/%g\n", -b, sqrt(-d), a*2);
}
else { // d == 0
printf("El resultado es: %g/%g\n", b, a*2);
}
}
int main(){
get_values();
d = discriminante(a,b,c);
printf("El valor del discriminante es: %g\n",d);
display(d);
return 0;
}
I made a post in codereview.stackexchange to get some feedback on my code. After rewritting the thing in a cleaner generally better way, I found the small issue that the functions never take the input properly. I've checked the scanf and converters for an hour now, and I'm just baffled at this point on why it just refuses to take input properly. Did I use an improper converter or did I made an error at discriminante?
%g in scanf() is for reading float. You should use %lg to read double.
Note that using %g in printf() to print double is fine.

Function prototyping in C language

Why can't my code run when I am using a function name "div". But when I change it to any name like " divi",it runs.Can someone explained it? Im 1st yr.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double div(int, int);
int main(void)
{
int num1, num2;
printf("Enter the numbers: \n");
scanf("%d", &num1);
printf("Enter the numbers: \n");
scanf("%d", &num2);
printf("The answer is %.2f", div(num1,num2));
}
double div(int x, int y)
{
double quot;
quot=((double) x/y);
return quot;
}
The standard library already defines a function called div, so you can't use that name for your own function.

Segmentation Fault (Core Dump) C [duplicate]

This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 5 years ago.
I've started to study C this week, I'm totally new in programming in C, and when I tryed to do this exercise this error in the console keep showing up.
#include <stdio.h>
#include <stdlib.h>
float calc(float *sall, float *salb)
{
float hraula, insspc;
int naula;
printf("Digite o valor da hora-aula e o numero de aulas dadas:");
scanf("%f%i", hraula, naula);
printf("Digite a porcentagem do inss retirada do salário:");
scanf("%f",insspc);
*salb = hraula * naula;
*sall = *salb * ((100 - insspc) / 100);
return 0;
}
int main()
{
float salbt, sallq;
calc(&sallq, &salbt);
printf("O salário bruto é: %f R$, liquido: %f R$", salbt, sallq);
return 0;
}
Well hope someone can help me, thanks!
scanf(" %f%i", hraula, naula);
scanf(" %f",insspc);
It should be as scanf requires the pointers to variables:
scanf(" %f%i", &hraula, &naula);
scanf(" %f",&insspc);
Pass a pointer to the receiving variables like this:
scanf("%f%i", &hraula, &naula);
Similarly
scanf("%f", &insspc);
Reference: man 3 scanf
It is also good practice to check the return value of scanf to ensure that you have collected the correct number of values. Something like this:
if (scanf("%f%i", &hraula, &naula) != 2) {
fprintf(stderr, "Failed to read hraula and naula\n");
return -1;
}
and then check the return value of calc():
if (calc(&sallq, &salbt) == 0)
printf("O salário bruto é: %f R$, liquido: %f R$", salbt, sallq);

Calculate the length of a side of triangle in C

I am trying to write a program that calculates the length of one side of a triangle with the help of the Pythagoras equation (c²=a²+b²). The user must have the option to choose what side he want to calculate, this is what I have tried:
#include <conio.h>
#include <stdio.h>
#include <math.h>
// Pitagora:
// c=sqrt(pow(a,2)+pow(b,2));
// a=sqrt(pow(c,2)-pow(b,2));
// b=sqrt(pow(c,2)-pow(a,2));
int cateta(int x, int y){
int cat;
printf("Dati marimea lui:");
scanf("%d", &x);
printf("Dati marimea lui:");
scanf("%d", &y);
cat=sqrt(pow(x,2)-pow(y,2));
return cat;
}
int main(){
int a,b,c;
char l;
printf("Ce latura doriti sa aflati?");
printf("\n c : ipotenuza\n a : cateta alaturata\n b : cateta opusa\n");
printf("Introduceti litera laturei respective : ");
scanf("%s", &l);
if (l == a){
a=cateta(c,b);
printf("Marimea catetei alaturate este: %d", a);
}
else if (l == b){
b=cateta(c,a);
printf("Marimea catetei opuse este: %d", b);
}
else {
c=sqrt(pow(a,2)+pow(b,2));
printf("Marimea ipotenuzei este: %d", c);
printf("\n");
}
getch ();
return 0;
}
But, for some reason when I give a value of a to the variable &l the program displays the content of this piece of code: printf("Marimea ipotenuzei este: %d", c); instead of scaning the value of x and y, and terminates. Here is a picture with the result: https://www.dropbox.com/s/wzk3osw1t8729et/Untitled.png?dl=0
you are using %s in scanf() for a character type variable, instead use this
scanf(" %c", &l);
First, u need to change the scanf statement to scanf( "%c",&l);
Now the variable l contains the character entered by user.
Next, during comparison change the if condition to if(l=='a') , if( l=='b') as a and b are character literals.
With this changes, the program should work!
Happy coding!!

printf in Eclipse

I have to work with eclipse in C. I wrote a simple program, but I have a problem with a printf command which doesn't work properly. Any idea?
Here is the code :
#include <stdio.h>
void change(double *x, double *y)
{
double help = *x;
*x = *y;
*y = help;
return;
}
int main()
{
double x=0, y=0;
printf("please give a value to a \n ");
scanf("%f",&x);
printf("please give a value to b \n");
scanf("%f",&y);
printf("x=%.2f\t y=%.2f\n",x,y);
printf("will give \n");
change(&x,&y);
printf("x=%.2f\t y=%.2f\n",x,y);
return 0;
}
So the problem is that I dont't get this first printf.
All your values are double for which you have to use %lf. Buut you are using %f which invokes undefined behaviour.
Change %f to %lf in your scanfs and prints.

Resources