#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
float FloatRandomizer(float a, float b)
{
float randomNumber = (a + (float)rand() / (float)(RAND_MAX / b));
if (randomNumber > b)
randomNumber = randomNumber - a;
return randomNumber;
}
float F(float x)
{
return x * x;
}
int main()
{
int n = 1000000;
float a;
printf("a = "); scanf_s("%f", &a);
float b;
printf("b = "); scanf_s("%f", &b);
float temp = (b - a) / n;
float sum = 0;
int i;
#pragma omp parallel for shared(sum)
for (i = 0; i < n; i++)
{
float randomNumber = FloatRandomizer(a, b);
sum = sum + F(randomNumber);
//printf("threadNum = %d\nsum = %f\n", omp_get_thread_num(), sum);
}
float result = temp * sum;
printf("result = %f", result);
}
There are not to much things to say. I am trying to get definite integral within given [a,b] range with Monte-Carlo method. So basically everything works, problems started when I wrote #pragma open mp for statement, it calculates result(that for a = 0 and b = 1 should be 1/3) in wrong way without commented line within for loop but with printf it calculates it correctly. I can't understand the problem
Without printf:
With printf for same a and b(last line of big output):
I am using Microsoft Windows 10 and Microsoft Visual Studio.
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;
}
I finally got code that's giving me random floats from 0 to 1.
My code looks like this:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((int)time(NULL));
int N=10;
float a = 1.0;
for (int i=0;i<N;i++)
printf("%f\n", ((float)rand()/(float)(RAND_MAX)) * a);
return 0;
}
Now, I need to make program that will give me the average from given numbers for 10<=N<=10000, but im out of ideas how to make it works. Any ideas?
Used the 'a' variable as temporary storage and summation for the floats.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((int)time(NULL));
int lowerLimit = 10 + 1, upperLimit = 10000;
//generates random number between 10 and 10000, if needed
int N = lowerLimit + rand() % (upperLimit - lowerLimit);
/* 'a' variable was not needed in previous*/
float a, sum = 0.0;
for (int i=0;i<N;i++){
a = ((float)rand()/(float)(RAND_MAX));
sum += a;
printf("Float %i = %f\n", i,a);
}
printf("Average = %f\n", sum/N);
return 0;
}
Before printing, store them and sum them up.
int main()
{
srand((int)time(NULL));
int N=10;
float a = 1.0f, r, sum = 0.0f;
for (int i=0;i<N;i++){
r = ((float)rand()/(float)(RAND_MAX)) * a;
printf("%f\n", r);
sum += f;
}
printf("sum = %f\n", sum / N);
return 0;
}
I am bashing my head because I cannot figure out why my C code keeps printing the wrong average of a set of n numbers!
This is my code below:
int main()
{
int i;
int n;
int sum = 0.0;
int lowest;
int highest;
float average;
int range;
int middle;
double median;
printf("\nEnter the amount of numbers you want?\n");
scanf("%d",&n);
int numbs[n];
int temp[n];
for(i = 0;i < n; i++)
{
printf("\nEnter a number from 0 to 15: ");
scanf("%d",&temp[i]);
}
while (temp[i] < 0 || temp[i] > 15) than 15
{
printf("This number is not from 0 to 15! Please re-enter another number: ");
scanf("%d",&temp[i]);
}
numbs[i] = temp[i];
sum += numbs[i];
}
int sortt = 0, j, x;
for (x = 1; x < n; x++) {
for (j = 0; j < n - x; j++) {
if (numbs[j] > numbs[j + 1]) {
sortt = numbs[j];
numbs[j] = numbs[j + 1];
numbs[j + 1] = sortt;
}
}
}
lowest = numbs[0];
highest = numbs[n-1];
middle = n/2;
if (n % 2)
{
median = numbs[middle];
}
else
{
median = (numbs[middle - 1] + numbs[middle]) / 2.0;
}
average = sum/n;
range = highest - lowest;
printf("\nSum: %d", sum);
printf("\nAverage: %.2f", average);
printf("\nMedian: %.2f", median);
printf("\nRange: %d\n", range);
return 0;
}
This is my input and output below. You can see that 8 divided by 3 is not 2, it is 2.67! I've tried using double and float.
Input & Output:
You need to correct the following line:
average = sum/n;
to
average = (float)sum/n;
You have to cast your return value into float. Think about it as a function with the following definition:
float divide(int x,int y){
return x/y; // returns an integer instead of float.
}
While this definition:
float divide(int x,int y){
return (float)x/y; // creates a temporary float variable and returns it immediately as the returned value of the function.
}
In addition, declaring int sum=0.0 is definitely going to show you a warning when compiling with -Wall. Try to follow warnings that you get from your compiler and fix all of them before you run your program.
8 divided by 3 is 2, remainder 2. 8 and 3 are integers, and when you divide two integers, you use integer division with integer rules.
Also, this line might be confusing you:
int sum = 0.0;
Since sum is an int, this just sets sum to zero.
And:
average = sum/n;
Since both sum and n are integers, this is integer division. What you do with a result does not affect how that result is computed -- C's rules are complex enough already.
/*Here see you can intake all values as float instead */
#include <stdio.h>
#include <stdlib.h>
void main()
{
float i,n,a,b,sum,ave;
printf("This is a program to calculate the average of 'n' numbers \n");
printf("Of How many numbers do you want to calculate average \n");
scanf("%f", &n);
printf("Enter the first number \n");
scanf("%f", &a);
sum = a;
for (i=1;i<n;i++)
{
printf("Enter another number \n");
scanf("%f", &b);
sum = sum + b;
}
ave = (sum/n);
printf("The average of the %f number is %f", n, ave);
getchar();
}
I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.
This is what i've tried, and well there's no output when I enter the values for x,n:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.
While I've written in C, C++ solutions are also appreciated.
Output window in code::blocks
The loop
for(i=1;i<=t;i+2)
in the function series() is an infinite loop when t >= 1 because i isn't updated in the loop. Try changing + to += and use
for(i=1;i<=t;i+=2)
instead. Also it seems you should use type int for x and n in the function main() because the arguments of series() is int. Don't forget to change the format specifier when changing their types.
Thanks to all those who helped. Here's the final working code:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) i.e for factorial and multiply with -1 i.e to achive negavtive number alternatively. by using this temp variable and adding it to sum variable in every iteration will give us answer.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", °);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}
This code is showing following errors:
missing ) before type
calc: too few arguments to call
syntax error ) Visual stuio 2013 platform
MyCode:
#include "math.h"
void main()
{
float num[5];
float (calc (float num[5]));
calc(float num);/* transferring control to calc function)*/
getch();
}
float calc(float nun[5])
{
int i;
float num[5];
float sum, avg, sqmn1, sumsqmn = 0, sqsd = 0; float sd;
printf("\nEnter 5 numbers");
for (i = 0; i < 5; i = i + 1)
{
scanf("%f", &num[i]);
}
sum = 0;
for (i = 0; i < 5; i = i + 1)
{
sum = sum + num[i];
}
avg = sum / 5;
for (i = 0; i < 5; i = i + 1)
{
sqmn1 = (avg - num[i])*(avg - num[i]);
sumsqmn = sumsqmn + sqmn1;
}
sqsd = sumsqmn / 5;
sd = sqrt(sqsd);
printf("\nThe sum is %f", sum);
printf("\nThe average is %f", avg);
printf("\nThe stabdard deviation is %f", sd);
getch();
}
float (calc (float num[5]));
in your main(), what is this exactly?
IMO, it can be,
float ff;
ff = calc(num);
Other than that,
#include <stdio.h> is missing.
Forward declaration of float calc(float nun[5]) is missing.
You can rewite your main() as
int main()
{
float num[5];
float ff;
ff = calc(num);/* transferring control to calc function)*/
getch();
return 0;
}
but then also, you're passing num from main() to calc() but i see you never used it. What are you upto?