While compiling this code in the terminal, I am getting an error saying :
newfile1.c:17: error: conflicting types for ‘average’
newfile1.c:2: note: previous declaration of ‘average’ was here
I don't see what is wrong with the code. Could someone help me out?
enter code here
#include<stdio.h>
float average(float);
int main()
{
float marks[4],avg;
int i;
printf("Please enter your marks\n");
for(i=0;i<=3;i++)
{
scanf("%d",&marks[i]);
}
avg = average(marks[4]);
printf("The average marks value is %f",avg);
return 0;
}
float average(float a[4])
{
int i,sum;
float avg_m;
for(i=0;i<=3;i++)
{
sum=sum+a[i];
}
avg_m=sum/3;
return avg_m;
}
Replace
float average(float);
with
float average(float[]);
The function declaration and definition are not matching.
Then call the function like this:
avg = average(marks);
Change line in your file
float average(float);
to
float average(float []);
You have declared the function to take one float instead you want array of floats.
Also, while calling it in main, change to
avg = average(marks);
float average(float);
expects a float variable . You need to pass an array , so add
float average(float[]);. Error happened since your function declaration and definition not matching.
in your main, you should call avg = average(marks); to pass the array to function avg = average(marks[4]); will pass a single variable.
In the prototype of average, you have given float as argument type so compiler is expecting a single float value as argument. If you want to pass an array of values, you have to declare your prototype like this:
float average(float input_marks[]);
You can't give length of an array argument in a prototype or definition. You have to pass array length as a separate argument. So your prototype should look something like
float average(float a[], int a_length);
Your function average takes one float as argument, hence the declaration should be floa avaerage(float). If you do float average(float a[4]) you are telling compiler that your function takes an array of 4 floats as argument.
Related
Task: write a function to calculate the area of a square, rectangle, circle.
My code is right. IDK why I am getting this error complete error
#include<stdio.h>
#include<math.h>
int square();
float airclearea();
int rectangle();
int main(){
int s,r1,r2;
float a;
printf("Enter the side of square : ");
scanf("%d",&s);
printf("Enter the side of rectangle");
scanf("%d %d",&r1,&r2);
printf("Enter the radius of circle");
scanf("%f",&a);
printf("%d",square(s));
printf("%d",rectangle(r1,r2));
printf("%f",airclearea(a));
return 0;
}
int square(int s){
return s*s;
}
int rectangle(int r1, int r2){
return r1*r2;
}
float airclearea(float a){
return 3.14*a*a;
}
Before main you declared the function airclearea without a parameter
float airclearea();
But you are calling the function with an argument of the type float
printf("%f",airclearea(a));
In this case the compiler performs default argument promotions and in the case of the function the argument of the type float is promoted to the type double.
So the compiler expects that the function is defined with a parameter of the type double. But the function is defined with a parameter of the type float
float airclearea(float a){
return 3.14*a*a;
}
Either declare the function before main with the parameter of the type float
float airclearea( float );
or in its definition specify the parameter as having the type double.
float airclearea(double a){
return 3.14*a*a;
}
In any case it is always better to provide function prototypes before referencing to functions.
You originally defined float airclearea(); near the top of your code as having no parameters. At the bottom of your code, you redefined it as
float airclearea(float a){
return 3.14*a*a;
}
The first definition defines float airclearea();, without parameters. Replace it with float airclearea(float a);. Your parameter is float a. You haven't shown your other error messages, but I would assume you are getting the same error with int rectangle(); and int square();. Add parameters to the first definitions of both those functions, or just move the main function down to the bottom of your code and remove the top three placeholder definitions.
I am working on a problem in my textbook and I need to make a triangle angle calculator Im used to java but Im not 100% on C yet, I don't understand the logic in it.
#include <stdio.h>
static float angleB;
static float angleA;
float remainingAngle(float answer)
{
float answer= angleA+angleB;
//redefinition of answer
return 0;
}
//CANT TOUCH FROM HERE TO END
int main(int argc, const char * argv[]) {
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA,angleB);
// to many arguments to function call (referring to angleC)
printf("the third angle is %.2f\n",angleC);
return 0;
} //END CANT TOUCH
I don't know what to do here.
In your function definition
float remainingAngle(float answer)
the function remainingAngle() accepts one parameter.
OTOH, in your function call
remainingAngle(angleA,angleB);
you're passing two arguments.
The supplied argument number and type should match with the parameter list in function definition.
That said, your code is wrong.
Point 1. Your local variables will shadow the global ones. Maybe that's not what you want. Change
float angleA = 30.0;
float angleB = 60.0;
to
angleA = 30.0;
angleB = 60.0;
Point 2. The function
float remainingAngle(float answer)
{
float answer= angleA+angleB;
//redefinition of answer
return 0;
}
is logically wrong. It should rather be
float remainingAngle(void)
{
float answer= angleA+angleB;
return answer;
}
and should be called like
float angleC = remainingAngle();
AFTER EDIT:
as per your requirement, you can do
float remainingAngle(float angleA, float angleB)
{
float answer= angleA+angleB;
return answer;
}
However, this makes the global variables useless.
In addition to Souravs answer you could also do the following:
float remainingAngle(float angA, float angB)
{
float answer = angA + angB;
return answer;
}
So your call will stay the same.
And for the logical side, what you do is:
You pass two arguments (value of angleA and value of angleB) to the function remainingAngle. There you do the calculation and return the result (answer).
The design of your function isn't good:
static float angleB;
static float angleA;
float remainingAngle(float answer)
{
float answer= angleA+angleB;
//redefinition of answer
return 0;
}
What do I do?
There's no point is passing in the answer; the answer is what you want to receive. (There's also no point in passing "space" for the answer. The anser is a float, a scalar variable. Just pass that around.) Remove the answer argument.
You redefine answer as local variable. After removing the argument of the same name, that redefinition goes away. But in that case you should return answer instead of 0. You could even do without the intermediary variable answer and return the result expression. So: Return the desired expression
The answer depends on two global variables. The function should work on two angles that you pass as arguments. Provide two angle arguments to your function and remove the global variables.
And finally, the sum of the three angles in a triangle is 180°, so your remaining angle calculation is wrong.
Putting all that together:
float remainingAngle(float a, float b)
{
return 180.0f - a - b;
}
and call it like this:
float angleC = remainingAngle(angleA, angleB);
Im used to java
I'm not too familiar with Java, but I don't think functios on scalars like this one are fundamentally different in C and Java.
I want to pass a float number to another function.
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number)
}
fun(float star, float ste, int numbe)
{
//here I get "star = 0", "numbe = 10", but "ste = -1.084264e-19"
}
what is wrong here?
Thank you
It is most certainly possible to pass a float to a function, as long as you provide a prototype or move the definition ahead of the first use of the function.
A prototype (also called a forward declaration) looks like this:
void fun(float star, float ste, int number);
In larger projects, prototypes go into header files.
Note: do not forget to add void in front of the function definition as well. Otherwise, the compiler treats your function as returning an int.
If you omit the prototype, the compiler will default to using the old K&R C rules to decide how to pass arguments to your function. The result is the unusual behaviour you are seeing, and hopefully a compiler warning as well.
You need a correct function declaration before your function call:
Add:
void fun(float star, float ste, int numbe);
before your main declaration and also add void return type in your fun function definition.
you need to declare the function before main function.
Try this:
void fun(float star, float ste, int number);
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number);
}
in the code below .
i have defined function prototype with no argument
in definition as well as in function call i have used one parameter.
i would like to know why i am not getting any error ?
# include <stdio.h>
float circle(); /* no parameter*/
int main()
{
float area;
int radius =2;
area=circle(radius);
printf("%f \n",area);
return 0;
}
float circle( r) /* with one parameter even no parameter type */
{
float a;
a=3.14*r*r;
return (a);
}
The
float circle();
is not a function with zero parameters. It's a function with an unspecified number of parameters.
The
float circle( r) {
is a K&R-style definition in which the type of r defaults to int. See https://stackoverflow.com/a/18433812/367273
This is because compiler treat r as int by default when no parameter is defined for circle. Try to run your code after declaring function prototype as
float circle(void);
and you will get error.
That's because function
float circle();
declaration doesn't declare function that takes no arguments.
It's implicitly declared as a function that takes undefined number of integer variables as arguments.
Just like
function();
is valid function declaration. Implicitly this function will be treated as function taking int as arguments and returning int.
If you want to declare function function taking no arguments or not returning any value, you do it with void keyword:
void funct(void);
So my problem is, I am passing a variable by value (it is a float) from one function in my C code to another function. For some reason the variable is 0 after the pass. I have multiple other floats being passed by value, that are not 0 (all in the same function call), so I can't understand why this one is. It might be as simple as some typo that I am just not seeing:
int cuda_call(float *h_DataA, float *h_Kernel, int numSmooths, float kernelSum, int KERNEL_R, int KERNEL_W, int DATA_W){
printf("\n What am I here?: %f \n", kernelSum);
convolutionProgram(h_DataA, h_Kernel, numSmooths, kernelSum, KERNEL_R, KERNEL_W, DATA_W);
return 1;
}
extern "C" void convolutionProgram(float *h_DataA, float *h_Kernel, int numSmooths, float kernelSum, int KERNEL_R, int KERNEL_W, int DATA_W);
void convolutionProgram(float *h_DataA, float *h_Kernel, int numSmooths, float kernelSum, int KERNEL_R, int KERNEL_W, int DATA_W){
printf("\n what am I now? %f \n", kernelSum);
float
*d_DataA,
*d_DataB,
*d_Temp,
*d_Kernel;
.......
}
By the time I get to that second print in the called function, it is 0.
Maybe you have somehow ended up mixing calling conventions (ABIs). For instance, the first compilation unit may be performing a cdecl call, while the second one is compiled with fastcall.
http://en.wikipedia.org/wiki/X86_calling_conventions
Is it possible that you have not rebuilt the object file for one of the c files? This looks like a signature mismatch which can arrise from a function signature change without rebuilding both object files.