How to call a void function in C - c

Ok so I have written this code with four different functions, and the main purpose of it is to display in a table from angles 0-90 what the angle, time, distance of a velocity is. the velocity is inputed from the user.
But when I call the void function that is making the function I get an error "undefined reference to `create_table'" Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define G 9.8 /* gravitation acceleration 9.8 m/s^2 */
#define PI 3.141592654
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("please enter the velocity at which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
}
void create_table(double v)
{
printf("Angle t d\n");
printf("(deg) (sec) (m)\n");
double a,i;
for( a=0; a<=90; a+=5)
{
for(i=0; i<=2; i++)
{
double t = Projectile_travel_time(a, v);
double s = Projectile_travel_distance(a, v);
printf("%d %d %d\n", a, t, s);
}
}
}
double Projectile_travel_time(double a, double v)
{
double t = ((2*v*sin(degree_to_radian(a)))/(G));
return t;
}
double Projectile_travel_distance(double a, double v)
{
double d = ((v*v)/G)*sin(2*degree_to_radian(a));
return d;
}
double degree_to_radian(double d)
{
double r = d*atan(1) * 4 / 180;
return r;
}
any help would be appreciated.
thanks
edit I have edited the code but now have encountered another problem with my outputs being completely off. Any suggestions how my functions are incorrect?

You must keep the functions you create outside of the main function

Try to implement your functions outside the main()

You need to move the function definitions out of main. C does not support nested functions.
Edit: That is, in GCC they are, but it's not portable.

I have edited the code but now have encountered another problem with my outputs being completely off.
Change
printf("%d %d %d\n", a, t, s);
to
printf("%lf %lf %lf\n", a, t, s);
You can use %7.3f to align all the values.

Write all the function definitions create_table, Projectile_travel_time, Projectile_travel_distance and degree_to_radian outside the main.
Linker is not able to find the definition of create_table at the point at which you are calling create_table.

Related

Returns a value as an output parameter

I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}

confliction type for a user defined function in c

I am working in c after a long time.Here i have to achieve three functionality which includes
get a number and show half
2.Get the square of the number
3.Get two number and show their summation and sabtraction.
I am using devC++ and when i compile the code i get the error i mentioned in the title which conflict type if squareInput.What is wrong here:
#include<stdio.h>
#include<conio.h>
int main(){
float x;
printf("enter a number\n");
scanf("%f",&x);
//TASK 1 : display half of the number
pirntf("half of x is = %.3f",x);
//TASK 2 : square of number
squareInput(x); //call square function from here
// TASK 3 : get two numbers and display both summation and sabtraction
float num1,num2; // declare two floating number( floating numbers can hold decimal point numbers
printf("enter num1 \n");
scanf("num1 is =%f",&num1);
printf("enter num2 \n");
scanf("num2 is =%f",num2);
calculate(num1,num2);// call calculate function
getch();
}
float squareInput(float input){
float square=input*input;
printf("\n square of the number is %.3f \n",square);
return 0;
}
float calculate(float num1,float num2){
//summation
float summation= num1+num2; // declare antoher variable called summation to hold the sum
//sabtraction
float sabtraction=num1-num2;
printf("summation is %.2f \n",summation);
printf("sabtraction is %.2f \n",sabtraction);
return 0;
}
Things will go wrong without prototypes. Add
float squareInput(float input);
float calculate(float num1,float num2);
in front of int main().
If you don't declare a function before it's called, the compiler assumes it as a int-returning function. However, squareInput() return float, so the compiler(or linker, maybe) complains to you.
Also note that definitions are declarations(but not vice versa, obviously), so moving the definitions of squareInput() and calculate() in front of where they are called works too.
At the time you call squareInput and calculate, they haven't been defined yet. So C assumes an implicit declaration of int squareInput() and int calculate(). These implicit declarations conflict with the definitions of these functions.
You can fix this by either adding declarations for each of these functions before main:
float squareInput(float input);
float calculate(float num1,float num2);
Or by simply moving the functions in their entirety before main.
Be sure to add prototypes when you use a function. That way you do not need to worry too much about the order in which you call them.
Also try to separate your problems into smaller bits if you can. A comment like TAKS1 shows you that you actually want a function with that name.
#include <stdio.h>
//prototypes
void AskUserForOneNumer(float * number, const char * question );
void TASK_1(float x);
void TASK_2(float x);
void TASK_3(float a, float b);
int main()
{
float x, a, b;
AskUserForOneNumer(&x, "enter x");
AskUserForOneNumer(&a, "enter a");
AskUserForOneNumer(&b, "enter b");
TASK_1(x);
TASK_2(x);
TASK_3(a, b);
}
void TASK_1(float x)
{
printf("x = %g\n", x);
printf("0.5 * x = %g\n", 0.5 * x);
}
void TASK_2(float x)
{
printf("x = %g\n", x);
printf("x * x = %g\n", x * x);
}
void TASK_3(float a, float b)
{
printf("a = %g\n", a);
printf("b = %g\n", b);
printf("a + b = %g\n", a + b);
printf("a - b = %g\n", a - b);
}
void AskUserForOneNumer(float * number, const char * question)
{
float x;
printf("%s\n", question);
scanf("%f", &x);
printf("your input was %g\n", x);
*number = x;
}

