Showing powers to numbers powered by minus number - c

my program is working well but if I put negative number as power it just print me numbers from 1 to 20, whats the problem? I am just a begginer in this and I did not foudn any answer on the internet so I hope I will find some here, you helped me before too. Thank you and looking forward to you advices!
code:
#include <stdio.h>
#include <math.h>
double xToNPower (double mocnenec, double mocnitel);
int main(void)
{
double pole[20];
int i;
double mocnina;
for (i = 0; i < 20; i++)
{
pole[i] = i + 1;
}
printf("Zadaj mocninu: \n");
scanf("%lf", &mocnina);
for (i = 0; i < 20; i++)
{
pole[i] = xToNPower(pole[i], mocnina);
printf("%.3lf ", pole[i]);
}
return 0;
}
double xToNPower (double mocnenec, double mocnitel)
{
double x = mocnenec;
int j;
if (mocnitel == 0)
{
return 1;
}
else if (mocnitel == 1)
{
return mocnenec;
}
else
{
for(j=2; j<=mocnitel; j++)
{
x *= mocnenec;
}
return x;
}
return 0;
}

When you call xToNPower(pole[i], mocnina); with mocnina < 0, then in double xToNPower (double mocnenec, double mocnitel), the argument mocnitel will be negative as well.
Control drops into the else clause, because mocnitel does not equal 0 or 1.
This loop is the problem:
for(j=2; j<=mocnitel; j++)
{
x *= mocnenec;
}
What if mocnitel < 0? Well, the whole loop is simply going to be skipped (since j <= mocnitel will be false even before the first iteration), and the next statement, return x;, will be executed.
Thus, you got double x = mocnenec; as your result - the original number.

Related

Can anyone tell why this C function isn't working?

