Display the power of a requested (integer) number in C language - c

The exercise I am trying to do is to display the power of an integer number requested from the user.
I don't understand why it displays 1 instead of the number requested by the user (the power).
Here is the source code:
#include <stdio.h>
int main()
{
double x, carre, cube,resp;
int p, tmp;
printf("Enter a number : ");
scanf("%lg", &x);
printf("Enter a power : ");
scanf("%d",&p);
carre = x * x;
cube = x * carre;
resp = 0;
tmp = x;
while (p > 1){
resp = tmp*x;
p = p-1;
tmp = resp;
}
printf("%g ^ %d = %lg\n", x, 0, 1.0);
printf("%g ^ %d = %lg\n", x, 1, x);
printf("%g ^ %d = %lg\n", x, 2, carre);
printf("%g ^ %d = %lg\n", x, 3, cube);
printf("%g ^ %d = %lg\n", x, p, resp); # it display 1 instead of the requested number (p)
return 0;
}
For my example I choose 2 as number and 5 for the power
my example compiled with error
I tried the source code and I asked the user to enter a number and a power, but the display shows 1 instead of the value of p which is 5 in my example (see the picture)

In this loop:
while (p > 1){
resp = tmp*x;
p = p-1;
tmp = resp;
}
You are decrementing p on each time around the loop (iteration). The loop only ends when p == 1
So when you print p after the loop it is always going to be 1.

It happens because you change your p in this line p = p-1;
Try to add a variable that holds your old value p like:
// Online C++ compiler to run C++ program online
#include <cstdio>
int main()
{
double x, carre, cube,resp;
int p, tmp, oldP;
printf("Enter a number : ");
scanf("%lg", &x);
printf("Enter a power : ");
scanf("%d",&p);
oldP = p;
carre = x * x;
cube = x * carre;
resp = 0;
tmp = x;
while (p > 1){
resp = tmp*x;
p = p-1;
tmp = resp;
}
printf("%g ^ %d = %lg\n", x, 0, 1.0);
printf("%g ^ %d = %lg\n", x, 1, x);
printf("%g ^ %d = %lg\n", x, 2, carre);
printf("%g ^ %d = %lg\n", x, 3, cube);
printf("%g ^ %d = %lg\n", x, oldP, resp);
return 0;
}

Related

Why don't float operations work properly?

I'm new to C, but i have previously coded in C++ and C#. I have written this code as an assignment, but the float operations don't work properly. What it's supposed to do is, by entering two positive integers, n and m, the end result should be this a sum of a sum with n and the square root of a multiplication.
My problem is that, even though the first sum works, both the multiplication and the square root (and in the end the final sum) don't work. In the end, whatever two numbers n and m i write, the sum will be ok and the other two will be completely innaccurate - either 1, both the multiplication and the final sum, or something that makes no sense (to be precise, "1.#INF00").
This is the code i have written. Does anybody know what i did wrong, or how can I fix this?
float sum(int n)
{
float s = 0;
for(float i = 1; i<=(float)n; i++)
{
s += (2*i)/(3*i*i+4);
}
return s;
}
float multiplication(int m)
{
float p = 1;
for(float j = 1; j <= (float)m; j++)
{
p *= (float)(j*j+1);
}
return p;
}
int main()
{
int n;
int m;
scanf("%i", &n);
scanf("%i", &m);
float s = sum(&n);
float p = multiplication(&m);
float e = s + (float)sqrt(p);
printf("The sum is %f \n", s);
printf("The multiplication is %f \n", p);
printf("The final expression is %f \n", e);
getch();
return 0;
}
You should pass the integer values, not pointers, to the functions sum and multiplication.
float sum(int n)
{
float s = 0;
for(float i = 1; i<=(float)n; i++)
{
s += (2*i)/(3*i*i+4);
}
return s;
}
float multiplication(int m)
{
float p = 1;
for(float j = 1; j <= (float)m; j++)
{
p *= (float)(j*j+1);
}
return p;
}
int main()
{
int n;
int m;
scanf("%i", &n);
scanf("%i", &m);
float s = sum(n); /* pass an integer, not a pointer */
float p = multiplication(m); /* pass an integer, not a pointer */
float e = s + (float)sqrt(p);
printf("The sum is %f \n", s);
printf("The multiplication is %f \n", p);
printf("The final expression is %f \n", e);
getch();
return 0;
}

