I am new to c, please help, the answer to this is always zero.why? Instead of converting KM to Metres or centimetres(sorry for typos);
#include <stdio.h>
int main()
{
float Km;
float metres;
float inches;
float centimetres;
printf("Welcome, please enter the distance in Km.\n");
scanf("%f", &Km);
metres = Km * 1000;
centimetres = Km*100000;
inches = Km*25/1000000;
printf("Distance In Metres is:\n");
printf("%f\n", &metres);
printf("Distance in Centimeters is:\n");
printf("%f\n", ¢imetres);
printf("Distance in Inches is:\n");
printf("%f\n", &inches);
printf("bye\n");
return 0;
}
printf function writes the value of the variable. The ampersand operator & turns your value into a pointer and that's the error. Instead of printing the actual value of your variables, you are printing the address memory of the pointer.
Read documentation on printf function. More info on & and * here.
You are printing the location of the variables. The calculation is fine, but you're not actually printing the value of the variable. You're printing where it is in memory.
The & operator will give the location of the variable. You can fix your program by removing the &s in the printf statements, i.e. this:
printf("%f\n", &inches);
becomes:
printf("%f\n", inches);
Also, here is a link to a pretty in-depth printf() reference; to learn more about pointers, you can go to this page.
Related
Here is my code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x, y, z;
double numerator;
double denominator;
printf("This program will solve (x^2+y^2)/(x/y)^3\n");
printf("Enter the value for x:\n");
scanf("%lf", x);
printf("Enter the value for y:\n");
scanf("%lf", y);
numerator = sqrt(x) + sqrt(y);
denominator = pow((x/y),3);
z = (numerator/denominator);
printf("The solution is: %f\n", z);
return(0);
}
Can anyone give me a (hopefully) quick pointer to fix my infinite loop?
There's no loop in your function, so I think it's your calls to scanf() that are causing the error:
You need to pass reference to scanf(), i.e. use scanf("%lf",&x) instead of scanf("%lf",x).
BTW, according to your fonction definition, you should use pow(x,2) instead of sqrt(x) which returns the square root.
Since this is your first question
**Welcome to stack overflow**
Your code doesnt go into an infinite loop,there is a runtime error.
Your scanf Code is flawed use this:
scanf("%lf",&x);
scanf("%lf",&y);
you want scanf to modify the value contained at the address field of your value.Please read tutorials.
Also use
numerator=pow(x,2) + pow(y,2);//numerator=x^2+y^2
It's not infinite loop, your code just returns infinity. And that is because scanf() needs a pointer to variable where it should put the read number. To get an address of variable you can use & operator like this:
scanf("%lf", &x);
scanf("%lf", &y);
I am making a program to convert weight and height from metric to US and vice versa. I did the height part successfully, but the part with the weight is giving me a runtime error that stack around variable was corrupted.
I know that happens with arrays because pretty much thats all I get when I google the issue, but this is happening with a regular integer variable in 2 different functions.
This is the function that calls other functions to convert weight, one is for input, one is to convert and one is for output:
void weight_to_metric(void){
int kilograms, pounds;
double grams, ounces;
int * pkilograms= &kilograms, *ppounds=£s;
double * pgrams=&grams, *pounces=&ounces;
input_us_weight(ppounds, pounces);
weight_us_to_metric(ppounds, pounces, pkilograms, pgrams);
output_metric_weight(pkilograms, pgrams);
}
this is the function that inputs
void input_us_weight(int* feet, double * inches){
printf("enter the number of pounds you want to convert: ");
scanf(" %lf", feet, "\n");
printf("enter the number of ounces you want to convert: ");
scanf(" %lf", inches, "\n");
}
this is the function that converts
void weight_us_to_metric(int* pounds, double* ounces, int* kilograms, double * grams){
double temp_kilograms;
*kilograms = *pounds / 2.2046 + ((*ounces / 16) / 2.2046);
temp_kilograms = *pounds / 2.2046 + ((*ounces / 16) / 2.2046);
*ounces = ((temp_kilograms - *kilograms) *2.2046)*16;
*grams = ((*ounces / 16.0)/2.2046) * 1000;
}
The output function doesn't even deal with the variable that corrupts. The variable that corrupts is pounds. The integer pounds declared in the initial variable.
How do i fix this?
You are using wrong format specifiers in one of your scanfs. Use %d when scanning an int and %lf when scanning a double. Change
scanf(" %lf", feet, "\n");
to
scanf("%d", feet);
Also, remove the third argument("\n") that you pass to the scanfs. It makes no sense.
This is not homework, but my last assignment made it clear that I didn't clearly understand pointers when coding C.
Therefore, I tried to type a simple program using pointers just to invert an integer in a function, so I could figure out where the gap in my understanding is.
Apparently, I've arrived at it, but I still cannot figure out what I am doing wrong.
My source code is below.
#include <stdio.h>
float invert(int *num);
void main (void)
{
int num;
float a;
printf("enter an integer \n");
scanf("%i", &num);
printf("Number entered %i \n", num);
a=invert(&num);
printf("This is the invse from main %f \n", a);
}
float invert(int *num) /* function inverts integer */
{
float invse;
printf("num is %i \n\n", *num);
invse = 1/(float)num;
printf("invse is %f \n\n", invse);
return(invse);
}
My thinking was that I used the pointer to direct the computer to use the value stored at the address for num in the function invert(). The pointer appears in the variable declaration. I cast the value stored at that pointer as a float, so I could invert it, and store it in a local variable.
The problem appears to be in the local variable assignment. My compiler returns "invert.c:29:2: error: pointer value used where a floating point value was expected
invse = 1/(float)num;
^
Apparently my code indicates a pointer value for inverse, but I declared it as a float, which I find confusing.
Any help is appreciated. This will save me on completing my larger set of code for my assignment, which I did not post here.
Thanks.
Judging by the printf call inside invert
printf("num is %i \n\n", *num);
you already know that in order to access the value passed to invert for inversion you have to dereference num pointer: *num.
If so, then why aren't you dereferencing num when you perform the inversion itself?
invse = 1/(float)num;
I mean, if you are the one who wrote that printf, you should also realize that the actual inversion should be done as
invse = 1 / (float) *num;
or, alternatively, as
invse = 1.f / *num;
On top of being incorrect your original variant is illegal: you are not allowed to convert pointers to floating-point types in C, which is the reason for the error message.
P.S. From the bigger picture point of view, there's no real reason to pass that the number to invert by pointer. Passing the immediate value would make more sense
float invert(int num)
{
...
In that case you, of course, don't have to dereference anything inside invert.
Program only displaying the 32 for when I have it print "NewTemp"
NewTemp = 32 + input * 180/100; this part seems like the main problem
#include <stdio.h>
float celsius(float input) {
float NewTemp;
**NewTemp = 32 + input * 180/100;
printf("Please enter the temperature value to convert to fahrenheit\n");
scanf("%f", &input);
printf("The temperature in celsius is: %f\n", NewTemp);
return NewTemp;
}
int main(void){
float CelToFahren, input;
CelToFahren = celsius(input);
}
You do the math before you read the input. You need to do it the other way around.
Also, there's no reason to pass a meaningless and uninitialized value to the celsius function.
Lastly, 180/100 is 1 remainder 80 because when you divide two integers, you get integer division. You can use 180.0/100.0.
Basically, you need to learn C.
#include<stdio.h>
float func (float t, float y){
return y ;
}
int main (){
float t0,y0,t,y;
printf ("the value of t: ");
scanf ("%f",&t0);
printf ("the value of y: ");
scanf ("%f",&y0);
t=t0;
y=y0;
static int n=0;
// t[0]=t0;
// y[0]=y0;
for (n=0;n<=3;n++){
y[1]=y[0];
printf ("value of y %f %f \n",t,y);
}
return 0;
}
The error is:
Building prog.obj.
D:\master\c language\ch3\prog.c(166): warning #2117: Old-style function definition for 'main'.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
*** Error code: 1 ***
You cannot array index something that is not an array, or a pointer into an array.
Your y and t floats are not pointers into arrays in your program.
You should make them float *y, *t into pointers so you can point them into array.
Change float t0,y0,t,y; to float t0,y0,*t,*y;
and
t=&t0; //assign address of t0 to t
y=&y0;
Change printf ("value of y %f %f \n",t,y); to
printf ("value of y %f %f \n",*t,*y); //note to dereference t and y here, to get their values
Here's a example of your program I fixed to work
The 'Old-style function definition for main()' message means that you've not given a prototype definition. The correct forms are:
int main(void) { ... }
int main(int argc, char **argv) { ... }
The version int main() is fine in C++, but not strictly a prototype in C, and hence gets the 'old-style' tag.
The other messages are more inscrutable; the line numbers do not correspond to the code you show. However, as Tony The Lion notes in his answer, the line
y[1] = y[0];
is erroneous since y is not an array. There is room to think that should be:
y = y0;
and you'd need a companion:
t = t0;
in order to have defined values printed in the printf() statement.
Even with these changes, the code does not make a lot of sense. However, given that you removed 150-odd lines, we can suppose that the missing code would make more sense.
There is no need to make n into a static variable; it is better not to do so.
Please make sure, in future, that your error messages correspond to the source code you post, not to some variant version of the code you post. The line numbers should not be as large as 166 or 182; they should be single digit numbers or small double digit numbers. But even more importantly, they should match the code!