return code misunderstanding - c

#include<stdio.h>
int calsum(int x,int y,int z);
int main()
{
while(1)
{
int a, b, c, sum;
printf("Enter any3 numbers");
scanf("%d%d%d", &a, &b, &c);
sum = calsum(a, b, c);
printf("sum=%d\n", sum);
}
}
int calsum (int x, int y, int z)
{
int d;
d = x + y + z;
if(d > 2)
return d;
else
d = 1;
return;
}
when I am giving input as -1 1 0 my output should be 1 but it is giving 0
why?
it is all about adding three numbers

int calsum (int x,int y,int z)
{
return ;
}
Your function is declared and defined to return an int, but your return statement is expressionless. It's a language constraint violation.
The behavior of your program is undefined. Funny results is a possible outcome in this case.

Update your calsum function as below. You are assigning d=1 in else part but not returning it.
int calsum (int x,int y,int z){
int d;
d=x+y+z;
if(d>2)
return d;
else
return 1;
}

Related

Calculate power of all the numbers till n-1

Given n, the program should calculate 1^1 + 2^2 + 3^3 + ... till n-1^n-1. Below is my code, in which there is one function inside while loop which and the passed value is from n-1 in the function. The function definition has two variables which return the ans. Output is wrong always 1.
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
la= (x*power(x, y-1));
ans+=la;
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, a, b, res, res1;
scanf("%d%d", &n, &m);
while(n-- && n>0)
{
a = power(n-1, n-1);
}
printf("%d", a);
}
return 0;
}
Some problems in your code.
As pointed in another answer, your power function was broken:
ans was not initialized
{ } were missing after the else
in the while, you compute x^x, but you forget the result, whearas you
should sum it.
first thing you do in while loop is to decrease n and to compute power(n-1, n-1)
that sound not logical.
Hence, your corrected code could be:
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
if(y==0)
return 1;
else
return x*power(x, y-1);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, b, a = 0;
scanf("%d%d", &n, &m);
while(n>1)
{
--n;
b = power(n, n);
a += b;
printf("%d^%d -> %3d\n",n, n, b);
}
printf("sum= %d", a);
}
return 0;
}
Gives for n = 6:
5^5 -> 3125
4^4 -> 256
3^3 -> 27
2^2 -> 4
1^1 -> 1
sum=3413
C uses braces to form blocks, your power() function looks like it's wanting to use indentation like in Python.
It should probably be:
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
{
la= (x*power(x, y-1));
ans+=la;
return ans;
}
}
Of course since the first if has a return, the else is pointless, and you can simplify the code:
int power(int x, int y)
{
if (y==0)
return 1;
return x * power(x, y-1);
}
The variable ans was never assigned to, that looked broken so I simplified it out.
Of course this is susceptible to integer overflow.

C functions in finding a max value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to write a C Function that takes three integers as arguments and returns the value of the largest one.
int largest(int x,int y,int z)
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);
if(y>=x && y>=z)
printf("Largest number = %d", y);
if(z>=x && z>=y)
printf("Largest number = %d", z);
}
I have tried this codes but they don't work i need help please I am also a beginner at this
This should work fine.
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z){
int max;
max=x;
if(y>max){
max=y;
}
if(z>max){
max=z;
}
return max;
}
Try this one:
#include <stdio.h>
int largest(int x, int y, int z);
int main() {
int val1, val2, val3;
int maximum;
printf("enter value \n");
scanf("%d", &val1, &val2, &val3);
maximum = largest(val1, val2, val3);
printf("the largest integer is %d = \n", maximum);
return 0;
}
int largest(int x, int y, int z){
if (x >= y && x >= z)
return x;
if (y >= x && y >= z)
return y;
// otherwise
return z;
}
The problem is that you wanted the method to return the largest value, but simply didnt do that - the code is not compiling because the largest function is defined to "return" an int but there's no return statement anywhere in your function.
If you dont know what exactly "returning function" is then take a look at this tutorial: http://www.cplusplus.com/doc/tutorial/functions/
This is a pretty short way of doing what you want:
int largest(int a, int b, int c)
{
a = (a > b) ? a : b;
a = (a > c) ? a : c;
return a;
}
To return a value you must use a return statement in function.
Let us inspect what you've done,
#include<stdio.h>
int largest(int x,int y,int z)/* missed ';' */
/* missed 'void main()' */
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3); /* missed format specifier for other two values */
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);/* written print statement instead of return statement */
if(y>=x && y>=z)
printf("Largest number = %d", y);/* written print statement instead of return statement */
if(z>=x && z>=y)
printf("Largest number = %d", z);/* written print statement instead of return statement */
}
After modification the code should be like this,
#include<stdio.h>
int largest(int x,int y,int z);
int main()
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
return x;
if(y>=x && y>=z)
return y;
if(z>=x && z>=y)
return z;
}

