5 Replies - 5366 Views - Last Post: 19 May 2009 - 01:26 AM Rate Topic: -----

#1 derickwong87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 09

mean median and mode

Posted 18 May 2009 - 10:49 PM

this is my code for homework. im supposed to find the mean, median, and mode for the following 2 arrays. i have median down and some idea for mean, but im mainly thinking of mode. any advice is greatly appreciated. thanks.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <time.h>

using namespace std;

void sortArray(int [], int);
void ShowArray(int [], int);
void selectionSort(int [], int);
void showArray(int [], int);
void mean(int[]);

int main()
{

// Arrays of unsorted values
   const int SIZE = 100;
   const int SIZE2 = 100;
   int a[SIZE];
   int b[SIZE2];
   srand(123);
	for (int i=0; i<SIZE; i++)
	{
		a[i]=rand() % 100 +1;
	}
 
	for (int i=0; i<SIZE2; i++)
	{
		b[i]=rand() % 100 +1;
	}
//	int swapcount;

// Displays unsorted values in array a
cout << "the unsorted values in array sorted by bubble: \n";
showArray(a, SIZE);
cout << "\n";

// Displays unsorted values in array b
cout << "the unsorted values in array sorted by selection: \n";
showArray(b, SIZE2);
cout << "\n";

// Displays sorted values in array a
cout << "The sorted values in array sorted by bubble: \n";
sortArray(a, SIZE);
cout << "\n";
cout << "\n";

// Displays sorted values in array b
cout << "The sorted values in array selection: \n";
selectionSort(b, SIZE2);
cout << "\n";
system("pause");
return 0;
}

double mean(int array[], int size)
{
	int i;
	int total = 0;
	for (i=0; i < size; i++) {
		total = total + array[i];
	}
	return (double) total / size;
}
//************************************...
// Definition of function sortArray
// The function performs an ascending order bubble sort on
// array a size is the number of elements in the array.
//************************************...

void sortArray( int array[], int size)
{
	 bool swap;
	 int temp;
	 int swapcount =0;
	 int compCount = 0;

	 do
	 {
	 swap = false;
   
	 for (int count = 0; count < (size - 1); count++, compCount++)
	 {
	   
	 if (array[count] > array[count + 1])
	 {
	 temp = array[count];
	 array[count] = array[count + 1];
	 array[count + 1] = temp;
	 swapcount = swapcount + 1;
	 swap = true;
	 }
	 }
	 } while (swap);
		 /* mean */
	 		 int i,mean;
		 int total = 0;
	for (i=0; i < size; i++) {
		total = total + array[i];
	} 
	mean = total / size;
	 showArray(array, size);
	 cout << "CompCounts: " << compCount << endl;
	 cout << "SwapCounts: " << swapcount << endl;
	 cout << "Median: " << array[48]<< endl;
	  cout << "Mean: " << mean <<endl;
}

void showArray(int array[], int size)
{
int count=0;
for ( int i = 0; i < size/10; i++)
{
	for( count; count < 10*i; count++)
		 cout << array[count] << " ";
	cout << endl;
}
}

//************************************...
// Definition of function sortArray
// The function performs an ascending order selection sort on
// array b size is the number of elements in the array.
//************************************...

void selectionSort(int array[], int size)
{
	 int startScan, minIndex, minValue;
	 int swapCounter =0;
	 int compCount =0;

	 for (startScan = 0; startScan < (size - 1); startScan++)
	 {
		 minIndex = startScan;
		 minValue = array[startScan];
		 for(int index = startScan + 1; index < size; index++, compCount++)
		 {
		 if (array[index] < minValue)
		 {
			minValue = array[index];
			minIndex = index;
			swapCounter++;
			}
		 }
		 array[minIndex] = array[startScan];
		 array[startScan] = minValue;
		 }
	/* mean */
	 		 int i,mean;
		 int total = 0;
	for (i=0; i < size; i++) {
		total = total + array[i];
	} 
	mean = total / size;
	 showArray(array, size);
	 cout << "CompCounts: " << compCount << endl;
	 cout << "SwapCounts: " << swapCounter << endl;
	 cout << "Median: " << array[48] <<endl;
	 cout << "Mean: " << mean <<endl;
}




This post has been edited by derickwong87: 18 May 2009 - 11:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: mean median and mode

#2 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: mean median and mode

Posted 18 May 2009 - 10:54 PM

Do you have any questions? Errors? Is it not doing what you want it to do?
Was This Post Helpful? 0
  • +
  • -

#3 derickwong87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 09

Re: mean median and mode

Posted 18 May 2009 - 11:11 PM

View Postapw5020, on 18 May, 2009 - 09:54 PM, said:

Do you have any questions? Errors? Is it not doing what you want it to do?


i cant think of a way of finding the mode of the arrays. just updated. i have the mode (just displaying the middle of the array) and mean down.
Was This Post Helpful? 0
  • +
  • -

#4 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: mean median and mode

Posted 18 May 2009 - 11:33 PM

I found this snippet of code on another website... http://www.daniweb.c...ead161948.html#
int currvalue = a[0]; // start from the 1st element
int counter = 1;
int maxcounter = 1;
int modevalue = a[0];
for (int i = 1; i < n; ++i) {
	if (a[i] == currvalue)
		++counter;
	else { // next value started...
		if (counter > maxcounter) { // new mode candidate
			maxcounter = counter;
			modevalue = value;
		}
		currvalue = a[i]; // ready to count next values
		counter = 1;
	}
}
// Check last value, copy code...
if (counter > maxcounter) {
	maxcounter = counter;
	modevalue = value;
}



Not sure if it is what you are looking for, however.

This post has been edited by apw5020: 18 May 2009 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: mean median and mode

Posted 19 May 2009 - 12:39 AM

Hi,

Follow the code that was given to you from daniweb, that seems to make sense. Have a look at this site as well, http://www.mathsisfun.com/mode.html, it give the following information.

Quote

The mode is simply the number which appears most often.

To find the mode or modal value requires you to put the numbers you are given in order.

Look at these numbers:


3, 7, 5, 13, 20, 23, 39, 23, 40, 23, 14, 12, 56, 23, 29

In order these numbers are:

3, 5, 7, 12, 13, 14, 20, 23, 23, 23, 23, 29, 39, 40, 56

This makes it easy to see which numbers appear the most.

In this case the mode or modal value is 23.


Perhaps using this would give you some indication as how to go about creating a method for the modal value.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#6 derickwong87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 09

Re: mean median and mode

Posted 19 May 2009 - 01:26 AM

apw5020 i finally got it. thanks a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1