Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
In my function when I set *area = 2 or to any other int, my program works as intended. For some reason I cannot calculate the length by the width and have it out put properly, all I get are 0's. What am I missing? Is it something in the call? Also is %f an appropriate conversion for double or is there something better to use? My code is below:
#include <stdio.h>
void area_perimeter(double width, double length, double *area, double *perimeter);
int main(){
double width, length, are, peri;
printf("Enter the width of the rectangle: ");
scanf("%f",&width);
printf("Enter the length of the rectangle: ");
scanf("%f",&length);
area_perimeter(width, length, &are, &peri);
printf("The area of the rectangle is: %f\n",are);
printf("The perimeter of the rectangle is: %f\n",peri);
return 0;
}
void area_perimeter(double width, double length, double *area, double *perimeter){
*area = length * width;
*perimeter = (*area * 2);
}
You invoked undefined behavior by passing a pointer to object having wrong type: %f in scanf()callls for float*, but you passed double*. Use %lf to read double.
Using %f in printf() to print double is good. C99 compiler will also accept %lf.
Try this:
#include <stdio.h>
void area_perimeter(double width, double length, double *area, double *perimeter);
int main(void){
double width, length, are, peri;
printf("Enter the width of the rectangle: ");
if (scanf("%lf",&width) != 1) {
fputs("read error width\n", stderr);
return 1;
}
printf("Enter the length of the rectangle: ");
if (scanf("%lf",&length) != 1) {
fputs("read error length\n", stderr);
return 1;
}
area_perimeter(width, length, &are, &peri);
printf("The area of the rectangle is: %f\n",are);
printf("The perimeter of the rectangle is: %f\n",peri);
return 0;
}
void area_perimeter(double width, double length, double *area, double *perimeter){
*area = length * width;
*perimeter = (*area * 2);
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My Code:
#include <stdio.h>
int main()
{
int l, b, a;
printf("Enter the length of the rectangle: ");
scanf("%f", &l);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &b);
printf("Area of rectangle is %f", l * b);
return 0;
}
When I give any input it doesn't show me its product, but 0.000000 instead:
As I gave input 2 and 3, it should print Area of rectangle is 6
%f expects its corresponding argument to have type float and you are passing int to it, so changing it to %d would fix the issue, as %d expects its corresponding argument to have type int.
#include <stdio.h>
int main() {
int length, breadth, area;
printf("\nEnter the Length of Rectangle: ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle: ");
scanf("%d", &breadth);
area = length * breadth;
printf("\nArea of Rectangle: %d\n", area);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
here is the code I wrote.
#include <stdio.h>
#include <stdlib.h>
typedef struct note {
int pitch;
int velocity;
int channel;
} note;
int printnote(int pitch, int velocity, int channel);
int main() {
int size = 100;
note note;
struct note *ptr = malloc(size * sizeof(int));
printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
scanf("%d %d %d", ¬e.pitch, ¬e.velocity, ¬e.channel);
printnote(note.pitch, note.velocity, note.channel);
free(ptr);
return 0;
}
int printnote(pitch, velocity, channel) {
printf("The MIDI Note is:\n");
printf("Pitch -> %d\n", pitch);
printf("velocity -> %d\n", velocity);
printf("channel -> %d\n", channel);
return 0;
};
When I run the code and type the numbers, it shows the wrong answers.
For example, I run the code, and it shows
Input the values for the `pitch`, `velocity`, and `channel`
5 5 5
The MIDI Note is:
Pitch -> 5
velocity -> 0
channel -> -429762432
The three numbers should be the same as the input numbers.
Can anyone help me?
There is a subtle typo in your scanf() conversion string:
scanf("%d %d %d", ¬e.pitch, ¬e.velocity, ¬e.channel);
You used the unicode full width percent sign % (\uff05) instead of the ASCII % character. scanf does not recognise this as a conversion specifier and tries to match the byte sequence used to encode %, (0xEF 0xBC 0x85 in UTF-8) and fails thus only converting the first input into note.pitch and leaving note.velocity and note.channel uninitialized, returning 1. Note how % looks different from % in the fixed font used for code, but identical in the font used for this text: % % % % % %.
Just replace % with the correct character:
scanf("%d %d %d", ¬e.pitch, ¬e.velocity, ¬e.channel);
Also note these remarks:
size and ptr are not used in main(),
you should check the return value of scanf() to detect invalid input. This check would have helped find the error,
the prototype in the definition of printnote is incorrect: the argument types are missing,
the ; after the } is useless,
using the same identifier note for the variable and its type is confusing.
Here is modified version:
#include <stdio.h>
typedef struct note {
int pitch;
int velocity;
int channel;
} note;
void printnote(int pitch, int velocity, int channel);
int main() {
note note1;
printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
if (scanf("%d %d %d", ¬e1.pitch, ¬e1.velocity, ¬e1.channel) != 3) {
printf("invalid input\n");
return 1;
}
printnote(note1.pitch, note1.velocity, note1.channel);
return 0;
}
void printnote(int pitch, int velocity, int channel) {
printf("The MIDI Note is:\n");
printf("pitch -> %d\n", pitch);
printf("velocity -> %d\n", velocity);
printf("channel -> %d\n", channel);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have researched a solution for this error but I still don't get it.
#include <stdio.h>
float calcF(float a, float b, float c);
int main(void)
{
float mag_flux_den, cur, len;
float result;
printf("What is the magnetic flux density in tesla? : ");
scanf("%f", &mag_flux_den);
printf("What is the current in the conductor in Amperes? : ");
scanf("%f", &cur);
printf("What is the length of the conductor in the magnetic field in metres? : ");
scanf("%f", &len);
result = calcF(mag_flux_den, cur, len);
printf("Force on the current carrying conductor: %f", result);
return 0;
}
float calcf(float a, float b, float c) //calculates force on the current carrying conductor{
float F;
F = a * b * c;
return F;
}
I am using ideone.com and still getting the same error message (undefined reference to 'calcF'). Any help will be much appreciated.
You declared your function as "calcF" with a capital "F", but your definition is "calcf" with a lowercase "f". Make sure they are the same.
Also, your function definition at the bottom of your code block has it's open bracket at the end of the comment.
Move it after the comment.
Try this:
#include <stdio.h>
#include <cs50.h>
float calcF(float a, float b, float c);
int main(void)
{
float mag_flux_den, cur, len;
float result;
printf("What is the magnetic flux density in tesla? : ");
scanf("%f", &mag_flux_den);
printf("What is the current in the conductor in Amperes? : ");
scanf("%f", &cur);
printf("What is the length of the conductor in the magnetic field in metres? : ");
scanf("%f", &len);
result = calcF(mag_flux_den, cur, len);
printf("Force on the current carrying conductor: %f", result);
return 0;
}
float calcF(float a, float b, float c) //calculates force on the current carrying conductor
{
float F;
F = a * b * c;
return F;
}
I was trying to calculate the area of both a triangle and a circle using 3 separate user defined functions within main. After compiling, the GetInt function worked properly but the other two functions' calculations are not printing on the screen accurately for my c program on Microsoft Visual Studio 2013. What am i doing wrong?
#define _CRT_SECURE_NO_WARNINGS
#define PI 3.14159
#include <stdio.h>
#include <math.h>
int GetInt(void);
double CalcTriangleArea(int base, int height);
double CalcCircleArea(int radius);
int main(void)
{
printf("%d", GetInt());
printf("%f", CalcTriangleArea);
printf("%f", CalcCircleArea);
return(0);
}
int GetInt(void)
{
int radius, base, height;
printf("What is the radius of the circle? \n\n");
scanf("%d", &radius);
printf("What is the base of the triangle? \n\n");
scanf("%d", &base);
printf("\nthe height of the triangle? \n\n");
scanf("%d", &height);
return (radius, base, height);
}
double CalcTriangleArea(int base, int height)
{
double triangleArea;
printf("Triangle area is %.2f \n\n", triangleArea = .5*base*height);
return(0);
}
double CalcCircleArea(int radius)
{
double circleArea;
printf("Circle area is %.4f \n\n", circleArea = PI * pow(radius, 2));
return(0);
}
CalcTriangleArea Is not calling the function, it's using the functions address. Try CalcTriangleArea()
edit: Just noticed CalcTriangleArea takes parameters - you'll need to pass those in too.
edit: And how do you think you can return 3 values?
For CalcTriangleArea and CalcCircleArea there is no need to have the printf's in main if you're printing the results in your functions. You could simply call them in main like so:
GetInt();
CalcTriangleArea(base, height);
Also, if this is the approach you are taking (printing results in the function and returning 0), your functions could be declared like so:
void myFunction(int arg1, int arg2)
Since your are not returning anything that you are using, using a void type allows you to have a function without an explicit return.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm stuck on this code and have searched the web for an answer and I didn't find anything.
The code get compiled but in options 1 and 2 the code doesn't get the real answer, I don't know if the problem is about the all thing like double and float or because of a stupid mistake.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
void twoPoints ();
void hypotenuseInTriangular ();
void areaAndScopeInCircle (float radius);
void areaOfSquare ();
void areaOfRectangle ();
int main()
{
int num=0 ;
float radius=0;
double ans1= 0,x1=0,x2=0,y1=0,y2=0;
printf("hello friend, please enter one of the options: \n");
printf("press 1 to calculate length between two points\n");
printf("press 2 to calculate hypotenuse in right tringle \n");
printf("press 3 to calculate area and scope in circle \n");
printf("press 4 to calculate area of square \n");
printf("press 5 to calculate area of rectangle \n");
printf("press 6 to exit from the code \n");
scanf("%d",&num);
switch (num)
{
case 1:
twoPoints(x1,x2,y1,y2);
break;
case 2:
hypotenuseInTriangular();
break;
case 3:
areaAndScopeInCircle (radius);
break;
case 4:
areaOfSquare();
break;
case 5:
areaOfRectangle();
break;
case 6:
printf("bye!\n");
break;
default:
printf("not good number \n");
break;
}
system("PAUSE");
return (0);
}
/**
this funcion calculate the length between two points.
input: two points (x1,y1) (x2,y2).
output: the length between them.
*/
void twoPoints (double x1,double x2,double y1,double y2)
{
double ans1=0,calculate1=0,calculate2=0;
printf("enter two integer points like this: x1 y1 x2 y2\n");
scanf("%1f %1f %1f %1f",&x1,&y1,&x2,&y2);
calculate1 = (x1 - x2) * (x1 - x2);
calculate2 = (y1 - y2) * (y1 - y2);
calculate1 += calculate2;
calculate2 = sqrt(calculate1);
printf("the length between your two points is: %f\n",calculate2);
}
/**
this funcion calculate the hypotenuse in right tringle using two ribs.
input: two integer numbers of the tringle.
output: the hypotenuse length.
*/
void hypotenuseInTriangular (double sideA,double sideB)
{
double sideC=0.0,sum=0.0;
printf("enter two ribs from the triangle: \n");
scanf("%1f %1f",&sideA,&sideB);
sum = (sideA * sideA + sideB * sideB);
sideC = sqrt(sum);
printf("your hyptenuse is %f \n",sideC);
}
/**
this funcion calculate area and scope using radius.
input: a radius number.
output: the area and the scope of this circle.
*/
void areaAndScopeInCircle (float radius)
{
float area = 0,scope =0;
printf("enter radius: \n");
scanf("%f",&radius);
area = PI * radius * radius;
scope = PI * radius * 2;
printf("the area of your circle is: %.1f\n",area);
printf("the scope of your circle is: %.1f\n",scope);
}
/**
this funcion calculate area of square using two ribs.
input: two integer numbers.
output: the area of the square.
*/
void areaOfSquare (int rib)
{
printf("enter one of the square rib: \n");
scanf("%d",&rib);
printf("the area of your square is: %d\n",(rib*rib));
}
/**
this funcion calculate area of rectangle using two ribs.
input: two integer numbers.
output: the area of the rectangle.
*/
void areaOfRectangle (int length,int width)
{
printf("enter length and than width\n");
scanf("%d %d",&length,&width);
printf("the area of your rectangle is: %d\n",(length*width));
}
In your function twoPoints() you have used incorrect format specifiers with
scanf("%1f %1f %1f %1f",&x1,&y1,&x2,&y2);
these could be typos or copying errors because a 1 (numeral one) looks a lot like an l (letter ell). The line should be
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
and after making this correction the function works.
Note that the lower-case L in the format specifier stands for long. In scanf() and friends, for a float variable input you use %f and for a double variable you use %lf. However in printf() and friends, you use %f for double and for float because any float argument is automatically promoted to double.
In your function twoPoints -
scanf("%1f %1f %1f %1f",&x1,&y1,&x2,&y2);
instead of above try this -
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); // %lf not %1f
And similar in other funcitons .
Note - Also , your function when defined expect arguments , but when you call them in main , you don't pass them any arguments. Such as this -
hypotenuseInTriangular(); // expects 2 double arguments
Call them with correct arguments , and also declare prototypes correctly .