#include <stdio.h>
int multiple(int, int);
int main()
{
int i;
int x;
int y;
scanf("%d %d", &x, &y);
printf("%d", multiple(x,y));
}
int multiple(int N,int M)
{
int i;
int result;
for (i=0;i*M<N;i++)
{
result=i*M;
printf("%d", result);
}
}
When I put input (for example x=100 and y=7) the output displays all the multiple until 105 and not until 98 as it should be.
The loop does print up to 98. However, multiple is declared to return an int but doesn't actually have a return statement, so the return value is unspecified (and in practice you'll get some arbitrary value from a previous calculation). Then you print this "garbage" return value and in your case it happens to be 105.
If you don't want multiple to return a value then don't declare it to return a value, and don't print the value it returns.
Related
I am wroting two program that calculates a factorial. The program #1 is to have the return value type to be void, and the program #2 is to have the return value type to be int.
But I lack knowledge, so I don't know which one is right.
Which one is correct?
program #1 - return value type: void
#include <stdio.h>
void calculateFactorial(int count, int *sum);
int main()
{
int number;
int sum = 1;
printf("factorial number: ");
scanf(" %d", &number);
calculateFactorial(number, &sum);
printf("result: %d\n", sum);
return 0;
}
void calculateFactorial(int count, int *sum)
{
if (count == 0)
{
return;
}
*sum *= count;
calculateFactorial(--count, sum);
}
program #2 - return value type: int
#include <stdio.h>
int calculateFactorial(int count, int *sum);
int main()
{
int number;
int sum = 1;
printf("factorial number: ");
scanf(" %d", &number);
calculateFactorial(number, &sum);
printf("result: %d\n", sum);
return 0;
}
int calculateFactorial(int count, int *sum)
{
if (count == 0)
{
return *sum;
}
*sum *= count;
calculateFactorial(--count, sum);
}
The first one is correct, and the second one is wrong. In the second one, you don't return anything even though the function type is int. You are supposed to return an int. E.g.,
int func() {//Whatever code}
is wrong. It needs to return an int. The correct version is:
int func() {
return 0; //Or whatever int
}
If you don't want to return anything, then you need to have type void.
void func() {
return 0;
}
This is wrong. You can't return anything, and you need to do:
void func {
return; //Not having a return won't do anything though, you can leave it blank
}
void func {} //Works, no need to return
You may be confused because:
1. void main() {} //Works and
2. int main() {} //Works as well, and
3. int main() {return 0;} //Works as well
So, no 1. is absolute rubbish. It works somehow, but it's wrong and it's rubbish. Check How does int main() and void main() work?.
In no. 2 the compiler adds return 0 for you. It only happens in main function.
No. 3 is perfectly correct.
There is no right or wrong when it comes to how you handle returning values from a function: they can be returned as a direct value, or as a secondary value through a passed pointer. There is, however, one (or more) right way(s) to DO each, and many wrong ways. In your two examples, the first one is done a right way, and the second one is done a wrong way. In the second example, there is no return for the case where count != 0, so it is a wrong way.
I general, I prefer a simple mathematical function (like factorial) to return a direct value (in this case, as an int); to do so I would write the calculateFactorial function like this:
int calculateFactorial(int num)
{
if (num <= 1) return 1;
return num * calculateFactorial(num - 1);
}
sum is not needed in this scenario, just print the returned value (which is technically a product, not a sum anyway).
The question is as follows.
Q) Develop a function f1 to accept an integer argument, reverse the digits and return the reversed value. Also, develop another function f2 to accept two integer arguments x and n and to return the value of x raised to the power n. The return value of f2 should be passed on to f1 and the return value of f1 must be checked whether it is prime or not by another function f3. The result must be printed in the program.
The control does not flow beyond the 45th line in this code. I do not really know what it the issue here because when i run the code in code blocks, the output screen displays the output of f2 and then it remains idle. for example, if i give the input as 8 and 2, then the output returns a value of 81 and then it does not do anything beyond that.
#include <stdio.h>
#include <math.h>
int f1(int npow)
{
int ret=0;
while (npow>0)
{
ret=(ret*10)+(npow%10);
}
return ret;
}
int f2(int x,int n)
{
int res;
res=pow(x,n);
return res;
}
int f3(int resrev)
{
int i,check;
for (i=2;i<resrev;i++)
{
if (resrev%i==0)
{
check =1;
break;
}
else
{
check=0;
break;
}
}
return check;
}
void main()
{
int resrev,x,n,npow,prime;
printf("Enter two numbers x and n\n");
scanf("%d %d",&x,&n);
npow=f2(x,n);
printf("x to the power n is %d\n",npow);
resrev=f1(npow);
printf("Reversed value of x to the power n is %d \n",resrev);
prime=f3(resrev);
if (prime==1)
printf("It is not a prime number \n");
else
printf("It is a prime number\n");
}
Problem is in function f1. It is an infinite loop.
Loop condition is until npow>0 but you are not updating value of npow in the loop.
Following is corrected code:
int f1(int npow)
{
int ret=0;
while (npow>0)
{
ret=(ret*10)+(npow%10);
npow = npow/10;
}
return ret;
}
Loop inside f1 is infinite as condition to exit while loop is unaltered.
snippet has to be updated as follows,
int f1(int npow)
{
int ret=0;
while (npow>0)
{
ret=(ret*10)+(npow%10);
npow /= 10; // divide to remove lsb digit
}
return ret;
}
I Wrote this code which can identify whether a number is a Armstrong number or not
#include <stdio.h>
#include <stdlib.h>
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount()
{
int amount=0;
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
return amount;
}
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
When run with n=153 i always get 0.After several debugging,I found out the problem is somewhere in the Armstrong function(most likely)
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
The debug watches indicate that instead of execute the for loop,it went straight to the return z line,I have tried everything but still can't figure it out.Can you tell me what the problem is?
You are getting the wrong result because of some logical error. When you are choosing a variable to be global, you need to consider that the variable value can be modified by any function and in this case, you have already modified its value in num_amount function. You have also made some logical error in Num_amount and Armstrong function.
You haven't included math.h header file for pow.
Here is your modified code,
#include <stdio.h>
#include <stdlib.h>
#include<math.h> //<-------------Should have included
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount() //<------------modified
{
int z = n; //<--------take a copy of global n
int amount=0;
while(z>0)
{
amount++;
z=z/10;
}
return amount;
}
int Armstrong() //<------------modified
{
n=input();
int v;
int z=0;
int x=Num_amount();
int i;
while(n>0)
{
v=n%10;
z+=pow(v,x);
n/=10; //<-------modification of global n
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
Found a lot problems with the code. Here is a modified version.
1. Do not use a global variable.
2. Make calculation for power easier.
3. Return the status of result, not the result. You want to check whether number is Armstrong or not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int no_digits(int n){
int digits = 0;
while(n){
digits++;
n = n/10;
}
return digits;
}
int armstrong(){
int n;
printf("insert n:");
scanf("%d",&n);
int digits = no_digits(n);
int curnum = 0,original = n;
while(n){
curnum += pow(n%10,digits);
n /= 10;
}
if(original == curnum)
return 1;
return 0;
}
int main(){
if(armstrong())
printf("Is Armstrong\n");
else printf("Not Armstrong\n");
}
Let's take a look at your loop:
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
What's the value of n at this point? You've set it in the previous call to Num_amount like so:
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
So, after Num_amount has finished executing, n must be less than 10, meaning the loop in Armstrong won't execute.
This is a big reason why you shouldn't use globals, even in a toy program like this. If you use it for different purposes in different places, you just create headaches like this.
At the very least, you should change your code such that n is passed as a parameter to Num_amount and Armstrong.
Your function Num_amount() return "n" value is already less than 10 and for loop never run.
I need to write a function to compute a^b but I am not allowed to use pow. Any ideas? I am lost.
It looks like problem is in main now...
Somewhere it gets that vys is what i characterise it. So if i set that vys=1 in main i get 1 in output..
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
void multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *=b;
i++;
}
return vys;
}
main(void)
{
int b=0, n=0, vys=1;
printf("Give numbers b and n but they must be in interval <0,10>!\n");
scanf("%d %d", &b, &n);
if ((b < 0 || b>10) || (n<0 || n>10))
{
printf("Numbers are not in interval <0,10>!\n");
}
else
{
printf("Number is in interval so i continue...\n");
sleep(2);
vys= multiplied(&b, &n);
printf("%d", vys);
}
Let's be explicit.
First, this
void multiplied(int *b, int *n)
returns an int, so say so.
int multiplied(int *b, int *n)
Next, you initialised variables in main: do the same here.
int i, vys;
Like this:
int i=1, vys=1;
Now let's look at the loop:
while (i<=n)
{
vys=*b**b;
i++;
}
As it stands, you are setting vys to something over and over again in the loop.
You want to multiply up, e.g. 2, then 2*2, then 2*2*2, .... if you want a power of two:
while (i<=n)
{
vys *= *b;
i++;
}
Now, you don't need to pass pointers.
int multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *= b;
i++;
}
return vys;
}
Edit:
Watch out for when you call the function:
main(void)
{
int b=0, n=0, vys;
//input and checking code as you have it
multiplied(&b, &n); //<---- return ignored
printf("%d", vys); //<-- print uninitialsed local variable
}
Change you last two lines:
vys = multiplied(&b, &n); //<---- return captured
printf("%d", vys); //<-- print returned variable
Edit 2:
With the change to use int in the function and not pointers, pass the ints not their addresses:
vys = multiplied(b, n); //<---- pass the ints not their addresses
printf("%d", vys); //<-- print returned variable, which should vary now
Here you have a simple code:
#include <stdio.h>
long long intpow(int a, int b)
{
long long tempres = 1;
while(b--)
tempres *= a;
return tempres;
}
int main(void) {
printf("%lld\n", intpow(5,10));
return 0;
}
you need much larger int to accommodate the result.
You can play with it yourself: https://ideone.com/4JT6NQ
I am learning recursion and i encountered a conceptual doubt while solving the problem of calculation of remainder when a positive integer is a is divided by a positive integer b.
My code is:
#include<stdio.h>
#include<stdlib.h>
int x;
int rem(int a,int b)
{
x=a;
if(x>=b)
{
x=x-b;
rem(x,b);
}
printf("%d\n",x);
return x;
}
int main()
{
int a,b;
printf("Enter a & b\n");
scanf("%d %d",&a,&b);
int y =rem(a,b);
printf("rem is :%d",y);
return 0;
}
Its working fine. I have learned that for every call a new set of formal parameters and local variables are created.
So i experimented it by printing x on return of every recursive call!
But it is printing 1 1 1 1. Why is the value of x corresponding to a particular call not printed. ?
Why only the last modified value printed?.. Is that because i declared 'x' as global?
In this case perhaps you need only to move your print up
int rem(int a,int b)
{
x=a;
printf("%d\n",x);
if(x>=b)
{
x=x-b;
rem(x,b);
}
return x;
}
But I think you should avoid the use of global variables in a recursive alrotithm. It could make the algorithm very difficult to reason about. Recursive functions are better to be 'pure functions'.
It is because while x >= b, rem() is repeatedly called before printf()s are called. Only after x < b will the printf()s are called as each call on rem() unwinds.
You might want to make x local to rem() to get the desired result.
Ignoring issues with checking the return value from scanf() and that the two entered values are both positive, etc, I think you can and should avoid x altogether. You could use:
#include <stdio.h>
static int rem(int a, int b)
{
if (a >= b)
a = rem(a-b, b);
printf("%d\n", a);
return a;
}
int main(void)
{
int a, b;
printf("Enter a & b\n");
scanf("%d %d", &a, &b);
int y = rem(a, b);
printf("rem(%d, %d) is: %d\n", a, b, y);
return 0;
}
This code captures the return value from rem() at each level of recursion. In this case, because the returned value doesn't change as the recursion unwinds, you could use the global variable x, but there is no need for it, and you should avoid global variables whenever you can.
#include<stdio.h>
#include<conio.h>
int fun(int,int);
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
fun(a,b);
//printf("%d",fun(a,b));
}
int fun(int a,int b)
{
if(a<b)
printf("%d",a);
if(a>=b)
a=fun(a-b,b);
return a;
}