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

  • Home
  • SEARCH
  • 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 7539495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:23:49+00:00 2026-05-30T07:23:49+00:00

I have this homework assignment where the user is asked to input numbers and

  • 0

I have this homework assignment where the user is asked to input numbers and then calculates the mean median and mode, followed by asking if he/she wants to play again, and either repeating the program or quitting. Everything compiles, but I can seem to figure out the few things going wrong:

The mean works. the median doesn’t. If the array of ints has an even length, ie 4 numbers in the array, the median is supposed to be the middle two numbers averaged out. so if the numbers are ‘1, 3, 5, 6’ in order, then the median should be 4.000000. The mode doesn’t work either, and when asked to ‘play again?’ any answer causes the program to suddenly exit and crash. can someone help me find the error in my mean median mode calculations, and help me with the menu?

#define MAX 25
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>

int readTotalNums();
void fillArray(int total, int nums[]);
void sortArray(int nums[], int total);
double findMean(int nums[], int total);
double findMedian(int nums[], int total);
int findMode(int nums[], int total);
void printResults(double mean, double median, double mode);
bool goAgain();

int main()  {
    int nums[MAX];
    int total;
    double mean, median, mode;
    do {
        total = readTotalNums(); //guarantee 1-25
        fillArray(total, nums); //read in the #s don't need to check range
        sortArray(nums, total);
        mean = findMean(nums, total);
        median = findMedian(nums, total);
        mode = findMode(nums, total);
        printResults(mean, median, mode);
    } while (goAgain());
    return 0;
}

int readTotalNums() {
    int num;
    do {
        printf("How many numbers? ");
        scanf("%i", &num);
    } while (num < 1 || num > 25);
    return num;
}

void fillArray(int total, int nums[]) {
    int temp;
    int i;
    printf("Please enter %i numbers\n", total);
    for (i = 0; i <= total-1; i++) {
        scanf("\n%i",&nums[i]);
    }
}

void sortArray(int nums[], int total) {
int x;
int y;
for(x=0; x<total; x++) {
    for(y=0; y<total-1; y++) {
        if(nums[y]>nums[y+1]) {
            int temp = nums[y+1];
            nums[y+1] = nums[y];
            nums[y] = temp;
        }
    }
}
}

double findMean(int nums[], int total) {
    int i;
    double sum = 0.0;
    for(i = 0; i < total; i++) {
        sum += nums[i];
    }
    return (sum/total);
}

double findMedian(int nums[], int total) {
    int temp;
    int i,j;
    for(i=0;i<total;i++)
        for(j=i+1;j<total;j++) {
            if(nums[i]>nums[j]) {
                temp=nums[j];
                nums[j]=nums[i];
                nums[i]=temp;
            }
        }
        if(total%2==0) {
            return (nums[total/2]+nums[total/2-1])/2;
        }else{
            return nums[total/2];
        }
}

int findMode(int nums[],int total) {
    int i, j, maxCount, modeValue;
    int tally[total];
    for (i = 0; i < total; i++) {
         tally[nums[i]]++;
    }
    maxCount = 0;
    modeValue = 0;
    for (j = 0; j < total; j++) {
        if (tally[j] > maxCount) {
            maxCount = tally[j];
            modeValue = j;
        }
    }
    return modeValue;
}

void printResults(double mean, double median, double mode) {
    printf("Mean: %d\tMedian: %d\tMode: %i", mean, median, mode);
}


bool goAgain() {
    char *temp;
    printf("\nWould you like to play again(Y/N)? ");
    scanf("%s", &temp);
    while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y') {
        printf("\nI am sorry that is invalid -- try again");
        printf("\nWould you like to play again(Y/N)? ");
        scanf("%s", &temp);
    }
    if (temp == 'y' || temp == 'Y') {
        return true;
    } else {
        return false;
    }
}

the output should be something like this:

How many numbers 4
Please enter 4 numbers
6
2
5
25
 Mean: 9.50 Median:  5.50   Mode:  2
Go again (y/n) n
  • 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-30T07:23:51+00:00Added an answer on May 30, 2026 at 7:23 am

    Well, I found 3 problems:

    1. Your printf is wrong. to print double, you should use %f. not %d or %i.
    2. You should initialize tally before using.
    3. In goAgain, temp should be char, and you should use %c instead of %s.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework assignment which asks to have the user input a date
(this is indirectly a part of a much larger homework assignment) I have something
Note: This is a homework assignment. I have two classes, one inherits from the
This is homework...I'm not asking for answers, I just have a bug I'm not
I have a homework assignment, and i am finished other then one question (see
Continuing on this problem , but I'll reiterate: For a homework assignment I have
This is part of a homework assignment. What we have to do is write
This is a homework assignment, so I'm mostly asking for a nudge in the
I'm still not very good with data structures, but I have this homework assignment
This was a homework assignment problem which I know I have incorrectly answered. I

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.