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,

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

(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;
}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education