#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--
Related
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;
}
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
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;
}
I can't seem to get my program running
Write a function integerPower(base, exponent) that
returns the value of
Baseexponent. For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that
exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use for to
control the calculation.
Do not use any math library functions.
i have this program
#include<stdio.h>
int power(int b, int e){
int x
for (x = 0; x <= e; x++)
b=b;
return b;
}
void main(){
int b = 0;
int e = 0;
scanf("input a base %d\n", &b);
scanf("input an exponent %d\n", &e);
power(b,e);
printf("%d" , b);
}
In the loop
for (x = 0; x <= e; x++)
b=b;
the line b=b; is useless. It just assign the value of b to itself. You need to multiply b by e times. For this you need to take another variable with initial value 1 and multiply it by b at each iteration of loop to get be.
Change your function to this
int power(int b, int e){
int x, y = 1;
for (x = 1; x <= e; x++)
y = y*b; // Multiply e times
return y;
}
#include<stdio.h>
int power(int b, int e){
int x;
int result = 1;
for (x = 0; x < e; x++)
result*=b;
return result;
}
int main(){
int b = 0;
int e = 0;
printf("Input a base ");
scanf("%d", &b);
printf("Input an exponent ");
scanf("%d", &e);
b = power(b,e);
printf("%d" , b);
return 0;
}
First problem is with the scanf function. you are using "input a base ". For this to output you have to use printf("input a base ").
Try using the below function to calculate the value of base raised to the power of exponent
int ipower(int b, int e)
{
int x, tmp = 1;
for (x = 0; x < e; x++)
tmp *= b;
return tmp;
}
Here the for loop is iterated for e times that is from 0 to e-1. "tmp" will hold the result.
Make sure you don't change the value of "b/e"(base/exponent) inside the loop, else will lead to wrong result.
doing b*=b in function body won't help either. And why initialize b and e to zero when you're supposed to input them from the user?
Try this:
#include<stdio.h>
int power(int b, int e) {
int temp = b;
int x;
for (x = 1; x < e; x++)
b *= temp;
return b;
}
void main() {
int b ;
int e;
scanf_s(" %d", &b);
scanf_s(" %d", &e);
int h= power(b, e);
printf("%d", h);
}
int power(int b, int e){
int x,t=1
for (x = 1; x <= e; x++)
t*=b;
return t;
}
if user give e= 1 then also its work.
My teacher wants the sum of all numbers from x to y... like x+(x+1)+(x+2)...until y. But I think I'm doing something wrong here!
Can someone advice me what is wrong here?
#include <stdio.h>
int sum_naturals(int n)
{
return (n-1) * n / 2;
}
int sum_from_to(int m)
{
return (m-1) * m / 2;
}
void test_sum_naturals(void)
{
int x;
scanf("%d", &x);
int z = sum_naturals(x);
printf("%d\n", z);
}
void test_sum_from_to(void)
{
int x;
int y;
scanf("%d", &x);
scanf("%d", &y);
int z = sum_naturals(x);
int b = sum_from_to(y);
printf("%d\n", z);
}
int main(void)
{
//test_sum_naturals();
test_sum_from_to();
return 0;
}
Your code should in fact be:
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_to(int m)
{
return (m+1) * m / 2;
}
Notice + instead of your -.
To find the sum just add in the function test_sum_from_to this line:
printf("The sum is %d", b-z);
Here's one solution :
#include<stdio.h>
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_x_to_y(int x, int y){
return sum_naturals(y) - sum_naturals(x);
}
main()
{
printf ("Sum: %d \n",sum_from_x_to_y(5, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 5));
return 0;
}
Note : sum from 0 to N is (n+1)*n/2 and not (n-1)*n/2