Calculating power raised to a number

I am calculating the power of x raised to n. I can't understand one thing: why is it showing segmentation fault when I am both declaring and initializing the temp variable at the start? I do know what segmentation fault is, but why is it showing.
#include<stdio.h>
int power(int x,unsigned int y)
{
int temp=power(x,y/2);
if(y==0)
return 1;
if(y%2==0)
return temp*temp;
else
return x*temp*temp;
}
//Driver function
int main(int u, int v)
{
printf("Enter the value of u and v");
scanf("%d %u",&u,&v);
printf("%d",power(u,v));
return 0;
}
You will recurse infinitely. You need a small adjustment [please pardon the gratuitous style cleanup]:
#include <stdio.h>
int
power(int x, unsigned int y)
{
//int temp = power(x, y / 2);
if (y == 0)
return 1;
int temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
// Driver function
int
main(int argc,char **argv)
{
int u;
unsigned int v;
printf("Enter the value of u and v");
scanf("%d %u", &u, &v);
printf("%d\n", power(u, v));
return 0;
}

C- function for printing each base exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3

I can't seem to get my program running
Write a function integerPower(base, exponent) that
returns the value of
Baseexponent. For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that
exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use for to
control the calculation.
Do not use any math library functions.
i have this program
#include<stdio.h>
int power(int b, int e){
int x
for (x = 0; x <= e; x++)
b=b;
return b;
}
void main(){
int b = 0;
int e = 0;
scanf("input a base %d\n", &b);
scanf("input an exponent %d\n", &e);
power(b,e);
printf("%d" , b);
}
In the loop
for (x = 0; x <= e; x++)
b=b;
the line b=b; is useless. It just assign the value of b to itself. You need to multiply b by e times. For this you need to take another variable with initial value 1 and multiply it by b at each iteration of loop to get be.
Change your function to this
int power(int b, int e){
int x, y = 1;
for (x = 1; x <= e; x++)
y = y*b; // Multiply e times
return y;
}
#include<stdio.h>
int power(int b, int e){
int x;
int result = 1;
for (x = 0; x < e; x++)
result*=b;
return result;
}
int main(){
int b = 0;
int e = 0;
printf("Input a base ");
scanf("%d", &b);
printf("Input an exponent ");
scanf("%d", &e);
b = power(b,e);
printf("%d" , b);
return 0;
}
First problem is with the scanf function. you are using "input a base ". For this to output you have to use printf("input a base ").
Try using the below function to calculate the value of base raised to the power of exponent
int ipower(int b, int e)
{
int x, tmp = 1;
for (x = 0; x < e; x++)
tmp *= b;
return tmp;
}
Here the for loop is iterated for e times that is from 0 to e-1. "tmp" will hold the result.
Make sure you don't change the value of "b/e"(base/exponent) inside the loop, else will lead to wrong result.
doing b*=b in function body won't help either. And why initialize b and e to zero when you're supposed to input them from the user?
Try this:
#include<stdio.h>
int power(int b, int e) {
int temp = b;
int x;
for (x = 1; x < e; x++)
b *= temp;
return b;
}
void main() {
int b ;
int e;
scanf_s(" %d", &b);
scanf_s(" %d", &e);
int h= power(b, e);
printf("%d", h);
}
int power(int b, int e){
int x,t=1
for (x = 1; x <= e; x++)
t*=b;
return t;
}
if user give e= 1 then also its work.

how to make a array of pointer to call func pointer?

i have code to array of func pointer
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
code for array of pointer to function:
#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
int i = 0;
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int);
pfun[0] = sum;
pfun[1] = product;
pfun[2] = subtract;
for( i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b);
printf("\nresult = %d", result);
}
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int subtract(int x, int y)
{
return x - y;
}
now how to combine this two program. such that array of pointers pointing to func pointers and the func pointers may have different number of args? any suggestion.
You not only need to store function pointers with a variable number of arguments (that is not very difficult, you could use a union for instance), but you also need to make sure you call the functions with the correct argument, and that is a bit trickier given your design.
I suggest to use a stack instead. All your functions would only take the stack as an argument:
void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
And your array could be declared this way:
typedef void callback_t(stack_t *);
callback_t *p[] =
{
sum,
subtract,
product,
/* ... */
};
Then for instance sum would be implemented as such:
void sum(stack_t *stack)
{
if (depth(stack) < 2)
perror("Not enough arguments in stack!");
int b = popstack(stack);
int a = popstack(stack);
int c = a + b;
pushstack(stack, c);
}
But unary minus would be implemented this way:
void neg(stack_t *stack)
{
if (depth(stack) < 1)
perror("Not enough arguments in stack!");
int a = popstack(stack);
pushstack(stack, -a);
}
Each function decides how many arguments they need. The caller does not need to know.

Resources