There was no big problem when the function had 1 variable. However, there was a problem because there were many variables and many declarations.
This program applies avg (where to store the mean) and std (where to store the standard deviation) throughout the program You can find the mean and standard deviation of a 10-digit number. But...
#include <stdio.h>
#include <math.h>
#define NUM 10
float avg, std;
void read_array(float n[], int size);
float average(float n[], int size);
float std_dev(float n[], int size, float average);
void print(float avg, float std);
int main()
{
float a[NUM];
read_array(a, NUM);
average(a, NUM);
std_dev(a, NUM, avg);
print(avg, std);
return 0;
}
void read_array(float n[], int size)
{
int i;
printf ("Enter %d numbers one after another."\n", size);
for (i = 0; i < size; ++i)
{
scanf_s("%f", &(n[i]));
}
}
float average(float n[], int size)
{
float sum = 0;
int i;
for (i = 0; i < size; ++i)
{
sum += n[i];
}
avg = sum / size;
return avg;
}
float std_dev(float n[], int size, float average)
{
float sum = 0;
int i;
for (i = 0; i < size; ++i)
{
sum += (n[i] - average)*(n[i] - average);
}
std = sqrt(sum / size);
return std;
}
void print(float avg, float std)
{
printf ("average = %).2f\n", avg);
printf ("standard deviation = %").2f\n", std);
}
What I intended to do was to have the mean and standard deviation left in the main function to be calculated ...Tears
Like this ("float avg, std;" only changed the digits.)
#include <stdio.h>
#include <math.h>
#define NUM 10
void read_array(float n[], int size);
float average(float n[], int size);
float std_dev(float n[], int size, float average);
void print(float avg, float std);
int main()
{
float a[NUM];
float avg, std;
read_array(a, NUM);
average(a, NUM);
std_dev(a, NUM, avg);
print(avg, std);
return 0;
}
void read_array(float n[], int size)
{
int i;
printf ("Enter %d numbers one after another."\n", size);
for (i = 0; i < size; ++i)
{
scanf_s("%f", &(n[i]));
}
}
float average(float n[], int size)
{
float sum = 0;
int i;
for (i = 0; i < size; ++i)
{
sum += n[i];
}
avg = sum / size;
return avg;
}
float std_dev(float n[], int size, float average)
{
float sum = 0;
int i;
for (i = 0; i < size; ++i)
{
sum += (n[i] - average)*(n[i] - average);
}
std = sqrt(sum / size);
return std;
}
void print(float avg, float std)
{
printf ("average = %).2f\n", avg);
printf ("standard deviation = %").2f\n", std);
}
However, if you write and run it like this, it says that avg and std are not declared. crying Shouldn't we have declared it in the main function? I don't know where to fix it. Or I wonder if there is only the first one.
function declare
Learn about global and regional variables.
In the function below, the variables avg, std have not been declared. You must explicitly declare a regional variable.
float average(float n[], int size)
{
float sum = 0;
int i;
for (i = 0; i < size; ++i)
{
sum += n[i];
}
//avg = sum / size;
float avg = sum / size; //! Modify
return avg;
}
float std_dev(float n[], int size, float average)
{
float sum = 0;
int i;
for (i = 0; i < size; ++i)
{
sum += (n[i] - average)*(n[i] - average);
}
//std = sqrt(sum / size);
floatstd = sqrt(sum/size); //! Modify
return std;
}
© 2024 OneMinuteCode. All rights reserved.