Getting "too few arguments to function"

#include<stdio.h>
int powFunc(int, int);
int main() //main function
{
int x, y, p = 0;
printf("enter the base no. and the power ");
scanf("%d %d", &x, &y);
p = powFunc(int x, int y); // Getting an error here
printf("%d to the power of %d is %d\n",x,y,p);
return 0;
}
int powFunc(int base, int n)
{
int p;
for(p = 1; n < 0; n--)
p = p * base;
return p;
}
Don't put data types when you want to use variables as function parameters. Change:
p = powFunc(int x, int y);
into:
p = powFunc(x, y);
And also, you made a semantic error here:
for (p = 1; n < 0; n--) {
p = p * base;
}
You want to check if n > 0 not n < 0. Correct the logic:
p = 1; n > 0; n--

How could you convert the following recursion with dynamic programming?

How would you convert the following recursive program with dynamic programming (DP)?
I'm just having a little trouble trying to redefine this code into a dynamic programming form. I got the base case and the general case identified, and I am aware that DP is about a "bottom-up" approach.
int add(int, int);
int main()
{
int x = 0, y = 0;
printf("Enter positive integers x, y: ");
scanf("%d %d", &x, &y);
printf("Result: %d\n", add(x, y));
return 0;
}
int add(int x, int y)
{
if(x < 0 || y < 0){
fprintf(stderr, "Negative Integer received!\n");
return -1;
}
if (x == 1 || y == 1)
return 1;
else
return add(x, y-1) + add(x - 1, y) + add(x-1, y-1);
}
Why do you want to do it in recursive way? There is an iterative way, and iterative 'almost always' beats recursive. Besides it is less code:
int DP[500][500];
memset(DP, 0, sizeof(DP));
for(int i=1; i<=x; i++) DP[i][1] = 1;
for(int i=1; i<=y; i++) DP[1][i] = 1;
for(int i=2; i<=x; i++) {
for(int j=2; j<=y; j++) {
DP[i][j] = DP[i-1][j-1] + DP[i-1][j] + DP[i][j-1];
}
}
printf("Result: %d\n", DP[x][y]);
But if you insist on recursion you can pass your DP array to function by pointer. And every time check if you calculated DP[i][j] before, if so don't calculate it again and return back:
#include <stdio.h>
#include <string.h>
void add(int x, int y, int (*M)[500])
{
if(M[x][y] > 0) return;
if (x == 1 || y == 1) {
M[x][y] = 1;
return;
}
add(x, y-1, M);
add(x - 1, y, M);
add(x-1, y-1, M);
M[x][y] = M[x][y-1] + M[x-1][y] + M[x-1][y-1];
return;
}
int main()
{
int x, y;
printf("Enter x, y: ");
scanf("%d %d", &x, &y);
int DP[500][500];
memset(DP, 0, sizeof(DP));
add(x, y, DP);
printf("Result: %d\n", DP[x][y]);
return 0;
}
Your code will cause stack overflow for all the possible x,y and z integer(negative, positive) combinations

Taylors Theorem C

