Write a program with three functions, call the first function from main - c

a. In the first function print “Hello, I’m in function 1”. Call the second function.
b. In the second function ask the user to enter a float value. Call a third function and pass the value to it.
c. In the third function calculate the square root of the number. Return the square root to the second function, then the first function and then to main.
d. Print the square root in main in TWO decimal places.
Please correct my coding for since I'm not getting the desired output.
float a(float);
void b(float);
void c(float);
void main(float d)
{
printf("In main\n");
printf("back in main \n");
printf("The square root is %f", d);
}
float a(float c)
{
printf("Hello,I'm in function 1");
return (c);
}
void b(float b)
{
float number;
printf("in function 2");
printf("enter a float value");
scanf("%f", &number);
c(number);
return a(number);
}
void c(float a)
{
float power;
printf("in function 3");
power = sqrt(a);
return b(power);
}

What libraries have you included? stdio.h is required for printf function and math.h is also required to implement the sqrt() function.
#include <stdio.h>
#include <math.h>
Your functions are incorrectly defined:
a requires no input and returns no data so it should be void
b returns float variable so it should be labelled as such.
c has type float for input as well as output
void a(void);
float b(void);
float c(float);
printf is not the way to call your functions.
You simply type the name of the function and arguments if you have any into your main function in the order you wish to call them.
example:
void main()
{
//declare your variable
float num;
//calls a function
a();
//calls b function and initializes num as value returned from b
num = b();
//calls c function and passes variable num to parameter in c function
c(num);
}
This should get you headed in the right direction.

Related

C - Scanf straight into an function argument?

So, if I have the function foo:
float foo(float a, float b){
return a*b;
}
and I call it from another function, how can I call it like this?
void main(){
foo(scanf("%d"), scanf("%d");
}
scanf doesn't return the input string, I don't want to create a bunch of temp variables. Is that possible, and if so how?
You can't directly return a value from scanf. But you can create a function yourself that returns a value from scanf, but it'll still use temp variables inside of it:
int getInteger() {
int input;
scanf("%d", &input);
return input;
}
Then you can use it like:
foo(getInteger(),getInteger());
However, if you really don't wanna use variables, you can just use get_int() from cs50 library. More info here.
#include <cs50.h> // include cs50 to use get_int
int main(void)
{
foo(get_int("Number 1: "),get_int("Number 2:"));
return 0;
}

How to call different functions on different int values in c

In an interview I was asked to write an efficient program such that for each integer value given, a function is called.
For example: if user enters 1 then function one will get executed if user enters 5 then function five gets executed and so on..
I was not able to come up with an efficient idea, like we can't use an if statement for checking the value entered and then executing the corresponding functions.
Is there any way we can do this in an efficient manner?
One of the option would be using function pointers, as below.
Declares a type of a void function
typedef void (*fp)();
define your functions
void func1()
{}
.
.
.
void func10()
{}
declare array of function pointers like below
fp function_pointers[10]; //declares the array
Initialize the function pointers like below
function_pointers[0] = func1()
.
.
function_pointers[9] = func10()
Call function using function pointers like below
for (int i=0;i<10;i++)
function_pointers[i]();
Use function pointers.
Simple example:
#include <stdio.h>
//pointer to a void function declaration
typedef void(*someFunc)();
void f1() {
printf("f1 function call\n");
}
void f2() {
printf("f2 function call\n");
}
void f3() {
printf("f3 function call\n");
}
//array of function pointers
const someFunc funcTable[] = {
f1,
f2,
f3
};
int main() {
uint number = 0;
printf("Enter 0, 1 or 2\n");
scanf("%u",&number);
if(number < 3) {
funcTable[number]();
}
}

C - Function which takes 2 floats and returns their difference after the block using pointers

I'm trying to write a function which takes two generic floats (make it 'a' and 'b') and returns their difference assigned to the variables, so a=a-b and b=b-a.
It should print the values with 2 decimal places and it must work after I exit the block... What is wrong with my code?
#include <stdio.h>
float diff_abs (float *a, float *b) {
*a= *a-*b;
*b= *b-*a;
}
int main(void) {
float c, d;
scanf("%f", &c);
scanf("%f", &d);
printf("%.2f\n%.2f", diff_abs(&c, &d));
return 0;
}
I would appreciate your suggestions, thank you.
First, you are changing a value then using it to the next calculation so simplify change your function to:
void diff_abs (float *a, float *b) {
*a= *a-*b;
*b = -*a
}
Second, since you can't return multiple variables in a C function, one of the solution is to use void returning function but change variables while using pointers (like you're somehow doing)
Finally, since your function returns void you can't use it as floats in the printf function so just use c and d
like this:
printf("%.2f\n%.2f",c,d);
of course don't forget to call:
diff_abs(&c, &d)
before trying to print
you can refer to this topic for returning multiple variables in C

