This question already has answers here:
permanently changing value of parameter
(6 answers)
Closed 8 years ago.
I don't get any errors but I do not get the correct value. It keeps on printing 0! It looks like it's not reading my function and I really don't know what it could be.
#include <stdio.h>
#include <math.h>
int rec(int base, int ex,int ans);
int main()
{
int base;
int ex;
int ans;
for(ex=2;ex!=1;){
printf("Enter a base and an exponent\n");
scanf("%d %d",&base,&ex);
rec(base,ex,ans);
printf("%d raised to the %d is %d \n", base, ex, ans);
}
return 0;
}
int rec(int base, int ex,int ans)
{
ans=pow(base, ex);
return ans;
}
You have two different ans in your code and you are interpreting them incorrectly. Assign the return value of rec to ans and delete the one in rec, since it is pointless. Here we go:
int main() {
int base;
int ex;
int ans;
for(ex=2; ex!=1;) {
printf("Enter a base and an exponent\n");
scanf("%d %d",&base,&ex);
ans = rec(base,ex);
printf("%d raised to the %d is %d \n", base, ex, ans);
}
return 0;
}
int rec(int base, int ex) {
return pow(base, ex);
}
Related
the following is given
#include <stdio.h>
void func2 (int num, int *result);
int main()
{
int num, result;
printf("Enter a number: \n");
scanf("%d", &num);
func2(num, &result);
printf("func2(): %d\n", result);
return 0;
}
void func2(int num, int *result)
{
//key in code
}
in the void func2 i wrote
int i=0;
result=&i;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
but the programming is not returning the value of variable i properly. what's wrong with my variable assignment?
expected output:
Enter a number:
24 (user enter)
func2(): 20
actual output:
Enter a number:
24 (user enter)
func2(): 32767
You need to assign indirectly through result, not set result to the address of another variable.
int i=0;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
*result = i;
This my first program of C programming using recursive functions, it calculates the sum of the first n natural numbers. I am not able to get an output from it, it asks for the number but after that the program doesnt respond, can someone please help me out?
int n();
int main(){
int num;
printf("Enter num:\n");
scanf("%d", &num);
n(num);
printf("The sum of %d is: %f", num, n(num));
return 0;
}
int n(int x){
if (x != 0){
return n(x) + n(x-1);
**strong text** }
else{
return x;
}
}
Firstly, in the recursive function, return n(x) + n(x-1); should have been return x + n(x-1); as in the first case, n(x) will continuously make a called to another n(x), therefore making an infinite loop, or, more formally, return a 0xC00000FD exception, a Stack Overflow exception.
Also, in the last printf() function, %f should have been %d:
#include <stdio.h>
int n(int x)
{
if (x > 0) {return x + n(x-1);} return x;
}
int main()
{
int num;
printf("Enter num: ");
scanf("%d", &num);
printf("The sum of %d is: %d", num, n(num));
return 0;
}
Using %f to print an integer will caused an undefined behavior, because %f is a float format specifier, as noted here.
If you really want to convert it to a float:
#include <stdio.h>
int n(int x)
{
if (x > 0) {return x + n(x-1);} return x;
}
int main()
{
int num;
printf("Enter num: ");
scanf("%d", &num);
printf("The sum of %d is: %f", num, (double) n(num));
return 0;
}
*Note: Ran on Code::Blocks 20.03, Windows 10 64bit.
More info on format specifiers : https://www.tutorialspoint.com/format-specifiers-in-c
It should be return x instead of calling the same function,
you can use type conversion for changing the integer into a float,
int n();
int main(){
int num,x;
printf("Enter num:\n");
scanf("%d", &num);
x=n(num);
printf("The sum of %d is: %f", num, float(x));
return 0;
}
int n(int x){
if (x != 0){
return x + n(x-1);
**strong text** }
else{
return x;
}
}
This question already has answers here:
C function declaration
(4 answers)
Closed 8 years ago.
This is a small program to multiply and add two numbers using functions..
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,result;
clrscr();
printf("Enter two numbers to be added and multiplied...\n");
scanf("%d%d",&a,&b);
add(a,b);
getch();
return 0;
}
int add(int a,int b)
{
int res;
printf("%d + %d = %d", a,b,a+b);
res=mult(a,b);
printf("\n%d * %d = %d",a,b,res);
return 0;
}
int mult(int a,int b)
{
return a*b;
}
Although, I don't think that I need to have a return type add function,so I tried to use this code...
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,result;
clrscr();
printf("Enter two numbers to be added and multiplied...\n");
scanf("%d%d",&a,&b);
add(a,b);
getch();
return 0;
}
void add(int a,int b)
{
int res;
printf("%d + %d = %d", a,b,a+b);
res=mult(a,b);
printf("\n%d * %d = %d",a,b,res);
}
int mult(int a,int b)
{
return a*b;
}
but it tells me that there's an error for mismatch type declaration??
You need to provide a prototype before the first use:
void add(int a, int b); /* This tells the compiler that add() takes */
/* two ints and returns nothing. */
int main() {
...
add(a, b);
}
void add(int a, int b) {
...
}
Otherwise the compiler is obliged to assume that add() returns an int.
For more information, see Must declare function prototype in C?
First, you have to put declaration of functions before using it:
void add(int a,int b);
int mult(int a,int b);
Then you can put its definition wherever you want.
Or you can declare & define them before using.
#include<stdio.h>
#include<conio.h>
int adder(int,int);
void main()
{
int a,b;
printf("enter nos");
scanf("%d%d",&a,&b);
adder( a,b);
printf("sum is %d",adder);
getch();
}
int adder(int x,int y)
{
return x+y;
}
this program is not working.I think the code is right.Can you point out the error?
adder is a function, what you should printf is its return value.
And as #JonathanLeffler said, it's better to add a newline at the end if you want to ensure the output appears timely. So,
change
adder( a,b);
printf("sum is %d",adder);
to:
int result = adder(a,b);
printf("sum is %d\n", result);
or to:
printf("sum is %d\n", adder(a, b));
My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}