Finding Mode Median and Mean

This is a discussion on Finding Mode Median and Mean within the C Programming forums, part of the General Programming Boards category; I have to turn this code in tomorrow by 4:00. It will not run right. I found out it is ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Unhappy Finding Mode Median and Mean

    I have to turn this code in tomorrow by 4:00. It will not run right. I found out it is not even going through the sort loop and I can't figure it out. Please, can anyone help me out?
    Problem: Write a program to compute the arithmetic mean (average) median, and mode for up to 50 test scores. The data are contained in a text file.
    Code:
    #include <stdio.h>
    #include<math.h>
    
    #define NULL 0
    
    FILE *scoresFile;
    int scores[50];
    
    /*Function Definitions*/
    int readFile		(int);
    void calculateMean	(int);
    void sort			(int);
    void calculateMedian(int);
    void calculateMode	();
    
    main()
    {
     
    int numberOfScores = 0;
    
    numberOfScores = readFile(numberOfScores);
    calculateMean(numberOfScores);
    sort(50);
    calculateMedian(numberOfScores);
    calculateMode();
    
    
    }
    /*Read the file with scores*/
    //*************************read file*********************
    int readFile	(int numberOfScores)
    
    	{ 
    
    	/*bool flag = TRUE;*/
    	
    	scoresFile = fopen("scores.txt","r");
    	
    	if(scoresFile == NULL)
    
    	{
    	printf("\nERROR-cannot open the file\n");
    	}
    	
    	/*loop to read scores from file*/
    
    	/*read each entry from file*/
    
    		while ( fscanf(scoresFile, "%d", &scores[numberOfScores]) != EOF && numberOfScores < 50)
    		{
    			printf("\n%d",scores[numberOfScores]);
    			numberOfScores++;
    		
    			if(numberOfScores > 50)
    				{
    				printf("\nERROR-More than 50 scores available");
    				}
    		}
    	fclose(scoresFile);
    	return numberOfScores; 
    	}
    /*===============================================================*/
    void calculateMean( int numberOfScores)
    {
    int i, total;
    int mean;
    total = 0;
    
    printf("\n%d",numberOfScores);
    
    for(i = 0; i <= numberOfScores; i++)
    {
    total += scores[i];
    printf("\n%d",total);
    
    }
    
    mean = total/numberOfScores;
    
    printf("\nMean of the Scores:  %d" , mean);
    }
    
    /*================================================================*/
    void sort(int array_size)
    {
    	int i, j, temp, x;
    	for (i = array_size-1; i>= 0; i-- )
    		{
    			for (j=1; j<= i; j++)
    			{
    				if(scores[j-1] > scores[j])
    				{
    					temp = scores[j-1];
    					scores[j-1] = scores[1];
    					scores[1] = temp;
    				}
    			
    			}
    		}
    		printf("\n\n\n");
    	for (x=0;x <= 20; x++){
    	 printf("\n%d",scores[x]);
    	}
    }
    /*================================================================*/
    
    void calculateMedian(int numberOfScores)
    {
    float median;
    median = (scores[numberOfScores/2 - 1 ] + scores[numberOfScores/2])/2;
    printf("\nMedian:  %f", median);
    }
    
    /*================================================================*/
    
    void calculateMode()
    {
    
    int multi[50][2];
    int j, k;
    int higher;
    int score;
    
    /*initialize the array second element to 0*/
    for(k=0; k<50; k++)
    {
    multi[k][0] = 0;
    multi[k][1] = 0;
    }
    
    /*pass the original array and store array into multidimensional if there are no entries for that value*/
    
    for(j=0; j<50; j++)
    {
    
    score = scores[j];
    for(k = 0; k< 50; k++)
    {
    if(score == multi[k][0])
    	{
    	multi[k][1] = multi[k][1] + 1;
    	}
    }
    }
    higher = multi[0][1];
    
    for(j=0; j<50; j++)
    	{
    	if(higher , multi[j][1])
    	higher= multi[j][1];
    	}
    	
    	printf("\nMode of the scores:  %d", higher);
    	
    }
    Here is my text file:

    98
    100
    96
    92
    91
    89
    87
    86
    85
    83
    82
    81
    83
    85
    65
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    80
    81
    83
    72
    73
    74
    75
    76
    77
    78
    79
    85
    84
    83
    82
    100
    89
    96
    97
    88
    67
    68
    69
    70
    80

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Code:
    temp = scores[j-1];
    scores[j-1] = scores[1];
    scores[1] = temp;
    Are you sure you don't want this to be:

    Code:
    temp = scores[j-1];
    scores[j-1] = scores[j];
    scores[j] = temp;


    Code tags fixed by Hammer
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    4
    Sorry! thanks for the tip. Yea I am new. Didn't know what tags were.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21