Calculate with 2 functions - c

I am a Beginner in Functions.I wanna integrate that Functions in my main program. The program should scan a int number and then square it(sum=b*b). Then the program should output the result.
#include <stdio.h>
#include <stdlib.h>
void funktion(int);
void out(int);
int main(int)
{
int sum,b,v,w,z;
{
funktion();
calculator();
out();
printf("%i",sum);
}
return 0;
}
void funktion(int v)
{
printf("Enter any number that is to be squared!");
}
void calculator(int w) //calculate b*b
{
scanf("%i",&b);
sum=b*b;
}
void out(int z)
{
printf("Sum:");
}
Please give me some tips. ;)
Thx & Best regards!

Are you looking for this kind of solution: i just made little changes. you may need to do some initializing things if u gonna use this code
#include <stdio.h>
#include <stdlib.h>
void funktion(int);
void out(int);
int main()
{
int sum,b;
{
printf("Enter any number that is to be squared!");
scanf("%i",&b);
sum = funktion(b);
out(sum);
}
return 0;
}
int funktion(int b)
{
return b*b;
}
void out(int sum)
{
printf("Sum:%d",sum);
}

void funktion(int);
void out(int);
These two functions need not contain any arguments as you declared to be int's, because you are not performing any computation on sum, since they are just prints inside those functions.
So make them as,
void funktion(void);
void out(void);
and call those routines as,
funktion();
out();

void funktion(int)
{
printf("Enter any number that is to be squared!");
}
void out(int)
{
printf("Sum:");
}
to call these functions you are passing some value so you receive them with parameters.
void funktion(int a)
{
printf("Enter any number that is to be squared!");
}
void out(int b)
{
printf("Sum:");
}
You can make use of the passed value in functions using these variables, a nd b in corresponding functions.
If you dont want to access those passed values in the called functins then you dont need to pass the value. So change the functions to receive nothing, it can be
#include <stdio.h>
#include <stdlib.h>
void funktion();
void out();
int main()
{
int sum,b;
{
funktion();
scanf("%i",&b);
sum=b*b;
out();
printf("%i",sum);
}
return 0;
}
void funktion()
{
printf("Enter any number that is to be squared!");
}
void out()
{
printf("Sum:");
}
make the call to function without passing arguments. you can even specify void as type to indicate that function doesn't take any parameters.
To calculate in other function and return the value use return statement.
int calculator() //calculate b*b
{
scanf("%i",&b);
sum=b*b;
return sum;
}
change the function call statement to,
sum=calculator();

Related

How to get value macro in C?

I'm having trouble with macros in C. Is there any way to call and get the value of a parameter in macro to call a function?
The example code below generates an error:
#define order(i) fruits_i##_banana()
void fruits_1_banana()
{
printf("Order banana\n");
}
int main()
{
int a = 1;
order(a);
}
You need to use ## before and after i.
#define order( i ) fruits_##i##_banana()
void fruits_1_banana()
{
printf("Order banana\n");
}
int main()
{
order(1);
}
Note that you cannot pass a to order because macro expansion doesn't take the value of a variable, it just uses the variable name as it is.
References: https://learn.microsoft.com/en-us/cpp/preprocessor/token-pasting-operator-hash-hash?view=msvc-160
Instead, you can use an array of function pointers:
#include <stdio.h>
static void fruits_0_banana(void)
{
printf("Order banana 0\n");
}
static void fruits_1_banana(void)
{
printf("Order banana 1\n");
}
static void (*order[])(void) = {
fruits_0_banana, // Or NULL if you don't need it
fruits_1_banana,
// ...
};
int main(void)
{
int a = 1;
order[a]();
return 0;
}

How to point to a pointer of a pointer

I'm writing a program that can modify rows and cols from a function of a function. I don't understand how to do pointer of a pointer.
void changeNum(int*setRows, int *setCol)
{
changeNum2(*setRows,*setCol);
}
void changeNum2(int*setRows, int *setCol)
{
*setRows=5;
*setCol=5;
}
int main() {
int*row=10;
int* col=10;
changeNum(&row,&col);
printf("%d %d",row,col);
return 0;
}
First
int*row=10;
int* col=10;
This is wrong. Assigning hardcoded address. You don't want this
int row=10;
int col=10;
How to get the address of the row and col?
&row and &col.
How to pass it to function?
Call it, changeNum(&row,&col);
void changeNum(int*setRows, int *setCol)
{
...
}
How to pass pointer to pointer?
void changeNum(int*setRows, int *setCol)
{
chnageNum2(&setRows, &setCol);
}
ChangeNum2 how it would change value?
void chnageNum2(int **setRows, int **setCol){
**setRows = 110;
**setCol = 110;
}
Can we do the same change using changeNum() only?
Yes we can do that.
void changeNum(int*setRows, int *setCol)
{
*setRows = 110;
*setCol = 110;
}
Definitely check this. Grab a book. It will help a lot.
The complete code will be
void changeNum(int*setRows, int *setCol)
{
changeNum2(&setRows,&setCol);
}
void changeNum2(int**setRows, int **setCol)
{
**setRows=5;
**setCol=5;
}
int main(void) {
int row=10;
int col=10;
changeNum(&row,&col);
printf("%d %d",row,col);
return 0;
}
#include <stdio.h>
void changeNum(int*, int*);
void changeNum2(int*, int*);
void changeNum(int* setRows, int *setCol) {
changeNum2(setRows,setCol);
}
void changeNum2(int* setRows, int *setCol) {
*setRows=5;
*setCol=5;
}
int main() {
int row=10;
int col=10;
changeNum(&row, &col);
printf("%d %d\n", row, col);
return 0;
}
It takes sometime to grasp that every C function parameter is passed by value. So you can safely pass setRows pointer to the second function simply by its value.
Also, it's necessary to declare previously the function changeNum2, I've included the declaration without parameter names to clarify it's possible.
I strongly recommend reading this book: https://en.wikipedia.org/wiki/The_C_Programming_Language, specially chapter 5 (Pointers and arrays).
You can find a PDF copy easily. It's where I finally learned this concept.