Quadratic Equation not working properly

I am new to both this site and C programming and I am attempting to make a quadratic formula but I cannot get the roots to work. I believe I am not calling a function or perhaps there is something else wrong. Any help would be appreciated, thank you!
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
float root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is |n%fx^2%+fx%+f=0", a, b, c);
return 0;
}
float root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f,x1 , x2");
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f,x");
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2f,x3i and");
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2f,x4i");
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
You never call the function root() so it will never be executed. Put the function call somewhere before return 0; of main():
root(a, b, c);
Also, fix the printf() calls as mentioned above.
I'm not sure if this will fix everything but in your code you have your print statements formatted incorrectly. For example, this line:
printf("%.2f and %.2f,x1 , x2");
Should be printf("%.2f and %.2f", x1, x2);
The variables whose values you are trying to use should be outside of the quotation marks. Hopefully this helps.
You have several errors.
You never call root().
root is declared to return float, but it doesn't return anything, it just prints the roots. It should be declared void.
You have mistakes in many of your printf() calls. When you're displaying the equation, you have |n instead of \n, and %+f instead of +%f. When you're displaying the roots, you have the variables that you want to print inside the format string, instead of as separate arguments to printf.
Here's the corrected code.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
void root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
root(a, b, c);
return 0;
}
void root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f" ,x1 , x2);
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f",x);
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2fi and ", x3);
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2fi", x4);
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
DEMO

3 Pointers in 1 Function - Calculator doesn´t work

I've a problem with a function in my calculator-program. The function returns me back only 0 values​​. I want that:
Input:3+4
Output:7
I have worked with pointers to use the call by reference method. Please give me some tips.
The mistake is in the function readcalc. If I write the whole syntax in the main program it works.
Here is my code.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
while (restart != 'N')
{
readcalc(&a,&op,&b);
printf("%lf%lf", a, b);
printf("\n ---CALCULATOR--- \n\n\n");
switch (op)
{
case '+':printf("%lf + %lf = %lf\n", a, b, addition(a, b)); break;
case '-':printf("%lf - %lf = %lf\n", a, b, subtraction(a, b)); break;
case '*':printf("%lf * %lf = %lf\n", a, b, multiplication(a, b)); break;
case '/':printf("%lf / %lf = %lf\n", a, b, division(a, b)); break;
default:printf("bad operator!");
}
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", &restart);
if (restart != 'Y'&&restart != 'N')
{
printf("Bad input!");
}
}
fflush(stdin);
getchar();
return 0;
}
double readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", &x, &opp, &y);
return 0;
}
double addition(double a,double b)
{
double c = 0;
c = a + b;
return c;
}
double subtraction(double a, double b)
{
double c = 0;
c = a - b;
return c;
}
double multiplication(double a, double b)
{
double c = 0;
c = a*b;
return c;
}
double division(double a, double b)
{
double c = 0;
c = a / b;
return c;
}
What can I change?
The problem with readcalc is that you pass the pointer to the pointer as argument to scanf. The variables are already pointers, so you don't have to use the address-of operator to get a pointer, as then you get pointers to the pointers.
Also be careful with the scanf format, as the "%c" format doesn't skip leading whitespace, so if you enter e.g. 1 +2 then the scanf call will not be able to read the operator or the second number.
void readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", x, opp, y); // <== no &'s
}
Your readcalc function invokes
scanf("%lf%c%lf", &x, &opp, &y);
on pointers, int *x, etc...
When you place it inline in main, it works on the addresses of the variables.
Change it to
scanf("%lf%c%lf", x, opp, y);
At a suitable warning level, you may have seen warning.
While you are there, readcalc returns a double, which you never use. Perhaps it should simply be void?

