Can't understand how this works [closed] - c

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 4 years ago.
Improve this question
I don't understand this, as far as I understand this should goes like this;
4*4^2*4
but it doesn't, I know it has a simple explanation but still I tried to figure out this like for 20 minutes, I hope someone helps. Sorry for the bad editing too.
int power(int n1, int n2);
int main() {
int base, powerRaised, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &powerRaised);
result = power(base, powerRaised);
printf("%d^%d = %d", base, powerRaised, result);
return 0;
}
int power(int base, int powerRaised) {
if (powerRaised != 0)
return (base * power(base, powerRaised - 1));
else
return 1;
}

The code behaves as expected, the power function is recursive. For arguments 4 and 2, it calls itself recursively twice:
power(4, 2)
-> 4 * power(4, 1)
-> 4 * (4 * power(4, 0))
-> 4 * (4 * 1)
-> 16
You might be more familiar with an iterative approach:
int power(int base, int powerRaised) {
int res = 1;
while (powerRaised > 0) {
res = res * base;
powerRaised--;
}
return res;
}

Related

It is a hackerrank problem, that deals with Dynamic arrays in C, So if anyone can explain me this program please [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 4 days ago.
Improve this question
#include <stdlib.h>
/*
* This stores the total number of books in each shelf.
*/
int* total_number_of_books;
/*
* This stores the total number of pages in each book of each shelf.
* The rows represent the shelves and the columns represent the books.
*/
int** total_number_of_pages;
int main()
{
int total_number_of_shelves;
scanf("%d", &total_number_of_shelves);
int total_number_of_queries;
scanf("%d", &total_number_of_queries);
total_number_of_books = (int*)calloc(total_number_of_shelves, sizeof(int));
total_number_of_pages = (int**)calloc(total_number_of_shelves, sizeof(int *));
for (int i = 0; i < total_number_of_shelves; i++)
{
total_number_of_pages[i] = (int*)calloc(1100,sizeof(int));
}
while (total_number_of_queries--)
{
int type_of_query;
scanf("%d", &type_of_query);
if (type_of_query == 1)
{
int shelf, pages;
scanf("%d %d", &shelf, &pages);
total_number_of_books[shelf] += 1;
int i = 0;
while (total_number_of_pages[shelf][i] != 0)
{
i++;
}
total_number_of_pages[shelf][i] = pages;
} else if (type_of_query == 2) {
int shelf, number__ofpages;
scanf("%d %d", &shelf, &number__ofpages);
printf("%d\n", *(*(total_number_of_pages + shelf) + number__ofpages));
} else {
int shelf;
scanf("%d", &shelf);
printf("%d\n", *(total_number_of_books + shelf));
}
}
}
I feel like the solution is difficult to understand, and I have never faced problems of dynamic memory allocation like that before, I understood all the declarations, but most of if and else statements made me confused and I am did not understand what the program is trying to achieve.

For loop subtraction issue in c [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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
#include <stdio.h>
int main() {
int a , b, i , sum = 0 ;
printf("\a");
printf("\n\t\t🔸 What is the number you want to begin"
" subtraction with ? ");
scanf("%d",&a);
printf("\n\t\t🔸 What is the number you want to end"
" subtraction with ? ");
scanf("%d",&b);
printf("\a");
printf("\n\t\t🔸 The result of the subtraction of numbers from %d to %d :\n"
, a , b);
printf("\t\t ");
for (i = a ; i >= b+1 ; i--){
printf("%d - ", i);
sum = sum - i ;
printf("%d = %d\n\n", b , sum);
return 0;
}
I want to create a program that subtracts a sequence of numbers from a to b using a for loop in C programming language. I tried for many times, and couldn't find a solution. I want a program that runs like this: for example, if I chose (a) as 8 and (b) as 5, I want the program to write 8-7-6-5 = -10. I want a program that runs like that depending on the value I choose as (a) and (b) using a for loop in c programming.
if I chose (a) as 8 and (b) as 5, I want the program to write 8-6-5 = -3
I believe that you forgot about number 7 and it should be 8-7-6-5 = -10
If yes:
int myfunc(int start, int end)
{
int result = start;
for(int index = start - 1; index >= end; index--)
{
result -= index;
}
return result;
}
int main(void)
{
printf("%d", myfunc(8,5));
}
https://godbolt.org/z/6133cjbK1
If you want to print the whole expression:
int myfunc(int start, int end)
{
int result = start;
printf("%d", start);
for(int index = start - 1; index >= end; index--)
{
printf("-%d", index);
result -= index;
}
return result;
}
int main(void)
{
printf("=%d", myfunc(8,5));
}

Can someone tell me where I went wrong [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 1 year ago.
Improve this question
Can someone help me out with correcting this. The question was supposed to be (Write a code to get the value for expression 1 + x + x^2 + x^3 + ...... + x^n)
#include<stdio.h>
int main()
{
int power = 1,sum = 0,n,x , i;
printf("Enter the number : ");
scanf("%d",&x);
printf("Enter the limit to fill the following series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum = sum + power;
while (i>0)
{
power = power * x;
i = i - 1;
}
}
printf("The sum is %d",sum);
return 0;
}
Change
while (i>0)
{
power = power * x;
i = i - 1;
}
to
power = power * x;
As this loop is not required

Sum of odd numbers using recursion [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 1 year ago.
Improve this question
This might be simple but I'm new to recursion in c. I want to find the sum of odd integers based on user's input. For example if user inputs 3, function returns 9 (1 + 3 + 5 = 9)
int recursiveSumNOdd(int n)
{
int start = -2; //later start = start+2, so it starts at 0
int n1 = n*2; //total number of digits with rec
int num = 0;
int sum=0;
int count=0;
if(start>=n1)
{
return 0;
}
else
{
start = start+2;
count++;
sum = sum +recursiveSumNOdd(start);
}
return sum;
}
Explanations in comment:
int recursiveSumNOdd(int n) {
if (n == 1 || n == 0)// first "if" in a recursive is its stop condition
return n;
return 2 * n - 1 + recursiveSumNOdd(n-1); // formula for 2->3, 3->5 etc
}
int main(void) {
printf("%d\n", recursiveSumNOdd(3));
return 0;
}
NB: You may want to handle integer overflow
NB2: You can have a mathematics formula to return instantly the result, it is way better, but I guess it was to understand better recursion?
return n * n; // the sum of odd numbers is the square of user's input
You are over-complicating things.
You cannot have the sum of negative elements.
int sum_n_odd(unsigned n)
{
What is the sum of 0 (zero) elements?
if (n == 0) return 0;
If you knew the sum of n - 1 numbers, what is the sum of n numbers?
return sum_n_odd(n - 1) + something; // something is easy to figure out
}

Random number generation in my implementation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Is this implementation of random number generation wrong?
int random_number(int l, int u)
{
int num;
num = rand()%u;
if (num<l && num>u)
{
while(num<l && num>u)
{
num = rand()%u;
}
}
return num;
}
This is not giving me the correct answer.
If I try random_number(4,8); it generates numbers like 0,1,2 etc.
Assuming u means upper and l means lower, then it is wrong. Try this:
int random_number(int l, int u) {
int num = rand() % (u - l);
return num + l;
}
Consider the lines.
int random_number(int l, int u)
num = rand()%u // result 0, 1, 3, .... 7
if (num<l && num>u)
random_number(4,8);
Code needs to follow the if() when num <4 and num > 8. An int cannot both be less than 4 and greater than 8 at the same time.
The usual idiom is
int random_number(int lower, int upper) {
int num;
num = rand()%(upper - lower + 1) + lower;
return num;
}
Extra code is needed to cope/detect upper < lower, upper - lower >= RAND_MAX

Resources