Invalid conversion from int* to int using functions

I have this "simple" problem: I have in input 2 int numbers and i must output them in decreasing order.
#include <stdio.h>
#include <iostream>
int fnum()
{
int NUM;
scanf("%d",&NUM);
return NUM;
}
void frisultato(int x,int y)
{
if (x>y)
{
printf("%d",x);
printf("%d",y);
}
else
{
printf("%d",y);
printf("%d",x);
}
return;
}
int main()
{
int A,B;
A=fnum;
B=fnum;
frisultato(A,B);
}
I recieve an error at
A=fnum;
B=fnum;
my compiler says: invalid conversion from int(*)() to int.
This is the first time i use functions, what is the problem? Thank you!
Michelangelo.
A=fnum;
B=fnum;
You're not actually calling the function fnum here. You're attempting to assign a pointer to the function to the int variables A and B.
To call the function, do this:
A=fnum();
B=fnum();
Sorry, but since you seem to be new at programming, I couldn't help but refactor/comment on your code:
#include <stdio.h>
#include <iostream>
int fnum()
{
int num;
scanf("%d",&num);
return num;
}
void frisultato(int x, int y)
{
if (x>y)
{
printf("%d",x);
printf("%d",y);
}
else
{
printf("%d",y);
printf("%d",x);
}
/* No need to return in void */
}
int main()
{
/*
Variables in C are all lowercase.
UPPER_CASE is usually used for macros and preprocessor directives
such as
#define PI 3.14
*/
int a, b;
a = fnum(); //Function calls always need parenthesis, even if they are empty
b = fnum();
frisultato(a, b);
/*
Your main function should return an integer letting whoever
ran it know if it was successful or not.
0 means everything went well, anything else means something went wrong.
*/
return 0;
}
Also, don't sign your name on StackOverflow questions.

Recursive Function of Characters To Binary (ASCII)

I bought a Programming book at a yard sale for $2 because I've always wanted to learn how to code but don't have the money and resources for school. I've gotten through the first few chapters just fine, but I've also had the solutions to the problems I was working on. But the chapter is missing a few of the pages after the chapter summary when they start listing problems. I was wondering if you guys could help me out.
Here is the problem. Note: Needs to use a recursive function.
#include <stdio.h>
#include <stdlib.h>
void binaryPrinter(int value, int *numberOfOnes);
void print(char c);
//You do not need to modify any code in main
int main()
{
char value;
int result = 1;
while(result != EOF)
{
result = scanf("%c",&value);
if(result != EOF && value != '\n')
{
print(value);
}
}
}
//#hint: This is called from main, this function calls binaryPrinter
void print(char c)
{
}
//#hint: this function is only called from print
void binaryPrinter(int value, int *numberOfOnes)
{
}
void print(char c)
{
int n = CHAR_BIT;
binaryPrinter((unsigned char)c, &n);
putchar('\n');
}
void binaryPrinter(int value, int *numberOfOnes)
{
if((*numberOfOnes)--){
binaryPrinter(value >> 1, numberOfOnes);
printf("%d", value & 1);
}
}

Getting a sub-function to modify variables

This is a simple test program where I am trying to get the func function to modify the variables a and b which are then used in the main function. Is there a way to get func to return the modified variables so they can be used? (preferably without using struct as I don't understand how it works)
#include <stdio.h>
void func(int a, int b)
{
a=a+1;
b=b+1;
}
void main(void)
{
int a=0, b=0;
while (1)
{
func(a,b);
printf("%d\n",a);
}
}
If you want to modify variables in the calling function, you have to pass their address to func (i.e. pass a pointer to these variables
void func(int* a, int* b)
{
*a=*a+1;
*b=*b+1;
}
func(&a,&b);
Your code passes its arguments by value. This means that a and b are copied into new variables which are initialised with the values of the calling variables but only exist for the duration of func.
I agree. The best way to do this is using pointers. But are you aware that your code will never terminate? You should use a condition that can end excution of the while loop.
Also, if you wanna perfom the exact same action to both variables you could just use the return statement like this:
#include <stdio.h>
void func(int a, int b)
{
return a+1;
}
void main(void)
{
int a=0, b=0;
while (a < 5)
{
func(a);
func(b);
printf("%d\n",a);
}
}
We can achieve through pointers . Don't use While(1) because it will go to infinite loop. I think following program help to you.
#include <stdio.h>
void func(int* a, int* b)
{
*a=*a+1;
*b=*b+1;
}
void main(void)
{
int a=0, b=0;
//while (1)
// {
func(&a,&b);
printf("%d\n",a);
// }
}

Resources