mean, median and mode.

May 22, 2012 at 12:29pm
hello, i am a newbie and am really stuck in this. i am to prompt a user to enter a number and print out what user input, if the user did not input 5 then it should prompt the user till ten iteration. then display the mean, median and mode for the user. here is my code so far and i cant go any far more than this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// ozone.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include <iostream>

using namespace std;


int main()
{

		int user;
		int x = 0;
		float mean;
		float median;
		int mode;
		int range;
	do {
		cout << "please enter any number other than 5: \n";
			
			cin >> user;
			 x++;
			 for (int i = 0; i < 10; i++);
			 cout << "you enter " <<  user << endl;
			 
			
	} while (user != 5 && x < 10); {

		if (user == 5) {
			 	cout << "Hey! you were'nt suppose to enter that: .\n";
				
				user = user + user;
				mean = user / x + 1;
				cout << "your mean is: " << mean << endl; // this is where i start having the problem start //
				
					median = user / x + 1;
					if ( user % 2 == 0 || user % 2 != 0 )
				cout << " your median is : " << median << endl;

				
				
			}
		else {
			
			cout << "wow you are more patient than me u win \n"; 

			user = user + user;
				mean = user / x + 1;
				cout << "your mean is: " << mean << endl;
				
				median = user / x + 1;
					if ( user % 2 == 0 || user % 2 != 0 )
				cout << " your median is : " << median << endl;

					 


		
		}
	}

	system ("pause");


return 0;
}


any help rendered will be much appreciated
Thanks
May 22, 2012 at 12:54pm
You want to find the mean/median/mode of a single number?
May 22, 2012 at 1:09pm
naaaa i have iterate the loops to ten time if the user did not still get it then he should end it. or if the user enter 5 before the loops end then it should display the mean, median and mode the user input
May 22, 2012 at 5:23pm
anybody to help pls .....
May 22, 2012 at 6:47pm
you should order the array of numbers before you calculate the median or mode. in C++ you can use 'qsort()' function in 'stdlib' for the pourpose:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cstdlib>
#include <iostream>

using namespace std;

//define the 'comparison function' used by qsort() for the sort
int CompareValues(const void *a, const void *b){
    const int *val1 = (const int *)a;
    const int *val2 = (const int *)b;
    if(*val1 < *val2) return -1;
    else if (*val1 == *val2) return 0;
    else return 1;
}

//define the CalcMean() , CalcMedian(), CalcMode() and CalcRange(). CalcMedian() and CalcMode() work on a pre-sorted array!
float CalcMean(const int values[] , int num){
    if (num<=0) return 0;
    int count=0;
    for (int i=0;i<num;i++) count += values[i];
    return (float)count / num;
}

float CalcMedian(const int values[],int num){
    if(num<=0) return 0;
    if(num%2) return (float)values[(num+1)/2];
    else{
        int pos = num/2;
        return (float)(values[pos] + values[pos+1]) / 2;
    }
}

int CalcMode(const int values[],int num){
    if(num<=0)return 0;
    int max_count=0 , pos=0;max_nums=0;
    int count=1;
    for(int i=1;i<num;i++){
        if(values[i] != values[pos]){
            if(count > max_count){
                max_count=count;
                max_nums=0;
            }else if (count == max_count) max_nums++;
            pos= i;
            count=0;
        }else count++;
    }
    if(max_nums) return 0;
    else return values[pos];
}

int CalcRange(const int values[] , int num){
    if(num<=0) return -1;
    int max,min;
    max=min=values[0];
    for(int i=1;i<num;i++){
        if(values[i]>max)max=values[i];
        else if (values[i]<min) min=values[i];
    }
    return max-min;
}

#define NUM 10

int main(){
    int numbers[NUM];
    int count=0, val;
    do{
        cout << "enter a number ("<< count + 1<<" of "<<NUM<<" - digit 5 to stop input) : ";
        cin >> val;
        if(val == 5){
            cout<<"\nInput stopped! Digited numbers: "<<count<<".\n";
            break;
        }
        values[count] = val;
        count++;
    }while(count<10);
   
    int mean=CalcMean(numbers,count);
    int range=CalcRange(numbers,count);
    qsort( (void*)numbers, count, sizeof(int),CompareValues);
    float mode=CalcMode(numbers,count);
    float median=CalcMedian(numbers,count);
    
    cout<<"\n  Mean: "<<mean;
    cout<<"\n  Median: "<<median;
    cout<<"\n  Mode: "<<mode;
    cout<<"\n  Range: "<<range;
    cout<<endl;

    return 0;
}
Last edited on May 22, 2012 at 6:51pm
May 22, 2012 at 7:55pm
wow @Vins3Xtreme you are the bomb. thank you so much........
Topic archived. No new replies allowed.