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;
}
Related
#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.
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();
}
#include<stdio.h>
#include<math.h>
int main()
{
int n;
scanf("%d",&n);
int i=0,sum=0,average=0;
int num[100]={0};
double dvalue[100]={0.0};
double variance=0.0,s=0.0;
for(i=0;i<n;i++){//I just know this method to get the number array;
scanf("%d",&num[i]);
sum+=num[i];
}
average=sum/n;
for(i=0;i<n;i++){
dvalue[i]=num[i]-average;
dvalue[i]=pow(dvalue[i],2.0);//to get the (x-average)……2
variance+=dvalue[i];
}
s=sqrt(variance);
return 0;
}
this is my code,but it can't get the right answer.I am a new learner and this is the first time I seek help via stackoverflow I can't get the output why?
Thanks for every one .I actually debug at first ,but I can't know how to modify it ,sincerely thanks again
There were two things to correct:
1. type of average should be double
2. you have taken the variance incorrectly. after taking the sum of (x- average) you should divide it by n
#include<stdio.h>
#include<math.h>
int main()
{
int n;
scanf("%d", &n);
int i = 0, sum = 0;
double average = 0;
int num[100] = { 0 };
double dvalue[100] = { 0.0 };
double variance = 0.0, s = 0.0;
for (i = 0; i < n; i++) { //I just know this method to get the number array;
scanf("%d", &num[i]);
sum += num[i];
}
printf("sum %d\n", sum);
average = sum;
average = average / n;
printf("average %lf\n", average);
for (i = 0; i < n; i++) {
dvalue[i] = num[i] - average;
dvalue[i] = pow(dvalue[i], 2.0); //to get the (x-average)……2
variance += dvalue[i];
}
variance = variance / n; // population variance
s = sqrt(variance);
printf("varience %lf\n", variance);
printf("Population Standard deviation %lf", s);
return 0;
}
edited the answer according to #chux advice
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?
Friends in following code i am trying to get some of the average in float...i tried manually my ans should be in fraction, but here it comes in rounded fig...please help me.
#include <stdio.h>
#include <stdlib.h>
//finding out the average marks for students ....
float average (float []);
int main()
{
int i;
float total_avg=0;
float arr_1[] = {10.2, 22.9, 36.04, 89.1, 94.1, 10.8,};
for(i=0; i<6; i++)
printf("%2f\n",arr_1[i]);
printf("\n\n");
total_avg = average (arr_1);
printf("Total average : %3f\n",total_avg);
system("PAUSE");
return 0;
}
float average (float a[5])
{
int sum = 0, i;
float total;
for(i=0; i<6; i++)
{
sum = sum+a[i];
}
total = sum/5;
return total;
}
Once you've fixed the type of sum as pointed out by bash.d, your answer will still be wrong. arr_1 contains 6 elements but you divide by 5 when calculating the average. You need to change the calculation of total to
total = sum/6;
Or, better still, change average to
float average(float* a, int num_elems)
{
float sum = 0
int i;
for(i=0; i<num_elems; i++) {
sum += a[i];
}
return sum/num_elems;
}
and call average like
total_avg = average (arr_1, sizeof(arr_1)/sizeof(arr_1[0]));
Why use int as sum? Use float instead:
float average (float a[5])
{
int sum = 0,
float total, sum = 0.0f;
for(i=0; i<6; i++)
{
sum = sum+a[i];
}
total = sum/5;
return total;
}