Please help with arrays homework (in C)

I'm having trouble figuring out how to calculate the mode. We're not allowed to use pointers (except for qsort) nor linear linked lists nor dynamic variables, nor structures.

I've honestly tried my best at doing it myself

Here is what I have:

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
PROBLEM				One must write a C program which takes input scores and prints them 5 per line. 
					The program should then sort the scores in ascending order to be then printed out once more five per line.
					After being sorted, one should create a frequence distribution chart.
					The output percentage of passing and failing scores will need to be calculated 60 and above being passing.
					the mean will need to be calculated.
					the mode will need to be printed;
					finally, the median will need to be determined.

ANALYSIS
inputs:				data array
outputs:			terminal window
formuals:			
constraints:		stats.c
					print five scores per line
					round to nearest tenth
					each segment should be assigned its own function

DESIGN				* * 1 create inputs
					* 1.1 use an array
					1.1.1 create a constant of 30
					1.1.2 initialize array
					1.1.3 put scores into array elements
					* 1.2 print out original array
					1.2.1 print out 5 array elements
					1.2.2 newline character
					* * 2 qsort ascending
					* 2.1 qsort needs A function
					2.1.1 create function header for qsort
					2.1.2 write qsort function
					* 2.2 sort array
					* 2.3 print sorted array
					2.3.1 print out 5 array elements
					2.3.2 print newline character
					* * 3 create a frequency distribution chart
					* 3.1 find frequencies
					3.1.1 create function
					3.1.2 pass array values to function
					3.1.3 return frequency
					* 3.2 print chart
					* * 4 calculate percentage of passing and failing scores
					* 4.1 create function and header
					* 4.2 query array for passing scores
					4.2.1 find number of tests passed by incrementing a variable
					4.2.2 divide incrementation by total number of tests
					4.2.3 print percent of passed scores
					* * 5 calculate mean
					* 5.1 create function and header
					* 5.2 calculate mean
					5.2.1 add all the array elements together
					5.2.2 divide summation of array information by number of elements in array
					* * 6 calculate mode mode
					* 6.1 create function and header
					* 6.2 ???
					* * 7 calculate median
					* 7.1 create function and header
					* 7.2 if divisible by two
					7.2.1 divide maximum elements in array by two
					7.2.2 add the result as well as the result - 1 together
					7.2.3 divide that summation by 2
					7.2.4 print the median
					* 7.3 if not divisible by two
					7.3.1 deduct maximum elements in array by one
					7.3.2 divide maximum by two
					7.3.3 plug in the result to the array element and print

TEST				90, 85, 100, 50, 50, 85, 60, 70, 55, 55, 80, 95, 70, 60, 95
					80, 100, 75, 70, 95, 90, 90, 70, 95, 50, 65, 85, 95, 100, 65

IMPLEMENTATION		See Below
*/

#include <stdio.h>
#include <stdlib.h>

#define MAX 30
#define PASS 60

/****************************************************************************************************
 * function:		triage
 * parameters:		constant void pointer, constant void pointer
 * return:			integer
 * description:		qsort control function
*/
int triage(const void *, const void *);

/****************************************************************************************************
* function:			frequency
* parameters:		array, integer, integer
* return:			integer
* description:		returns frequency
*/
int frequency(int [], int, int);

/****************************************************************************************************
* function:			passmark
* parameters:		array, integer
* return:			void
* description:		calculates percent of scores
*/
void passmark(int [], int);

/****************************************************************************************************
* function:			mean
* parameters:		array, integer
* return:			void
* description:		calculates mean of scores
*/
void mean(int [], int);

/****************************************************************************************************
* function:			mode
* parameters:		array, integer
* return:			void
* description:		contains a printf informational for the mode of scores
*/
void mode(int [], int);

/****************************************************************************************************
* function:			median
* parameters:		array, integer
* return:			void
* description:		calculates median of scores
*/
void median(int [], int);

int main(void){
	int scores[MAX] = { 90, 85, 100, 50, 50, 85, 60, 70, 55, 55, 80, 95, 70, 60, 95, 80, 100, 75, 70, 95, 90, 90, 70, 95, 50, 65, 85, 95, 100, 65 };
	int freq = 0;
	int i;

	printf("Original Data:\n");
	for (i = 0; i < MAX; i += 5) {
		printf("%d\t %d\t %d\t %d\t %d\t %c", scores[i], scores[i + 1], scores[i + 2], scores[i + 3], scores[i + 4], '\n');
	}

	printf("\nSorted Data:\n");
	qsort(scores, 30, sizeof(int), triage);
	for (i = 0; i < MAX; i += 5) {
		printf("%d\t %d\t %d\t %d\t %d\t %c", scores[i], scores[i + 1], scores[i + 2], scores[i + 3], scores[i + 4], '\n');
	}

	printf("\nFrequency Distribution:\n");
	for (i = 0; i < MAX; i++){
		freq = frequency(scores, MAX, scores[i]);
		printf("Score[%d]\t Frequency:\t%d\n", scores[i], freq);
	}
	passmark(scores, MAX);
	mean(scores, MAX);
	mode(scores, MAX);
	median(scores, MAX);
}

int triage(const void *p, const void *q){
	if (*(int *)p < *(int *)q) {
		return -1;
	}
	return *(int *)p > *(int *)q;
}

int frequency(int scores[], int max, int query){
	int i;
	int freq = 0;
	for (i = 0; i < max; i++){
		if (scores[i] == query){
			freq++;
		}
	}
	return freq;
}

void passmark(int scores[], int max){
	int pass = 0;
	float percent;
	int i;
	for (i = 0; i < max; i++){
		if (scores[i] >= PASS){
			pass++;
		}
	}

	percent = (float)pass / (float)max;
	printf("\n\n%.2f percent scores have recieved a passing score\n\n", percent);
}

void mean(int scores[], int max){
	int i, summation = 0;
	float mean;
	for (i = 0; i < max; i++){
		summation += scores[i];
	}
	mean = (float)summation / (float)max;
	printf("\n\nThe mean scores is %.2f\n\n", mean);
}

void mode(int scores[], int max){
	int alamode[30];
	int i;
	printf("The mode is 95");
}

void median(int scores[], int max){
	int i;
	float median;
	if (max % 2 == 0){
		max /= 2;
		median = (float)scores[max] + (float)scores[max - 1];
		median /= 2.0;
		printf("\n\nYour median:\t%.2f\n\n", median);
	}
	else{
		max -= 1;
		max /= 2;
		printf("\n\nYour median:\t%d\n", scores[max]);
	}
}
Last edited on
you could have some loops and say
1
2
3
4
5
6
7
8
9
10
int repeat[30];
for(int b = 0;b<30;b++)
    repeat[b] = 0;
for(int a = 0; a < 29; a++)
{
    if(alamode[a] = alamode[a+1])
    {
          repeat[a] += 1;
    }
}

and check what repeat value is the greatest.
The Mode is the most frequent number.

I'm sure there's a more efficient method, but the way I'd do it on paper is to sort the numbers and count the number in each repeating group.

To do that in your function, you need to copy the array and work with the copy.
Topic archived. No new replies allowed.