Mean, Median, Mode

I have been given some source code by a friend for a program that calculates the mean, median and the mode. I want to make the program read from a txt file instead of the keyboard. This is what i have so far( i know it is wrong) :
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
92
93
94
95
96
97
#define SIZE 500
#include <iostream.h>
#include <fstream.h>
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
int main()
{
int i,n,choice;
float array[SIZE],mean,median,mode;

{
	int itotal = 0;
	int inumbercount = 0;
	int inumbers[500];
ifstream fin ("infile.txt");
 {   while (!fin.eof())
	{   fin >> inumbers[inumbercount];
	    inumbercount++;
	}
	fin.close();
    }
itotal = inumbers/inumbercount;
}

for(i=0;i<n;i++)
cin >> array[i];


// use a while not eof to read from text file and count how many numbers


do
{
cout << "\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n4.Exit";
cin >> choice;
switch(choice)
{
case 1: mean=mean_function(array,n);
cout << "\n\tMean = %f\n",mean;
break;
case 2: median=median_function(array,n);
cout << "\n\tMedian = %f\n",median;
break;
case 3: mode=mode_function(array,n);
cout << "\n\tMode = %f\n",mode;
break;
case 4: break;
default:cout << "Wrong Option";
break;
}
}while(choice!=4);
return 0;
}

/***************************************************************
Function Name : mean_function
****************************************************************/
float mean_function(float array[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+array[i];
return (sum/n);
}

/***************************************************************
Function Name : median_function
Return Type : Float
****************************************************************/

float median_function(float a[],int n)
{
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}

float mode_function(float a[],int n)
{
return (3*median_function(a,n)-2*mean_function(a,n));
}


I have been trying to figure it out for quite as while now and it is annoying to sday the least.
Thanks for any help.
hey here's a trick.. in the cmd type program.exe<input.txt
where program.exe is the name of the program and input.txt has the input in each line..

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main() {
    int n1, n2, sum;
    cin >> n1;
    cin >> n2;
    sum = n1+n2;
    cout << "The sum is: "  << sum << endl;
    return 0;
}
[input.txt]
6
11



output is:
The sum is: 17


no need to modify the code..
Last edited on
It wouldn't be a big difference with the modified code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
using namespace std;
int main() {
    int n1, n2, sum;
    ifstream fin ( "input.txt" );
    fin >> n1;
    fin >> n2;
    sum = n1+n2;
    cout << "The sum is: "  << sum << endl;
    return 0;
}
or
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
using namespace std;
int main() {
    int n1, n2, sum;
    ifstream fin ( "input.txt" );
    cin.rdbuf ( fin.rdbuf() );
    cin >> n1;
    cin >> n2;
    sum = n1+n2;
    cout << "The sum is: "  << sum << endl;
    return 0;
}


@OP
why do you have all those braces on lines 12-24?
Thanks for the replies, they have helped alot.
I am now coming across different errors when trying to compile it.
Again, im sure it is a simple resolution, I just dont know it yet.

My problem now is that I am getting 3 errors :
1
2
3
error C2109: subscript requires array or pointer type
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
error C2109: subscript requires array or pointer type

My source looks like this now;
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
92
93
94
#define SIZE 500
#include <iostream.h>
#include <fstream.h>
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
int main()
{
int i,n,choice,isum; //i is the counter

{
ifstream fin ( "input.txt" );
    if (fin)
	{ while (!fin.eof()) 
	{ fin >> n[i];
	isum+=n[i];
	i++; 
	}
fin.close();
}
else
cout << "Could not open file\n";
    return 0;
}

float array[SIZE],mean,median,mode;

// use a while not eof to read from text file and count how many numbers


do
{
cout << "\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n4.Exit";
cin >> choice;
switch(choice)
{
case 1: mean=mean_function(array,n);
cout << "\n\tMean = %f\n",mean;
break;
case 2: median=median_function(array,n);
cout << "\n\tMedian = %f\n",median;
break;
case 3: mode=mode_function(array,n);
cout << "\n\tMode = %f\n",mode;
break;
case 4: break;
default:cout << "Wrong Option";
break;
}
}while(choice!=4);
}

/***************************************************************
Function Name : mean_function
****************************************************************/
float mean_function(float array[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+array[i];
return (sum/n);
}

/***************************************************************
Function Name : median_function
Return Type : Float
****************************************************************/

float median_function(float a[],int n)
{
float temp;
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}

float mode_function(float a[],int n)
{
return (3*median_function(a,n)-2*mean_function(a,n));
}


*edit*
Also, I got rid of some of the braces, i went a bit mad with them I believe..
Last edited on
You declared 'n' as an int but you are using it as an array ( lines 15/16 )
bazzy +1 , also you should get rid of the scope on lines 11 and 51 unless you need to redeclare those variables as other types.
also int main() does not have an end brace or return type.(the return type is in the wrong position)

This is why proper and consistent indentation is important.

This is how it should probably look:
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
#define SIZE 500

#include <iostream>
#include <fstream>

using namespace std;

float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);

int main()
{
    int i,n,choice,isum; // FIXME

    ifstream fin ( "input.txt" );
    if (fin)
    { 
        while (!fin.eof()) 
	{ 
            fin >> n[i];  // FIXME
            isum+=n[i];  
            i++; 
	}
        fin.close();
    }
    else
        cout << "Could not open file\n";
    
    // return 0;
// }
// you close off main here but do not start a new function, so you need to either continue main
// or start a new function.

    float array[SIZE],mean,median,mode;

    do
    {
        cout << "\n\tEnter Choice\n\t1.Mean\n\t2.Median\n\t3.Mode\n4.Exit";
        cin >> choice;
        switch(choice)
        {
            case 1: mean=mean_function(array,n);
                cout << "\n\tMean = %f\n",mean;
                break;
            case 2: median=median_function(array,n);
                cout << "\n\tMedian = %f\n",median;
                break;
            case 3: mode=mode_function(array,n);
                cout << "\n\tMode = %f\n",mode;
                break;
            case 4: break;
            default: cout << "Wrong Option";
                break;
       }
    }while(choice!=4);

    return 0;  // we should return here...
}

Last edited on
Topic archived. No new replies allowed.