GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jun-2009, 21:19
jusmeehh jusmeehh is offline
New Member
 
Join Date: Jun 2009
Posts: 1
jusmeehh is on a distinguished road

Mean (average), median, and mode


Write a program to compute the aritmetic mean (average), median, and mode for up to 50 test scores. The data are contained in a text file. The program will also print a histogram of the scores.
The program should start with a function to read the data file and fill the array. Note that there may be fewer than 50 scores. This will require that the read function return the index for the last element in the array.
To determine the average. To determine the median, you mst first sort the array. The median is the score at last / 2 if last is an even inde and by averaging the scores at the floor and ceiling of last / 2 if last is odd. The mode is the score that occurs the most often. It can be determined as a byprodcut of building which score occurred most often. (Note that two scores can occur the same number of times.)

This is what I came up using Bloodshed Dev-C++:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

double average (int ary[ ]);
void printAverage (int data[ ], int size, int lineSize);

int main (void)
{
	double avg;
	int    base[50] = {30, 70, 20, 40, 50, 10, 90, 80, 40, 100,
                      90, 30, 60, 80, 20, 90, 100, 50, 30, 20,
                      70, 10, 50, 70, 90, 20, 10, 90, 50, 80, 
                      40, 90, 20, 20, 30, 40, 60, 50, 70, 90,
                      100, 20, 30, 40, 50, 60, 70, 80, 90, 90};

	avg = average(base);
    printAverage (nums, size, 10);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
	
	
    system ("pause");
    return 0;
}	

double average (int ary[ ])
{
	int sum = 0;
	for (int base = 0; base < 50; base++)
	     sum += ary[base];

	return (sum / 5.0);
}	

void printAverage (int data[], int size, int lineSize)
{
	int numPrinted = 0;

	printf("\n\n");
	for (int i = 0; i < size; i++)
	   {
	    numPrinted++;
	    printf("%2d ", data[i]);
	    if (numPrinted >= lineSize)
	       {
	        printf("\n");
	        numPrinted = 0;
	       } 
	   } 
	printf("\n\n");
	return;
}
Last edited by LuciWiz : 16-Jun-2009 at 02:52. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 16-Jun-2009, 09:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need Help Asap With C Programming Project!!


Quote:
Originally Posted by jusmeehh
...This is what I came up...

So what happened when you compiled it?

The title that you gave the thread is, "Need Help Asap With C Programming Project!!"

What kind of help would you like? The likelihood of getting help "Asap" might be improved if you ask specific questions rather than just posting the statement of the assignment along with some code that won't compile.

As far as the assignment itself, my approach would be to develop the code a step at a time.


Step number 1:
Quote:
The program should start with a function to read the data file and fill the array.



Regards,

Dave
  #3  
Old 17-Jun-2009, 04:13
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: Mean (average), median, and mode


Just a note to let you know that I edited the title of this thread.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #4  
Old 18-Jun-2009, 07:19
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Mean (average), median, and mode


Quote:
Originally Posted by jusmeehh
Write a program to compute the aritmetic mean (average), median, and mode for up to 50 test scores. The data are contained in a text file. The program will also print a histogram of the scores.
The program should start with a function to read the data file and fill the array. Note that there may be fewer than 50 scores. This will require that the read function return the index for the last element in the array.
To determine the average. To determine the median, you mst first sort the array. The median is the score at last / 2 if last is an even inde and by averaging the scores at the floor and ceiling of last / 2 if last is odd. The mode is the score that occurs the most often. It can be determined as a byprodcut of building which score occurred most often. (Note that two scores can occur the same number of times.)

This is what I came up using Bloodshed Dev-C++:

You didn't read the part that said that there may be fewer than 50 scores. Your functions will need to know the length of the array.

As Dave mentioned, you need to read the values in from a file of scores.


Maybe this will help get you started:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifndef __cplusplus
typedef char bool;
#endif

#ifndef TRUE
#define TRUE     (1)
#define FALSE    (0)
#endif

typedef int* INT_ARRAY;

void print_array(const INT_ARRAY pia, const size_t count)
{
    size_t i;
    if(pia != NULL && count > 0)
    {
        for(i = 0; i < count; i++)
        {
            printf("pia[%d] = %d\n", (int)i, pia[i]);
        }
    }
}

#define MAX(x, y) (x > y ? x : y)
#define MIN(x, y) (x < y ? x : y)

int compareGreaterOf(const void* pv1, const void* pv2)
{
    const int* p1 = (int*)pv1;
    const int* p2 = (int*)pv2;
    return (*p1 - *p2);
}

int mean(const int* p, const size_t len)
{
    size_t i;
    int avg = 0;
    if(len > 0 && p != NULL)
    {
	for(i = 0; i < len; i++)
	{
	    avg += p[i];
	}
	avg = avg/len;
    }
    return avg;
}

int median(int* p, const size_t len)
{
    int f, l, med = 0;
    
    if(len > 0 && p != NULL)
    {
	qsort(p, len, sizeof(int), compareGreaterOf);
	l = p[len-1];
	f = p[0];
	if(l % 2 == 0)
	{
	    med = l/2;
	}
	else
	{
	    med = (f + l)/2;
	}
    }
    return med;
}
    

int main()
{
    char* good                  = NULL;
    char value[16]		= {0};
    char filename[BUFSIZ]	= {0};
    int* p			= NULL;
    size_t l			= 0;
    int i			= 0;
    FILE* pfIn			= NULL;

    printf("Enter the filename from which to read scores: ");
    fgets(filename, sizeof(filename), stdin);
    filename[strlen(filename)-1] = '\0';

    pfIn = fopen(filename, "r");
    if(pfIn == NULL)
    {
	printf("Could not open file: %s\nExiting.", filename);
	exit(-1);
    }

    do
    {
        i = 0;
	good = fgets(value, sizeof(value), pfIn);
	if(!good)
	{
	    break;
	}
	value[strlen(value)-1] = '\0';
        i = atoi(value);
        if(errno != EINVAL && errno != ERANGE)
        {
            p = (int*)realloc(p, (sizeof(int) * (l + 1)));
            if(p != NULL)
            {
                p[l++] = i;
            }
        }
        if(feof(pfIn))
	{
	    break;
	}
    } while(TRUE);

    printf("Average score of %d scores is %d\n", (int)l, mean(p, l));
    printf("The median of %d scores is %d\n", (int)l, median(p, l));
    
    if(p != NULL)
    {
        free(p);
    }
    return 0;
}


Output:

Code:
bash-3.2$ ./mmm Enter the filename from which to read scores: /tmp/scores.txt Average score of 50 scores is 73 The median of 50 scores is 49

scores.txt:

Code:
80 99 73 0 89 88 80 77 55 87 85 34 58 80 76 84 79 92 98 85 64 77 56 78 81 55 67 99 80 77 78 79 67 89 93 94 56 80 99 73 89 88 80 77 87 0 55 32 16 85

Remember to "mix up" the scores so that you do not always have an even number at the end for your median tests (or a zero at the beginning, too)!

Note that I didn't do the mode and histogram parts.


MxB
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
c programming : project idea ??? batrsau C++ Forum 2 09-Jun-2005 05:55
Community Project Proposal dsmith Miscellaneous Programming Forum 71 19-Feb-2005 13:26
Need help on a project for C programming andrew410 C Programming Language 1 30-Oct-2004 08:01
Need help on simple Programming Project daNichex6 C Programming Language 1 03-Sep-2004 19:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:47.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.