printing by calling a function (pass by value) in C

I am trying a write a program that prints a pyramid by calling a function (pass by value and not pass by address).
Well, the function is just supposed to run a for loop to print the pyramid but the actual printing statement is written in the main. The rest of the program seems fine but there is an error in the function call.
Can you please tell me what I am doing wrong? It is the syntax that I am having trouble with. The error is "argument of type void is incompatible with parameter type of 'constant char*'"
#include<stdio.h>
void pyramid(int); //function declaration
int main()
{
int r1;
printf("Enter the number of rows you would like printed:");
scanf("%d",&r1);
printf(pyramid(r1)); //function call
return 0;
}
void pyramid(int r2) //function definition
{
int i,j;
for(i=r2;i>=1;i--)
{
for(j=r2;j<=i;j--)
{
printf("*");
}
printf("\n");
}
}
void pyramid(int); //function declaration
Here the function pyramid will not return any value,
But you are expecting it to return a value in the below statement
printf(pyramid(r1)); //function call
Also this program would run in to an infinite loop,
Here:
for(i=r2;i>=1;i--)
{
for(j=r2;j<=i;j--) //**j will always be less than i**
{
printf("*");
}
printf("\n");
}
printf's prototype is
int printf(const char *restrict format, ...);
But you are trying to pass void to it by calling printf(pyramid(r1));. Call function directly like pyramid(r1); instead of printf(pyramid(r1));.

Cannot get function in C to execute properly

I have a simple function that I am trying to create. I need this function to add 6 to whatever number the user decides to enter into the program. I have been working with this code for an hour now and cannot figure out what I'm doing wrong, even after looking at multiple examples from the course I am taking. I really appreciate the help.
Here is my code:
#include <stdio.h>
void closing(void);
void addSix(void);
int x;
int result;
int main()
{
int x;
printf("Please enter a number to add to 6: ");
scanf("%d", &x);
getchar();
addSix();
closing();
closing();
return 0;
}
void closing(void)
{
printf("That's all folks.\n");
}
void addSix(void)
{
int result = x+6;
printf("Result: %d\n", result);
}
You've got both a global x variable outside of main(), and a local x inside of main(). The code inside of main() writes to the local x while the code in addSix() reads from the global x.
Remove the int x declaration in main() so that both places access the global x.
You have x declared as both a global variable and variable local to main. When you pass x by pointer to scanf, it refers to the local variable, not the global.
You can remove the local declaration of int x in main but this isn't really the best solution. Generally, global variables should be avoided when possible (though they are, of course, sometimes necessary/the best tool for the job).
The best solution in this case is to make x a parameter to addSix(). There are a few options here:
You can have addSix return the sum and then use that return value
You can pass the address of x to addSix and have the function modify x itself by using a pointer.
The former would look like this:
int addSix(int x) {
return x + 6;
}
The latter would look like this:
void addSix(int * x) {
*x += 6;
}
You have two variables named x. One is a local to main, and the other is a global variable.
The function addSix cannot see the local in main. It can only see the global variable.
You should change addSix so that it is passed the value as a parameter.
void addSix(int x)
{
printf("Result: %d\n", x+6);
}
Call the function like this:
addSix(x);
Or perhaps you want your function to return a value:
int addSix(int x)
{
return x+6;
}
Which you can call like this:
int result = addSix(x);
Both of your global variables are needless. Remove them.
You have a global x, and a local x variable in which you actually save the user input.
Using only the global variable will do the trick for you
#include <stdio.h>
int x;
void addSix(void)
{
int result = x+6;
printf("Result: %d\n", result);
}
int main()
{
printf("Please enter a number to add to 6: ");
scanf("%d", &x);
getchar();
addSix();
return 0;
}

Resources