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
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 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.
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
}
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
I am asked to write the C code that finds the number pi using the Leibniz formula.
However, the result should be 3.14 but result turns 3.23. What is the reason for this?
//Calculating the value of PI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
//function main begins program execution
int main( void )
{
float pi = 0;
size_t k,n;
for ( n = 0 , k = 0; n <= 10 , k <= 10; n++ ,k++) {
pi += ( pow( -1, n ) * 4 )/ ( 2 * k + 1 );
}//end for
printf(" pi is %.2f\n",pi );
getch();
return 0;
}//end main
I played with it by increasing your iterations.
At "200" I got 3.15.
Basically "10" isn't even close to enough.
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;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
stdout gives:--------------------------------------------------
Here are the results of all 3 students.--------------------------------------------------
Although I wanted it to display:--------------------------------------------------Here are the results of all 3 students:
--> 8.75
--> 9.25
--> 9.00 --------------------------------------------------
Here is the code:
#include <stdio.h>
int main(void) {
/*The grades of 3 students*/
int grades[3][4] = {{ 7,10,9,9 }, { 10,10,8,9 }, { 9,8,9,10 } };
float result1 = (grades[0][0] + grades[0][1] + grades[0][2] + grades[0][3])/4;
float result2 = (grades[1][0] + grades[1][1] + grades[1][2] + grades[1][3])/4;
float result3 = (grades[2][0] + grades[2][1] + grades[2][2] + grades[2][3])/4;
printf("Here are the results of all 3 students:\n");
/*I stored the results into an array so i can use for loop to display them faster*/
float a[3]= {result1,result2,result3};
/*Here i wanted to try to display the results using for loop*/
int i;
for (i = 0; i > 3; i++){
printf(" --> %.2f\n", a[i]);
}
return 0;
}
What's the problem with the output ?
The problem is here:
for (i = 0; i > 3; i++)
Try for (i = 0; i < 3; i++) (while i is less than 3, instead of while i is greater than 3, which is never...)