Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hello I am beginner in C and I am reading 1.7 functions in K&R book. Below code is taken as is from the book.
int power(int m, int n);
int main()
{
int i;
for (i=0;i<10;++i)
printf("%d %d %d \n",i,power(2,i),power(-3,i));
return 0;
}
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
I can not understand how this code works, especially this part:
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
Here, where is returned p?
How this whole code raises number in power? And, relationship between these two parts of the code?
Any help is appreciated.
The code multiplies the base n times, which is essentially the definition of an integer exponent. What's important to recognize is that the loop executes n times, and each time it is multiplying p by the base.
Related
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 2 years ago.
Improve this question
#include<stdio.h>
double sum_1(int n)
{
int i=1;
double s;
while(n>0)
{
s=s+i/(2*i+2);
i=i+2;
n--;
}
return s;
}
int main()
{
int n=5;
double s1;
printf("Enter n:\n");
scanf("%d",&n);
s1= sum_1(n);
printf("sum = %lf",s1);
return 0;
}
The problem is
s=s+i/(2*i+2);
in the first iteration, s is used uninitialized. Since this is a type which can have trap representation, and it's address is never taken, trying to use the uninitialized value here invokes undefined behavior.
That said, the grouping of the statement
s=s+i/(2*i+2);
is same as
s = s + ( i / (2*i+2) );
^^^^^^^^^^^^^---- integer division
so, it involves integer division, which is most likely what you don't want. You need to enforce floating point arithmetic, like
s=s+i/(float)(2*i+2);
Finally, for printing a double, %f is sufficient, %lf is not needed and has no effect.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
#include<stdio.h>
double i;
int main()
{
(int)(float)(char) i;
printf("%d",sizeof(i));
return 0;
}
Its showing output as 8. Can anyone please explain me why it is showing as 8.?
Does the Typecasting have any effect on the Variable i..so that the output can possibly be 4??
(int)(float)(char) i; is not a definition of i. It merely is using the value for nothing.
#include <stdio.h>
double i;
int main(void) {
i; // use i for nothing
(int)i; // convert the value of i to integer, than use that value for nothing
(int)(float)i; // convert to float, then to int, then use for nothing
(int)(float)(char)i; // convert char, then to float, then to int, then use for nothing
printf("sizeof i is %d\n", (int)sizeof i);
char i; // define a new i (and hide the previous one) of type char
printf("sizeof i is %d\n", (int)sizeof i);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Question: Given an amount m and an array of numbers b[] print "Yes" if m can be formed by addition of any number of elements from the array without repeating them.
This is what I've come up with; how can I add a memoization matrix into it?
#include<stdio.h>
int check(int b[],int n,int m){
if(m==0){
return 1;
}
if(m<0){
return 0;
}
if(n<=0 && m>0){
return 0;
}
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d",&n);
scanf("%d",&m);
int b[n];
for(int i=0;i<n;i++){
scanf("%d",&b[i]);
}
if(check(b,n,m)>0){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
Can any one help me put a memorization matrix into it ?
The key to the memoization cache consists of the first n elements of b, plus m. Form a string from these, and use it as a key to a hash table.
...But what's the point? With n decreasing in each recursion, there are only two situations where memoization will help:
When a non-recursive call check is made for exactly the same list of numbers and m as an earlier call to check.
When m-b[n-1] == m, which is to say when b[n-1] == 0.
The first situation is unlikely (and decreasingly so as the size of the list increases, which is when memoization would help the most), and the second is easy to avoid. Just replace
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
with
return b[n-1] == 0
? check(b,n-1,m) * 2
: check(b,n-1,m) + check(b,n-1,m-b[n-1]);
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 5 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);
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
I had this written until i realized * is an arithmetic operation.
I want to return a nonnegative integer representation of the binary sequence input. IE. 0x4a returns 74 in decimal
typedef unsigned bit16;
unsigned int bin_to_dec(bit16 x)
{
int dec=0;
int k=0;
int remainder;
while (x!= 0){
remainder = x%10;
dec+=remainder*k;
k=k+2;
x=x/10;
}
:(
How would I go about this conversion if I can't use arithmetic operations other than +/-?
Since + is also an arithmetic operation, it becomes difficult. Depending on the exact rules, using a lookup table might be acceptable: return lookuptable[x];
As + and - are allowed...Instead of multiplying into k*reamainder try looping this way
int n;//consider new int
In the while loop write first line as
n=remainder;
Instead of *
for(i=0;i<k;i++)
remainder+=n;
This would do the multiplication :).
And for x%10, construct a function
int mod(int n)
{
int m;
while(n>0)
{
n=n-10;
m=n;
}
return m;
}
And for x/10 it'd be the same but you must return number of times you subtracted like this :
int mod(int n)
{
int count=0;
while(n>0)
{
count=count+1;
n=n-10;
}
return count;
}
edit : If + and - are also not allowed try making functions for them using binary operators and use them instead of + and - in the above answer!