Call by Reference Function

I would just like a push in the right direction here with my homework assignment. Here is the question:
(1) Write a C function called input which returns void, this
function prompts the user for input of
two integers followed by a double
precision value. This function reads
these values from the keyboard and
finds the product of the two integers
entered. The function uses call by
reference to communicate the values of
the three values read and the product
calculated back to the main program.
The main program then prints the
three values read and the product
calculated. Provide test results for
the input: 3 5 23.5. Do not use arrays
or global variables in your program.
And here is my code:
#include <stdio.h>
#include <stdlib.h>
void input(int *day, int *month, double *k, double *pro);
int main(void){
int i,j;
double k, pro;
input(&i, &j, &k, &pro);
printf("%f\n", pro);
return 0;
}
void input(int *i, int *j, double *k, double *pro){
int x,y;
double z;
double product;
scanf("%d", &x);
scanf("%d", &y);
scanf("%f", &z);
*pro += (x * y * z);
}
I can't figure out how to reference the variables with pointers really, it is just not working out for me.
Any help would be great!
You adding to pro but that is not initialized, you are not passing values back apart from pro. You store values into the addresses of variables passed in. In that case you need to dereference pointers to access/retrieve value, *i, and in your method use the passed addresses directly - then you don't need to take address of them again.
This works - I replaced double with float ... :
#include <stdio.h>
#include <stdlib.h>
void input(int *day, int *month, float *k, float *pro);
int main(void){
int i,j;
float k, pro;
i = j = k = pro = 0;
input(&i, &j, &k, &pro);
printf("%f\n", pro);
printf("%d : %d : %f\n", i,j,k);
return 0;
}
void input(int *i, int *j, float *k, float *pro){
scanf("%d", i);
scanf("%d", j);
scanf("%f", k);
printf("%d - %d - %f\n", *i,*j,*k);
*pro += (*i * *j * *k);
}
Output:
1
2
3.5
1 - 2 - 3.500000
7.000000
1 : 2 : 3.500000
You're almost there, but instead of making new variables x, y, and z, use the pointers you passed:
scanf("%d", i);
scanf("%d", j);
scanf("%f", k);
*pro += ((*i) * (*j) * (*k));
When reading the numbers in the input function you can make use of the pointers iptr, jptr, kptr and proptr to read the values directly into variables i,j and k declared in the main function as:
void input(int *iptr, int *jptr, double *kptr, double *proptr){
scanf("%d", iptr); // read directly into i using pointer to i.
scanf("%d", jptr);
scanf("%f", kptr);
*proptr = ( (*iptr) * (*jptr) ); // compute product and assign to pro.
}
*pro += (x * y * z);
This is going to break horribly. You're adding the product to whatever garbage happens to be in pro beforehand. You want to remove the +, i.e.:
*pro = (x * y * z);
What your program is not doing is setting the values of the input to i, j, and k.
Instead of using x,y and z, use the parameters instead.

Resources