McAfee quarantines Visual Studio project - c

Edit: thanks, fixed the i and j and now it works when my antivirus is off, but McAfee still blocks the project when activated...
I'm trying to run this code and McAfee keeps interfering. after disabling it for a few minutes, the code was compiled (with no errors) but the output is just 'inf' what does that mean?
I'm not sure that the code is correct but I still excepted it run properly, trying to debug, it seems that it's not actually running...
probably some dumb mistake I haven't noticed.
will appreciate any help.
double factorial(double num)
{
double j, factor = 1;
for (j = 1; j < num; j++)
{
factor *= j;
}
return factor;
}
double sum_of_sequence(double x, int n)
{
double i, f = 0;
double sum = 0;
int power = 2 * n + 1;
for (i = 1; i < power; i += 2)
{
double factor = factorial(f);
double numerator = pow(x, i);
sum += numerator / (i * factor);
f++;
}
return sum;
}
void main()
{
double x = 1;
int n = 2;
double res = sum_of_sequence(x, n);
printf("%lf", res);
}

Related

Miscalculation of Lagrange interpolation formula for higher degree

I am approximating Runge’s function using Lagrange’s interpolation formula for 50 interpolation points. I have written the following program to do this, but I am getting the wrong value for x= -0.992008. That wrong value is 4817543.091313, but it should be 5197172.55933613. I have got this value from the following link: Link The code used are as follows:
#include <stdio.h>
#include <math.h>
double
runge(double x)
{
return (1 / (1 + (25 * x * x)));
}
double
ab(double x)
{
if (x < 0)
return -1 * x;
return x;
}
double
lag_func(double x, double *y_i, double *x_i, int n)
{
double ex = 0.0;
for (int i = 0; i <= n; i++) {
double numer = 1.0,
denom = 1.0,
prod = 1.0;
for (int j = 0; j <= n; j++) {
if (i != j) {
numer = (x - x_i[j]);
denom = (x_i[i] - x_i[j]);
prod *= numer / denom;
}
}
ex += (prod) * y_i[i];
}
return ex;
}
int
main()
{
int n;
scanf("%d", &n);
double y_i[n + 1],
x_i[n + 1];
for (int i = 0; i < n + 1; i++) {
x_i[i] = ((2 * (double) i) / (double) n) - 1;
y_i[i] = runge(x_i[i]);
}
printf("%lf\n", lag_func(-0.992008, y_i, x_i, n));
return 0;
}
The web site is rounding its Runge coefficients to six digits. Given the magnitudes of the terms involved, up to 3.9978•1011, this introduces multiple errors up to around 2•105.
This can be seen by inserting y_i[i] = round(y_i[i] * 1e6) / 1e6; after y_i[i] = runge(x_i[i]);. Then the output of the program is 5197172.558199, matching the web site’s inaccurate result.
The web site is wrong; the result of the code in the question is better.

Loop to get sum of factorial / exponent

Hello I wrote a loop to get the sum of the factorial of given value n divided by n with increasing exponent. To describe it better it looks like this:
But for some reason my loop is always returning the value 1 whenever I input a number.
Here's my loop:
int nVal, i, j, k, nProduct = 1, nSum = 0, nFactorial = 1;
float fResult;
for (i = 1; i <= nVal; i++)
{
for (j = 1; j <= nVal; j++)
{
nFactorial *= j;
nSum += nFactorial;
}
for (k = 1; k <= nVal; k++)
{
nProduct *= k;
}
fResult += (nSum * 1.0) / (nProduct * 1.0);
}
Any fixes I can try?
OP's code is incorrect in the numerator and denominator calculation. Also, the integer math readily overflows.
To better handle large n, form each term based on the prior term with floating point math.
double sum_fact_expo(int n) {
double sum = 0.0;
double ratio = 1.0;
for (int i = 1; i <= n; i++) {
ratio *= 1.0*i/n;
sum += ratio;
}
return sum;
}
How big will n get? This can dictate what the type for nProduct and nFactorial should be (e.g. int or long long or __int128 or double).
The nested/inner loops are wrong. You only want/need the for loop for i.
And you'll [possibly/probably] overflow/underflow if you wait to calculate the ratio. So, do this at each iteration.
Your factorial calculation line was okay. But, the nProduct calculation was incorrect. The multiplier has to be n [and not j].
You don't initialize fResult, so it starts with a random value (i.e. undefined behavior). This would be flagged if you compiled with -Wall [and -O2] to enable warnings.
Remember that:
k! = (k - 1)! * k
And, that:
n**k = n**(k - 1) * n
So, we can build up nFactorial subterms and nProduct subterms iteratively, in a single loop.
Here's what I think your code should be like:
// pick _one_ of these:
#if 0
typedef int acc_t;
#endif
#if 0
typedef long long acc_t;
#endif
#if 0
typedef __int128 acc_t;
#endif
#if 1
typedef double acc_t;
#endif
double
calc1(int nVal)
{
int i;
acc_t nProduct = 1;
acc_t nFactorial = 1;
double ratio;
double fResult = 0;
for (i = 1; i <= nVal; ++i) {
nFactorial *= i;
nProduct *= nVal;
ratio = (double) nFactorial / (double) nProduct;
fResult += ratio;
}
return fResult;
}
#include <stdio.h>
int main(void) {
int nVal, i, j, k, nProduct = 1, nFactorial = 1;
float fResult = 0;
scanf("%d", &nVal);
for (i = 1; i <= nVal; i++)
{
for (j = 1; j <= i; j++)
{
//calculate 1! 2! 3! ... n!
//actually calculate i!
nFactorial *= j;
}
for (k = 1; k <= i; k++)
{
//calculate n^1 n^2 n^3 ... n^n
//actually calculate n^i
nProduct *= nVal;
}
fResult += (nFactorial * 1.0) / (nProduct * 1.0);
nProduct = 1;
nFactorial = 1;
}
printf("%f\n", fResult);
return 0;
}

Showing powers to numbers powered by minus number

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.

mypow() trough continued fraction and taylorseries return bigger number than pow()

double taylor_log(double x, unsigned int n){
double tmp;
double sum = 0;
if(x < 1){
int j = 2;
x = 1 - x;
tmp = x;
sum = -x;
for(unsigned int i = 1; i < n; i++){
sum -= ((tmp *= x) / j);
j++;
}
return sum;
}
else if (x >= 1){
tmp = ((x-1)/x);
for(unsigned int i = 1; i <= n; i++){
sum += (tmp/i);
tmp *= ((x-1)/x);
}
return sum;
this is my fuction for log with taylor series wich work correctly.
Im using this formula to get exponetional fuction of number.formula for mypow()
and this is my code for pow
double taylor_pow(double x, double y, unsigned int n){
double sum = 1.0;
int fac = 1;
double exp = y;
double lna = taylor_log( x, n);
for(unsigned int i = 1; i <= n; i++){
fac *= i;
sum += (exp * lna / fac );
exp *= y;
lna *= taylor_log( x, n);
}
return sum;
}
Now my problem is that if i put for example 30 iterations for my function the number is higher than pow(). For example pow(2,3) = 8 and my result with 20 iterations is 8.0007... and its growing. Thans for all responses.
int fac is overflowing. Changed it to long double, and it works much better.
A 32 bit signed int will only hold values up to 12!, while an 80 or 128 bit long double can hold something like 2000!.

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