I've got simple code for Taylor's Theorem for cosh() function.
I'm trying to catch a mistake - the result is sometimes close the real answer.
How to do it correctly?
When my start is 0, end is 5, and subdivides is 5 it gave good results, but when I put 5 as start and 10 as end, the result is farther away from the expected value.
#include <stdio.h>
#include <math.h>
int poww( float number, int a )
{
float result = 1.0;
int i;
if( a != 0 );
{
for( i = 0; i < a; i++ ) {
result = result * number;
}
}
return result;
}
int factorial(int n)
{
switch (n) {
case 0:
return 1;
break;
default:
return n * factorial(n-1);
}
}
void main()
{
puts("Enter start: ");
float start;
scanf("%f", &start);
puts("\nEnter end: ");
float end;
scanf("%f", &end);
puts("\nSubintervals:");
int subinterval;
scanf("%d", &subinterval);
float h = (end - start) / (float)subinterval;
printf("h is : %3.2f \n", h);
double x, result, temp;
int n;
for( x = start; x <= end; x += h) {
result = 0;
for(n = 0 ; ; n++) {
temp = poww(x, 2 * n) / (factorial( 2 * n ) * 1.0);
if(temp < 0.00001) {
break;
} else {
result = result + temp;
printf("X = %f temp = %f, result = %f\n", x, temp, result);
}
}
printf("X = %f, result = %3.2f, cosH = %3.2f\n\n", x, result, cosh(x) );
}
puts("Press any key...");
getchar();
}
PROBLEM SOLVE:
function returns an integer instead of double, also I changed every float to double.
Change all float types to double and use double as the return type for the factorial() and poww() functions, too. It's the last two that are most important in this case.
Also, the return type on main() should be int, not void.
[I just finished removing the dead if statement in poww(), and noticed that the function only "speeds up" a pow() computation. If you're worried about performance, worry about computing a factorial and a power on every term, rather than multiplying the previous term by x^2 and dividing by (2*n)*(2*n-1).]
I get good results between 4 and 10 on this minor fix of your code:
#include <stdio.h>
#include <math.h>
double poww( float number, int a )
{
float result = 1.0;
int i;
for( i = 0; i < a; i++ )
{
result = result * number;
}
return result;
}
double factorial(int n)
{
switch (n)
{
case 0: return 1;
break;
default: return n * factorial(n-1);
}
}
int main(){
puts("Enter start: ");
float start;
scanf("%f", &start);
puts("\nEnter end: ");
float end;
scanf("%f", &end);
puts("\nSubintervals:");
int subinterval;
scanf("%d", &subinterval);
float h = (end - start) / (float)subinterval;
printf("h is : %3.2f \n", h);
double x, result, temp;
int n;
for( x = start; x <= end; x += h){
result = 0;
for(n = 0 ; ; n++){
temp = poww(x, 2 * n) / (factorial( 2 * n ) * 1.0);
if(temp < 0.00001){
break; }
else{
result = result + temp;
printf("X = %f temp = %f, result = %f\n", x, temp, result);
}
}
printf("X = %f, result = %3.2f, cosH = %3.2f\n\n", x, result, cosh(x) );
}
puts("Press any key...");
getchar();
return 0;
}

find the exact amount which can not pay off a loan (avoid infinite loop)

Here's my code for a simple C program that will calculate the interest paid on
a loan. I don't have any question on the codes. But I can not figure out a way to find the exact payment amount which can not pay off the loan(that amount will lead to an infinite loop). I only know that the amount should be somewhere around 41.7. Is there a smart way to do this? Thank you
#include <stdio.h>
#include <stdlib.h>
int main()
{
float p;
float i;
float temp, ti = 0;
int a = 1;
printf("Please enter your monthly payment: ");
scanf("%f", &p);
printf("\n");
float r = 0.25;
float b = 2000.0;
printf("r = %.2f\nB = %.1f\nP = %.1f \n\n", r, b, p);
i = (r/12) * b;
temp = i;
printf("%d %.2f %.2f\n", a, i, b);
a ++;
while(i > 0)
{
i = (r/12) * (b - p + temp);
b = (b - p + temp);
ti += temp;
temp = i;
printf("%d %.2f %.2f\n", a, i, b);
a ++;
}
printf("\n");
printf("total interest paid: %.2f\n", ti);
return 0;
}
The program will go into an infinite loop when the monthly payment is less than the monthly interest value.
In the given code,
i = (r/12) * (b - p + temp);
determines the infinite loop condition.
For the loop to terminate
p > temp
p > (r / 12) * b
Substituting values give,
p > (0.25 / 12) * 2000
p > 41.66

Resources