Can someone help figure out what I'm doing wrong in my script? I'm trying to prompt the user to choose 1 of 2 total udfs and then display values but the 1st udf only shows 0.000 for all the float values and the 2nd udf behaves similarly. I need to stick to the stdio.h and math.h headers,
(C-PROGRAMMING) Can someone help figure out what I'm doing wrong in my script? I'm trying to prompt the user to choose 1 of 2 total udfs and then display values but the 1st udf only shows 0.000 for all the float values and the 2nd udf behaves similarly. I need to stick to the stdio.h and math.h headers, please.
#include <stdio.h>
#include <math.h>
void compute(int n){
float sum=0;float arr[100]; int i;
float average, variance = 0.0, standard_deviation;
for (i = 0; i < n; ++i)
{
sum = sum + arr[i] ;
average = sum / n;
variance += pow(arr[i] - average, 2);
variance = variance / n;
printf("\nWith the provided values, the average is %.2f and variance is %.2f. \n", average, variance, standard_deviation);
{
if (arr[i] > average)
printf("%.2f, which is larger than our average of %.2f. \n", arr[i], average);
else
printf("%.2f, which is less than our average of %.2f. \n", arr[i], average);
} standard_deviation = sqrt(variance);
printf("Standard dev is %f", standard_deviation);}}
void sort(float arr[],int n,int i){
printf("\nSorting the elements from largest to smallest:\n\n");
for (i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (arr[i] < arr[j])
{
float a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
}
int main(){
int cse;
printf("Which case will we be working with?");
printf("Type the numnber of the case you want");
scanf("%d",&cse);
//from there use if for each value of cse to connect to a specific UDF to do each case, i ran out of time
int n, i;
float sum = 0.0;
float average, variance = 0.0, standard_deviation;
printf("Enter the number of elements:\n");
scanf("%d", &n);
float arr[n];
for (i=0;i<n;++i)
{
printf("Enter the value of element %d:\n", i + 1);
scanf("%f", &arr[i]);
sum += sum + arr[i];
}
if(cse==1){
compute(n);
}
if(cse==2){
sort(arr,n,i);
}
if(cse==3){compute(n);
}
return 0;
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)