Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,463 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,063 people online right now. Registration is fast and FREE... Join Now!




calculate mean median, mode, std deviation using 2-D arrays

 
Reply to this topicStart new topic

calculate mean median, mode, std deviation using 2-D arrays, data analyses

tascofon
5 Feb, 2008 - 03:43 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 2

can anyone help me to complete this for me? The max, min, and mean works fine but i have tried to do that of the median, mode and standard deviation to no avail. I am a beginner and they've just thought us jsut for a week. I will be haapy to get many options (codes) from you.
Thanks
XX


CODE
#include <stdio.h>
#define STUDENTS 100
#define EXAMS 4

int minimum(const int scores[][EXAMS], int pupils, int test );
int maximum(const int scores[][EXAMS], int pupils, int test );
double mean(const int setOfScores[], int test);
void printArray(const scores[][EXAMS], int pupils, int tests);
void median( int setOfScores[], int test );
void bubbleSort( int a[][EXAMS], int, int );


int main(void)
{
    int student;
    int i, j, pupils, tests;

    
    int studentScores[ STUDENTS][EXAMS];
        
    FILE *infile;
    infile = fopen ("data.txt", "r");
    fscanf (infile, "%d %d", &pupils, &tests);
    for (i=0; i<pupils; i++) {
        for (j=0; j<tests; j++) {
            fscanf (infile, "%d", &studentScores[i][j]);
            }
        }
    

    /* output array studentScores */
    printf( "The array is:\n");
    printArray( studentScores, STUDENTS, EXAMS );

    /**************************************************
    ** determine smallest and largest grade values     *****
    **************************************************/
    printf("\n\nLowest grade: %d\nHighest grade: %d\n",
        minimum( studentScores, STUDENTS, EXAMS ),
        maximum( studentScores, STUDENTS, EXAMS ));

    /***************************************************
    **** calculate average grade for each student ************************************************/
    for (student = 0; student<STUDENTS; student++ ) {
    printf( "mean=The average grade for student %d is %.2f\n",
        student, mean(studentScores[student], EXAMS));

    /**************************************************
    ******** calculate median for each student **************
    *************************************************/
    for (student =0; student<STUDENTS; student++) {
        printf( "median= %d is %.2f\n",
        student, median(studentScores[student], EXAMS));

    }

    return 0;
}

/***********************************************************************
*********   Find the minimum grade                *******
***********************************************************************/
int minimum( const int scores[][EXAMS], int pupils, int tests )
{
    int i; /* student counter */
    int j; /* exam counter */
    int lowGrade = 100; /* initialize to highest possible grade */

    /* loop through rows of scores */
    for (i=0; i < pupils; i++) {

        /* loop through columns of scores */
        for (j=0; j < tests; j++) {

            if (scores[i][j] < lowGrade) {
                lowGrade = scores[i][j];
            }

        }
    } /*

    return lowGrade;
}

/**********************************************************
********                        Find the maximum grade    *******
*********************************************************/
int maximum( const int scores[][EXAMS], int pupils, int tests)
{
    int i;
    int j;
    int highGrade = 0; /*initialize to lowest possible grade */

    /* loop through rows of scores */
    for (i=0; i < pupils; i++) {
        /* loop through columns of scores */
        for (j=0; j < tests; j++) {

            if (scores[i][j] > highGrade ) {
                highGrade = scores[i][j];
            }
        }
    }

    return highGrade;
}

/******************************************************
*** Determine the average score for a particular student ********
*******************************************************/
double mean( const int setOfScores[], int tests )
{
    int i;
    int total = 0;

    /* total all scores for one student */
    for (i=0; i < tests; i++) {
        total += setOfScores[i];
    }

    return (double) total / tests;

}

/******************************************************
********      Print the array            ********
******************************************************/
void printArray(const int scores[][EXAMS], int pupils, int tests)
{
    int i;
    int j;


        for (i=0; i<pupils; i++) {

        /* output label for row */
        printf("\nstudentScores[%d] ", i);

        /* output scores for one student */
        for (j = 0; j < tests; j++) {
            printf( "%-5d", scores[i][j] );
        }
    }
void median( int setOfScores[], int tests)
{
    
    bubbleSort( studentScores[][EXAMS], int, int); /* sort array */
    printf("\n\nThe sorted array is ");
    printArray(setOfScores, int tests); /* output sorted array */

    /*display median element */
   printf("\n\nThe median is %d\n\n", scores[][ EXAMS / 2] );
}    /* end function median */

/*******************************************************
** function that sorts an array with bubble sort algorithm ***
*******************************************************/
void bubbleSort(int studentScores[][EXAMS], int hold, int test)
{
    int pass; /* pass counter */
    int i, j;    /* comparism counter */
    int hold; /* temporary location used to swap elements */
    
    /* loop to control number of passes */
    for (pass = 1; pass < tests; pass++)    {

        /* loop to control number of comparisons per pass */
        for (j = 0; j < tests -1; j++)    {

        /* swap elements if out of order */
        if (studentScores[i][j] > studentScores[i][j + 1]) {
           hold = studentScores[i][j];
           studentScores[i][j] = studentScores[i][j + 1];
           studentScores[i][j + 1] = hold;
        }
       }
    }
}


*mod edit - added code tags
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Calculate Mean Median, Mode, Std Deviation Using 2-D Arrays
5 Feb, 2008 - 03:47 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, welcome to </DIC>

Please when posting code code.gif

And please tell us what is the problem with the code, errors maybe?
User is offlineProfile CardPM
+Quote Post

tascofon
RE: Calculate Mean Median, Mode, Std Deviation Using 2-D Arrays
6 Feb, 2008 - 06:59 PM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 2

QUOTE(PennyBoki @ 5 Feb, 2008 - 04:47 PM) *

Hi, welcome to </DIC>

Please when posting code code.gif

And please tell us what is the problem with the code, errors maybe?




yes, the code is producing errors particularly with the median and bubblesort areas. if anyone could correct the code for me i WOULD BE HAPPY
tthanks
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Calculate Mean Median, Mode, Std Deviation Using 2-D Arrays
8 Feb, 2008 - 12:20 AM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Posting the specific errors will (i.e. copy-paste the errors into your post in quote tags) will help us track down the problem much quicker.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month