Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:17:29+00:00 2026-05-28T19:17:29+00:00

Trying to write this small program to help me in my Stats class, everything

  • 0

Trying to write this small program to help me in my Stats class, everything seems to be calculating accordingly except for the Median. What am i missing?

Extra Credit if anyone is willing to do the Variance function for me as well x).

Running OSX with the GCC compiler.

#include<stdio.h>  
#include<math.h>   

float mean1(float[],int);  
float median1(float[],int);  
float mode1(float[],int);  
double standarddeviation1(float[],int);  

int main()  
{  
int i,n,choice;  
float array[100],mean,median,mode;  
double standarddeviation;  

printf("Enter No of Elements\n");  
   scanf("%d",&n);  
printf("Enter Elements\n");  

for(i=0;i<=n-1;i++)  
    scanf("%f",&array[i]);  
do  
{  
    printf("\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n\t4.Standard deviation\n\t5.Exit\n");  
    scanf("%d",&choice);  

    switch(choice)  
    {  
        case 1: mean=mean1(array,n);  
            printf("\n\tMean = %f\n",mean);  
            break;  
        case 2: median=median1(array,n);  
            printf("\n\tMedian = \n",median);  
            break;  
        case 3: mode=mode1(array,n);  
            printf("\n\tMode = %f\n",mode);  
            break;   
        case 4: standarddeviation=standarddeviation1(array,n);  
            printf("\n\tStandard deviation = %f\n",standarddeviation); 
            break;  
        case 5: break;  
        default:printf("Wrong Option");  
            break;  
    }  

}while(choice!=5);  
  getchar(); 

return 0;  
}  

float mean1(float array[],int n) {  
    int i;  
    float sum=0;  
    for(i=0;i<=n;i++)  
    sum=sum+array[i];  
return (sum/n);  
}  
float median1(float array[],int n) {  
float temp;  
int i,j;  
for(i=n-1;i>=0;i--)  
    for(j=0;j<=i;j++)  
        if(array[j]>=array[j+1])  
        {  
            temp=array[j];  
            array[j]=array[j+1];  
            array[j+1]=temp;  
        }  

if(n%2==0)  
    return (array[n/2]+array[n/2-1])/2;  
else  
    return array[n/2];  
}  
float mode1(float array[],int n) {  
return (3*median1(array,n)-2*mean1(array,n));  
}  
double standarddeviation1(float array[],int n) {  
int j;   
double max[100],sum,variance,mean;  
mean=mean1(array,n);  
sum=0;  
for(j=0;j<=n;j++)   
{  
    max[j]=pow((array[j]-mean),2);  
    sum+=max[j];  
}  
variance=sum/(j-1);   
return sqrt(variance);  
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:17:30+00:00Added an answer on May 28, 2026 at 7:17 pm

    Your mean1 isn’t correct either. Both errors have the same reason, you access an element past the assigned number n

    float mean1(float array[],int n) {  
        int i;  
        float sum=0;  
        for(i=0;i<=n;i++)  
        sum=sum+array[i];  
    return (sum/n);  
    }
    

    You are adding n+1 elements here but divide by n. The n+1st element doesn’t belong in the calculation. Make the loop condition i < n.

    float median1(float array[],int n) {  
    float temp;  
    int i,j;  
    for(i=n-1;i>=0;i--)  
        for(j=0;j<=i;j++)
    

    In your bubble sort, you access array[n] too, which has an arbitrary value (and if n == 100 is past the allocated array, hence causes undefined behaviour). Make the inner loop condition j < i or start the outer loop with i = n-2.

    In standarddeviation1 you overstep the array bounds too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that cleans data, using Matlab. This program takes
I am trying to write a small program which has to store and retrieve
I'm trying to write a small class library for a C++ course. I was
I am just learning C++ and am trying to write a small program to
I'm trying to learn Haskell and want to write a small program which prints
I am trying to write a small java program that will accept a file
I'm trying to write a small java program that connects to a twitter search
I am trying to write a small program, that opens a server, creates a
I'm trying to write a small client server program. The Server is in C#,
I'm trying to write a really small program in Java that takes a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.