This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 4 months ago.
here's the code it is supposed to display the value of my function (1/1+(25xx)) in a [-1,1] interval. But when I run it I have 1 as a result!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int i,n;
double y=0;
double t=0;
double delta=0;
scanf("%d",&n);
delta=1/n;
for (i=0;i<n;i++)
{
t=t+delta;
y= 1 / (1 + (25*t*t));
printf("%lf \n",y);
}
return 0;}
Your program is working with integer math. Be sure to use double literals and to cast integers to doubles where needed.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n;
double y = 0, t = 0, delta;
scanf("%d",&n);
delta = 1 / (double)n;
for (i = 0; i < n; i++) {
t = t + delta;
y = 1.0 / (1.0 + (25.0 * t * t));
printf("%lf \n", y);
}
return 0;
}
Related
Hello I need to create this summation and if you put the number 30000 the response should the number of pi, however it's not working here's the summation and here's the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <ctype.h>
#include <stdbool.h>
int main( void ){
int num, k;
double pi= 0;
printf("Digite o total de termos >=30000: ");
scanf("%d", &num);
if (num < 30000){
printf("Erro.");
}else {
for (k = 1; k<= num; k++){
if (k % 2 == 0){
pi = (double)(-1)/ (2*k -1);
}else{
pi = (double)(1)/ (2*k -1);
}
pi = pi * 4;
}
printf("O valor de pi e %f", pi);
}
return 0; }
Here is a general way to sum things up:
double sum(int from, int to, double (*f)(int)) {
double ret = 0.0;
for(int i=from; i<to; i++)
ret+=f(i);
return ret;
}
And then you can write this function:
double fun(int current) {
double sign = current %2 == 0 ? -1.0 : 1.0;
return sign / (2*current - 1)
}
Finally, call it like this:
double pi = sum(1, num, fun);
Do note that this is probably not a good way to go if you're a beginner student that is looking for a solution to some homework.
#include <stdio.h>
int main(void) {
double pi=0;
for(int k=1; k<=3000; ++k)
{
pi += (2.*(k%2)-1) / (2*k-1); // This line does a summation, and uses floating point math (Not Integer Math)
}
printf("Pi : %f\n", 4*pi);
return 0;
}
This question already has answers here:
Factorial using Addition
(4 answers)
Closed 3 years ago.
I'm trying to figure out a C program that calculates a factorial using nested while loops with no multiplication. Is there an easy way to do this with as little variables as possible?
I have an inner loop that does multiplication using addition, but I can't seem to find an outer loop that would then find the factorial.
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned int a = 4;
unsigned int b = 4;
unsigned int c = 0;
int i = 0;
while(i < b) {
c += a;
i++;
}
printf("Result: %u", c);
return 0;
}
This is one solution for that :
#include <stdio.h>
int mult (int n1, int n2);
int main () {
int number,result,count;
scanf("%d",&number);
result=number;
for (count=number-1;count>1;count--)
result=mult(result,count);
printf ("factorial is %d",result);
return 0;
}
int mult (int n1, int n2) {
int i,answer=0;
for (i=1;i<=n2;i++)
answer+=n1;
return answer;
}
I used my own variable names but that shouldn't be a problem.
This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an x\n");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an x\n");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
Even upon using a type like double you haven't got enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the correct (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, typically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analytically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %d\n", i);
break;
}
}
printf(" result = %.*e\n", DBL_DECIMAL_DIG, sum);
printf("expected = %.*e\n", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %d\n", i);
break;
}
}
return result;
}
Total noob here. Can someone give me an example on how i can generate a 2kHz sine wave array with white noise of variance 0.01 in C? This is what I have so far:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141592653589793
int main() {
int i;
double buffer[10000];
for(i = 0; i < 10000; i++)
{
buffer[i] = sin(2000 * (2 * PI) * i / 6000) + sqrt(0.01)rand;
}
return 0;
}
(For reference)
You first have to seed the random-number generator using srand(), to which you should pass a unique value at every program-run.
Your code, modified for correctness:
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned)time(NULL));
int i;
double buffer[10000];
for(i = 0; i < 10000; i++)
{
buffer[i] = sin(2000 * (2 * M_PI) * i / 6000) + sqrt(0.01) * rand();
}
/* ... */
return 0;
}
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 9 years ago.
I have a function generating random numbers. Why does it always generate the same ones? I tried running the algorithm several times but always get the same results.
#ifndef UTIL_H
#define UTIL_H
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#define MIN 0
#define MAX 100000
void randomArray (double *array, int length)
{
int i ;
for (i = 0; i < length; i++)
{
array[i] = (double) (rand () /
(((double) RAND_MAX + 1) / (double) (MAX - MIN + 1))) + MIN;
}
}
int main(void)
{
int i;
double test_array[9];
randomArray(test_array, 9);
for(i = 0; i < 9; i++)
printf("%f ", test_array[i]);
printf("\n");
return 0;
}
You need to seed the rand function. Use srand(time(NULL)) in the beginning of your main.
There are 3 problems in your code:
1) Add srand to your main() function:
int main(void) {
int i;
double test_array[9];
srand (time(NULL)); // here it is
randomArray(test_array, 9);
for(i = 0; i < 9; i++)
printf("%f ", test_array[i]);
printf("\n");
return 0;
}
2) Add an stdio.h library for using printf():
#include <stdio.h>
3) There is unterminated #ifndef that will couse an error when compiling.
Add #endif:
#ifndef UTIL_H
#define UTIL_H
#endif // here it is