Errors

I know that what im going to post is a massive lines of codes but i hope you guys can help me to solve the problems...well, basically, i was asked to find the mode, median, mean and range of a set of numbers. The problem is, it seems that the codes somehow malfunction and i've got no idea how to debug it...i hope you guys can help me...

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
#include <fstream.h>

void displayarr (int* arr, int n);
float calculateMean (int* arr,int n);
void sort (int* arr, int n);
float calculatemedian(int* arr, int n);
float calculatemode (int* arr, int n);

void main()
{
   clrscr();
   unsigned int n;
   int* arr;
   //ifstream fin;
   //ofstream fout;

   //fout.open("assignment.txt");
   cout << "Enter the size of the array: ";
   //fin.open("assignment.txt");
   cin >> n;

   arr = new int[n];
   srand(time(0));

    int i=0;
  do
  {

   arr[i] = rand() % 9+4;
   cout <<  "The random numbers generated are: " << arr[i] <<endl;
   i++;
  }
   while (i<n);






   /*for (int i=0;i<n;i++)
      arr[i] = rand() % 9 + 4;*/

   cout << "The numbers in the array is: \n";
   displayarr (arr, n);

   float mean = calculateMean (arr, n);
   sort (arr, n);

   cout << "The mean is: " << mean << endl;

   float median = calculatemedian (arr, n);
   cout << "The median is: " << median <<endl;

   float mode = calculatemode (arr, n);
   cout << "The mode is: " << mode <<endl;


   getch();
   //fout.close();
   //fin.close();
}


float calculateMean (int *arr, int n)
{
   float total = 0;
   for (int i=0;i<n;i++)
      total = total+arr[i];
   
   return (total/n);
}


void sort (int* arr, int n)
{
   for (int i =0; i<n-1; i++)
   {    
       for (int j=i+1; j<n; j++)
      {
         if (arr[j] <arr[i])
         {
             int temp = arr[j];
             arr [j] = arr [i];
             arr [i] = temp;
          }
      }
   }
}     
   
      

void displayarr (int* arr, int n)
{
   for (int i=0; i<n; i++)
   cout << arr[i] << "\t";
   cout << endl;
}

float calculatemedian(int* arr, int n)
{
   float median;
   if (n%2 == 0)
   {
      median = (float) (arr[n/2] + arr[n/2-1])/2;
   }
   else
   {
      median = arr[(n-)1/2];
   }

return (median);
}


float calculatemode (int* arr, int n)
{

   int freqtable[9] = {0,0,0,0,0,0,0,0,0};
   for(int i=0; i<n; i++)
   {
     ++(freqtable [arr[i]-4]);
   }

   int indexlargest=0;
   int largest = arr[0];

   for (int a=1; a<n; a++)
   {
      if (arr[a] > largest)
      {
	 indexlargest =i;
	 largest =arr[a];
      }
   }
   return (indexlargest+4);
}




i know the codes are terribly long but please...i really need help...desperate T_T

I need to find the range of data too but i've got no idea how to find the last number in an array...
I could very well just copy the code and test it (if it wasn't for the fact that you're using non-standard and deprecated libraries), but I'm not going to.
Instead, you're going to learn to ask a proper question and post the expected output, the actual output, and your theories about what could be going wrong.
oh im sorry....im very new in c++ and in the same time, in this forum...so, what exactly should i do??? I know my codes are messy but well, i've got no idea how could i actually urm...arrange them...
Output, man. Output.
When you run it, what happens?
Say what you want it to do, what it does, and what the error message is. Give more details on the problem your having don't just say there's an error you gotta be more specific.
oh! no Error....just some bugs stuff...

it's something like...



The mean is:7

The median is: 13321

The mode is: 7


Which is all wrong....
Line 108: I think this: (arr[n/2] + arr[n/2-1])/2 should be like this: (arr[n/2] + arr[n/2+1])/2
Line 112: What is this supposed to mean? (n-)1/2
Lines 128-139: If you call this function while the array is sorted, then you can easily determine the largest element: it'll be on the tail.
okay...thanks for finding the error for me on line 112... it's suppose to be (n-1)/2 and also for line 108...

and for lines 128 - 139,
yes it's sorting...but i do not know how to find the range =.=" sorry for this noob question...but i really got no idea how to start...
Last edited on
Well, if the array is sorted from least to biggest...it should be too hard to find the range. Just look at the first/last elements.
the problem is, i don't really know how. It's because im quite fresh in all this C++ and on the other hand, the size of the array is unknown. It is designated to be set by users. That is the problem why i've got no idea on how to find the last element.



Oh okay...i've just got a clue...from the above codes and since "n" is the number of arrays that should be keyed in by the user, so could it be that i call the sort function and i do something like,

arr[0] - arr[(n-1)]

since the last element in an array is always the total number of elements - 1...
Last edited on
Well...since you tell it the array is of size 'n' (they input that value) then the maximum value is (n-1).
THAAAAAAAAAAAAAAAAAAAAAAAANK YOU GUYS!!!!!!!!!!!!!!!!!!!! THANK YOU VERY MUCH!!!!! YOU guys RAWKS!
Topic archived. No new replies allowed.