So, I'm doing an exercise, and I want to sort a list of float numbers. When I used for loop, it worked perfectly. When I changed to while loop, it shows nothing. I already tried to declare a different variable for each while loop, but remains the same. I'm getting this warning for the file ex005.c:
Please, input one non-integer values: 45.3
Please, input one non-integer values: 32.2
Please, input one non-integer values: 34.5
zsh: segmentation fault ./"ex005"
Here is my code:
#include <stdio.h>
int main(void)
{
float three_numbers[3];
char support_var;
int size_array;
int i;
int z;
size_array = 3;
z = size_array - 1;
i = 0;
while (i < size_array)
{
printf("Please, input one non-integer values: ");
scanf("%f", &three_numbers[i]);
i++;
}
i = 0;
while (i < size_array)
{
while (z != i)
{
if (three_numbers[i] < three_numbers[z])
{
support_var = three_numbers[i];
three_numbers[i] = three_numbers[z];
three_numbers[z] = support_var;
}
z--;
}
i++;
}
i = 0;
while (i < size_array)
{
printf("The %dth place is %.1f. \n", i + 1, three_numbers[i]);
i++;
}
return (0);
}
You probably want to set z in the inner loop otherwise it causes out of bound access of three_numbers[z]. Also, your support_var should be a float. Use the typeof operator if available (with gcc 10.2.1-6 it's still an extension so -std=gnu18):
#include <stdio.h>
#define ARRAY_SIZE 3
int main(void) {
float three_numbers[ARRAY_SIZE];
for(int i = 0; i < ARRAY_SIZE; i++) {
printf("Please, input one non-integer values: ");
scanf("%f", three_numbers + i);
}
for(int i = 0; i < ARRAY_SIZE; i++) {
for(int z = ARRAY_SIZE - 1; z != i; z--) {
if(three_numbers[i] < three_numbers[z]) {
// typeof (*three_numbers) support_var = three_numbers[i];
float support_var = three_numbers[i];
three_numbers[i] = three_numbers[z];
three_numbers[z] = support_var;
}
}
}
for(int i = 0; i < ARRAY_SIZE; i++) {
printf("The %dth place is %.1f. \n", i + 1, three_numbers[i]);
}
}
Not sure why you don't like for-loops but it makes your code easier to read. Also replaced int array_size with a constant ARRAY_SIZE. Minimizing scope of variables makes is usually a good idea.

i wrote a code for sum of series but i am not getting any output

The question was to add first seven terms of the following series using for loop
1/1! + 2/2! + 3/3! ....
I thought i might be loosing decimal point due to int but i am not even getting wrong answer.
I ran the code in terminal but it just keeps running no output.
Thanks for helping out.
#include<stdio.h>
int main()
{
int n;
float a;
float v=0.0;
for(n=1;n<=7;n++)
{
a=1.0;
while(n>0)
{
a=a/n;
n--;
}
v=v+n*a;
}
printf(" sum is %f ",v);
return 0;
}
#include <stdio.h>
int fact(int n) {
int product = 1;
for (int i = 1; i < n+1; i++) {
product *= i;
}
return product;
}
int main() {
double sum = 0.0;
for(int i = 1; i < 7+1; i++) {
sum += double(i) / fact(i);
}
printf("Sum is %f\n", sum);
return 0;
}
The series is 1/1! + 2/2! + .. n terms can be written as 1 + 1/1! + 1/2! + 1/3! + ...(n-1) terms .
So it is 1 + sum of 1/i! where i=1 to i=n-1 terms.
So I have taken sum=1 as initial value.
#include <stdio.h>
//this is the function for calculating n!
int fact(int n) {
int product = 1;
for (int i = 1; i < n+1; i++) {
product *= i;
}
return product;
}
int main() {
int n=5;
//we are taking sum=1 as initial value
double sum = 1.0;
//now we run 1/1! + 1/2! + 1/3! + 1/4! i.e. upto (n-1) terms (here n=5 => so n-1 = 4)
for(int i = 1; i < n; i++)
{
sum += (1.0) / fact(i);
}
printf("Sum is %f\n", sum);
return 0;
}
As #DeBARtha mentioned in the original code i was incrementing and then decreasing n causing to loop to run repeatedly.
By using one more variable (m) for the while loop and then decreasing it while increasing the 'n' in the for loop the problem gets resolved.
#include<stdio.h>
int main()
{
int n,m;
float a;
float v=0.0;
for(n=1;n<=7;n++)
{
m=n;
a=1.0;
while(m>0)
{
a=a/m;
m--;
}
v=v+n*a;
}
printf(" sum is %f ",v);
return 0;
}

Adding common factors in C

I'm just trying to add all common factors of 3 and 5, stopping at a sum of 1000. I keep getting an expected expression on line 15:18. Is there anyone that can find any new errors or help? It would be much appreciated. Thanks.
#include<stdio.h>
#include<stdlib.h>
/*Multiples of 3 and 5
If we list all natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6
and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.*/
int cd_function(int n, int sum)
{
if(sum >= 1000)
return 0;
if(n%3 == 0 || n%5 == 0)
sum + return cd_function(n, sum);
}
int main(void)
{
int i, iter, sum = 0;
for(i = 0; i < cd_function(iter, sum); i++)
sum++;
return 0;
}
#include<stdio.h>
#include<stdlib.h>
Most people will add a space here:
#include <stdio.h>
#include <stdlib.h>
.
int cd_function(int n, int sum)
{
if(sum >= 1000)
return 0;
if(n%3 == 0 || n%5 == 0)
sum + return cd_function(n, sum);
}
There is two ways, to have this functions add n to sum: Either by passing sum as pointer (reference), this would be done this way:
void cd_function(int n, int * sum) {
// if (*sum >= 1000) - no need to test this here
if (!n%3 || !n%5) {
*sum += stuff;
}
}
or by having the function return the new sum:
int cd_function(int n, int sum) {
// if (sum >= 1000) - no need to test this here
if (!n%3 || !n%5) {
return sum + stuff;
} else {
return sum;
}
}
Now, sum + return cd_function(n, sum); is wrong, YSC already told so in his comment. So you should either use *sum += n; or return sum + n; (i.e. replace stuff by n above).
int main(void)
{
int i, iter, sum = 0;
Since you don't need iter, get rid of it:
int i, sum = 0;
for(i = 0; i < cd_function(iter, sum); i++)
sum++;
a. If you want to sum some[tm] is, then you shouldn't increment sum in each iteration.
for (i = 0; i < cd_function(iter, sum); i++)
;
b. Then you should get a proper exit condition. You wanted to sum everything until you reach a sum >= 1000, so write that in the condition:
for (i = 0; sum < 1000; i++)
/* ??? i < cd_function(iter, sum) */;
c. Depending on the implementation of cd_function you choose from above, you would now either call
cd_function(i, &sum);
or
sum = cd_function(i, sum);
d. And finally, you should add curly brackets, even there is only one statement:
for (i = 0; sum < 1000; i++) {
// option 1
cd_function(i, &sum);
// option 2
sum = cd_function(i, sum);
}
rest is fine:
return 0;
}

Why this reverse function can not work in the for loop?

#include <stdio.h>
#include <math.h>
int prime (long n);
long reverse(long n);
int main(void)
{
long n;
long i, j;
puts("Enter n dight number, and we will help you find symmetrical prime number");
scanf("%ld", &n);
for (i = 11; i < (pow(10, n) - 1); i+= 2)
{
if (prime(i))
{
j = reverse(i);
if (i == j)
{
printf("%ld\n", i);
}
}
}
}
int prime (long n) //estimate whether the number n is primer number
{
int status = 0;
int j;
//1 is prime, 0 is not
if (n % 2 == 0 || n == 3)
{
if (n == 2)
status = 1;
if (n == 3)
status = 1;
else
{
n++;
status = 0;
}
}
else
{
j = 3;
while (j <= sqrt(n))
{
if (n % j == 0)
{
status = 0;
break;
}
else
status = 1;
j+= 2;
}
}
return status;
}
long reverse(long n) //reverse a number
{
int i, j, x;
long k, sum;
int digit = 0;
int ar[1000];
while (n > 0)
{
k = n;
n = n / 10;
x = (k - n*10);
digit++;
ar[digit] = x;
}
for (i = 1,j = digit - 1; i <= digit; i++, j--)
{
sum += ar[i] * pow(10, j)
}
return sum;
}
I build a reverse function in order to reverse numbers, for example, 214, to 412.
This function works fine in individual number, for instance, I type reverse(214), it return 412, which is good. But when I combine reverse() function with for loop, this function can not work... it produces some strange number...
so How can I fix this problem?
The reverse function is extremely complicated. The better way to go about it would be:
long reverse (long n)
{
long result = 0;
while (n != 0)
{
result *= 10;
result += n % 10;
n /= 10;
}
return result;
}
I think the problem in your code is that in the following segment
digit++;
ar[digit] = x;
you first increment the position then assign to it, thus leaving ar[0] unintialized.
How can I fix this problem?
You need to initialize sum
long k, sum = 0;
^
See the code from #Armen Tsirunyan for a simpler approach.

Problem with loop in C

i'm trying to compute "2^0 + 2^1 + 2^2 + ... + 2^14", using the following program(i'm a newbie and can only compute a exponent by multiply itself a certain times). The result should be 32767, but i ran it and got 270566475, i thought for long but can't figure out why...
#include <stdio.h>
int main(void)
{
int i, e, exponent, sum;
e = 1;
exponent = 1;
sum = 1;
for (i = 1; i <=14; i++)
{
for (e = 1; e <= i; e++)
{
exponent *= 2;
}
sum += exponent;
}
printf("%d\n", sum);
return 0;
}
So what's wrong with this??? Thanks!!!
You don't need the inner loop. Just execute exponent *= 2 once, directly inside the outer loop. BTW, I think you have to do it after the sum += ....
Also, you could start with sum = 0 and i = 0, which is closer to the math you described.
Look at your inner loop by itself. It's trying to calculate, for one specific value of i, 2^i.
But exponent does not start at 1 every time. So you go into that loop with exponent already some very large value.
for (i = 1; i <=14; i++)
{
exponent = 1;
for (e = 1; e <= i; e++)
{
exponent *= 2;
}
sum += exponent;
}
Now you've reset exponent (which, to be clear, isn't the exponent at all but the calculated result) for each new power of 2.
If you have right to create a function it better to do it like this with a recursive function :
#include <stdio.h>
int power(int x, int exp) {
if (exp == 0)
return 1;
else
return x * power(x, exp-1);
}
int main (int argc, const char * argv[])
{
int i;
int sum = 0;
for (i = 0; i <= 14; i++) {
sum += power(2, i);
}
printf("%d",sum);
return 0;
}
I hope it helps.
You just need one loop because each you already have the result of n-1 value. I had correct your code it works.
#include <stdio.h>
int main (int argc, const char * argv[])
{
int i, e, exponent, sum;
e = 1;
exponent = 1;
sum = 1;
for (i = 1; i <= 14; i++)
{
exponent *= 2;
sum += exponent;
}
printf("%d\n", sum);
return 0;
}
Both codes work

Resources