In an exercice I have to give to a function an array and his size to get the average of his value.
So I've tried this :
#include <stdio.h>
#include <stdlib.h>
double average(double array[], double array_size);
int main()
{
double array[4] = {12.0, 20.0, 8.9, 14.7};
printf("The average of the array is : %lf\n", average(array, 4.0));
return 0;
}
double average(double array[], double array_size)
{
int array_average = 0, i = 0;
while(i < array_size)
{
array_average += array[i];
i++;
}
return array_average / array_size;
}
I clang the file and run the ./a.out and that return me 13.500000 instead of 13.9 when I do the average with the calculator.
I don't see where is the errors, thanks for your help !
If that can help someone later I post my final code
#include <stdio.h>
#include <stdlib.h>
double average(double array[], double array_size);
int main()
{
double array[4] = {12.0, 20.0, 8.9, 14.7};
printf("The average of the array is : %lf\n", average(array, 4.0));
return 0;
}
double average(double array[], double array_size)
{
double array_average = 0.0;
int i = 0;
while(i < array_size)
{
array_average += array[i];
i++;
}
return array_average / array_size;
}
Related
Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}
I am pretty new to programming and I was working on printing arrays using functions and I ran to the following error.
Test.c: In function ‘main’:
Test.c:21:54: error: incompatible type for argument 1 of ‘theta’
21 | printf("The theta values are = %lf\n", x[i], theta(x[i]));
| ~^~~
| |
| double
Test.c:5:21: note: expected ‘double *’ but argument is of type ‘double’
5 | double theta(double x[N])
| ~~~~~~~^~~~
And here is the code.
#include <stdio.h>
#include <stdlib.h>
#define N 50
double theta(double x[N]);
int main(){
int i;
double x[i];
printf("The theta values are = %lf\n", x[i], theta(x[i]));
return 0;
}
double theta(double x[N]){
int i;
for(i = 0; i < 50; i++){
x[i] = (double)(i)/ ((double)(N) - 1.0);
}
return x[i];
}
I just want to print 50 values between 0 and 1. Just like linspace(0:1:50) in MATLAB.
Thanks for the help.
First, in declare function, you cannot set size of array.
change to
double theta(double* X)
Second, do double theta(double x[N]); in main function.
and this is compiler didn`t advise you but I found bug in your code.
First, i in theta is different with i in main
Second, use for to print
I think you want this code
#include<stdio.h>
#include<stdlib.h>
#define N 50
double* theta()
{
int i;
double x[N];
for(i = 0; i < N; i++){
x[i] = (double)(i)/ ((double)(N) - 1.0);
}
return x;
}
int main(){
int i;
double* x=theta();
for(i=0;i<N;i++){
printf("The theta values are = %lf\n", x[i]);
}
return 0;
}
And I like this code more
#include<stdio.h>
#include<stdlib.h>
#define N 50
int main(){
int i;
for(i=0;i<N;i++){
printf("%lf\n",i/(N-1.0));
}
return 0;
}
I found the solution. Here it is.
#include <stdio.h>
#include <stdlib.h>
#define N 50
double* theta(double x[], int nb_elements){
int i;
for(i = 0; i < nb_elements; i++){
x[i] = (double)(i)/ ((double)(nb_elements) - 1.0);
}
return &x[0];
}
int main(){
int i;
double x[N];
theta(x, N);
for(i = 0; i < N; i++){
printf("%lf\n", x[i]);
}
return 0;
}
I would like to write a program which finds the minimal number of 5 inputted numbers. I'm stuck at the point when I want to use function getMinNum, but there is an error saying: expected expression before ']' token
I understand it has a connection with pointers, however I would like to do it without them if it is possible of course.
#include <stdio.h>
#include <stdlib.h>
float getMinNum(float a[], int x);
int main()
{
int n = 5;
int i;
float z[n];
for(i=0; i<n; i++){
scanf("%f", &z[i]);
}
printf("%6.2f", getMinNum(z[], n));
return 0;
}
float getMinNum(float a[], int x)
{
int i, min = a[0];
for(i=0; i<x; i++){
if(min > a[i+1]){
min = a[i+1];
}
}
return min;
}
You shouldn't append '[]' to the variable name.
Instead of:
printf("%6.2f", getMinNum(z[], n));
do:
printf("%6.2f", getMinNum(z, n));
Your a[i+1] will be using values outside the array, so use a[i] instead.
So the code should look like
float getMinNum(float a[], int x){
int i;
float min = a[0]; // Min needs to be a float
for(i=1; i<x; i++){ // Do not need to check a[0]
if(min > a[i]){
min = a[i];
}
}
return min;
}
And call it as
printf("%6.2f", getMinNum(z, n));
The following code is trying to find the averages of a set of numbers in C, but the median and the mean both do not return anything. How do I make it so the mean and the median both return a float? Am I returning an invalid value or?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
float mean(int arr[], int size){
int sum = 0;
for(int i = 0; i<size; i++){
sum += arr[i];
}
return ((float)sum/size);
}
int range(int arr[], int size){
int smallest = arr[0];
int largest = arr[0];
for(int i=0; i<size; i++){
if(smallest>arr[i]){
smallest = arr[i];
} if(largest<arr[i]){
largest = arr[i];
}
} int difference = largest - smallest;
return difference;
}
int mode(int arr[], int size){
int maxValue = 0;
int maxCount = 0;
for(int i = 0; i<size; i++){
int count = 0;
for(int j = 0; j<size; j++){
if(arr[j] == arr[i]){
count++;
}
} if(count > maxCount){
maxCount = count;
maxValue = arr[i];
}
} return maxValue;
}
float median(int arr[], int size){
qsort(arr, size, sizeof(int), compare);
float middleOfArray = size/2;
int roundedMiddleOfArray = rint(middleOfArray);
if(ceilf(middleOfArray) == middleOfArray){
return((float)arr[roundedMiddleOfArray]);
}
else{
return((float)arr[roundedMiddleOfArray] - arr[roundedMiddleOfArray-1]);
}
}
int main(){
int array[6] = {1,2,3,4,5,6};
int newMean = mean(array, 5);
int newRange = range(array, 5);
int newMode = mode(array,5);
int newMedian = median(array, 5);
printf("The mean is : %f \n", newMean);
printf("The range is : %d \n",newRange);
printf("The mode is : %d \n",newMode);
printf("The median is : %f \n", newMedian);
return 0;
}
Turns out the way you fix it is by declaring the mean and the median as floats not ints (in main).
Given the following code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define NPOINTS 200
#define NMEASURES 50
#define PI 3.1415f
double mcIntSingleExp1(int);
double mcIntSingleExp1(int n){
int i, countIn = 0;
double x,y, integral1, integral2;
srand(time(NULL));
for(i = 0; i < n; i++){
x = ((double)rand()/(double)RAND_MAX)*PI;
y = (double)rand()/(double)RAND_MAX;
if(y <= sin(x))
countIn++;
}
integral1 = (PI * (double)countIn)/(double)n;
countIn = 0;
for(i = 0; i < n; i++){
x = ((double)rand()/(double)RAND_MAX) + PI;
y = ((double)rand()/(double)RAND_MAX) -1;
if(y >= sin(x))
countIn++;
}
integral2 = (PI * (double)countIn)/(double)n;
return integral1 - integral2;
}
double mcIntSingleExp2(int);
double mcIntSingleExp2(int n){
int i;
double x, sum = 0;
srand(time(NULL));
for(i = 0; i < n; i++){
x = ((double)rand()/(double)RAND_MAX) + 2*PI;
sum += sin(x);
}
return (1/(double)n) * sum;
}
void mcIntMultExp1(int, double [], int);
void mcIntMultExp1(int k, double res1[], int n){
int i;
for(i = 0; i < k; i++)
res1[i] = mcIntSingleExp1(n);
}
void mcIntMultExp2(int, double [], int);
void mcIntMultExp2(int k, double res2[], int n){
int i;
for(i = 0; i < k; i++)
res2[i] = mcIntSingleExp2(n);
}
double mean(double [], int);
double mean(double v[], int size){
int i;
double sum = 0;
for(i = 0; i < size; i++)
sum += v[i];
return sum/(double)size;
}
double stdDev(double [], int);
double stdDev(double v[], int size){
int i;
double avg, std_dev = 0;
avg = mean(v,NMEASURES);
for(i = 0; i < size; i++)
std_dev += (v[i]-avg)*(v[i]-avg);
return sqrt(std_dev/(double)size);
}
int main(){
double measure, deviation, res1[NMEASURES], res2[NMEASURES];
mcIntMultExp1(NMEASURES, res1, NPOINTS/2);
mcIntMultExp2(NMEASURES, res2, NPOINTS);
measure = mean(res1, NMEASURES);
deviation = stdDev(res1, NMEASURES);
printf("\nIntegral 1 = %.14lf\nStandard Deviation 1 = %.14lf\n",measure,deviation);
measure = mean(res2, NMEASURES);
deviation = stdDev(res2, NMEASURES);
printf("\nIntegral 2 = %.14lf\nStandard Deviation 2 = %.14lf\n",measure,deviation);
return 0;
}
In the stdDev function I noticed that each element of the vector v is equal to avg (v [0] = v [1] = ... = avg), so the standard deviation is zero! In fact, the elements of v should be those of res1 or res2. I do not understand what is wrong!
Your code probably runs quickly enough that time(NULL) will usually always return the same value. After doing an srand with a given seed, the following calls to rand will return the same values each time.
We usually only execute srand(time(NULL)) once at the beginning of a program.