Working with arrays

Hello everyone, I am having trouble with this assignment for school.

I am able to come up with the random numbers in that range and find the average, but that is where I get stuck. I have done a lot of research and still cant figure it out. Here is the code I have so far. Any help would be greatly appreciated.

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
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

void main()
{
	float sum=0;
	float avg=0;
	const int size = 200;

	int array[size];
	srand((unsigned)time(0));

	for(int a=0; a<size; a++)
	{
		array[a]=(rand()%1000)+1;

		cout<<array[a]<<endl;

		
		sum=sum+array[a];
		avg=sum/200;	
	}

	cout<<"The average is: "<<avg<<endl;
	


}
Last edited on
A function like this, called with sort_increasing(array); will sort the array in increasing order and a small modification will allow for a void sort_decreasing(int arr[]) function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sort_increasing(int arr[])
{
	int temp;
	for(int x = 0; x < 200; x++) 
	{
		for(int y = 0; y < 199; y++) 
		{
			if(arr[y] > arr[y+1]) 
			{
				temp = arr[y+1];
				arr[y+1] = arr[y];
				arr[y] = temp;
			}
		}
	}		
}
Thanks for replying. I appreciate it.
I still don't understand how this is done. Also does my array have a name? Im a complete beginner and really have little idea what im doing. when I enter your code into mine it gives an error that arr is undefined. thank you in advanced for any further help.
The sort increasing() function needs to be declared (prototyped) before main(), and the actual function comes after main(). sort increasing() is called from within main() with array is passed as a parameter. Here is some code:

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
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

// prototype for the sort_increasing function
void sort_increasing(int arr[]); 

int main()
{
	float sum=0;
	float avg=0;
	const int size = 200;

	int array[size];
	srand((unsigned)time(0));

	for(int a=0; a<size; a++)
	{
		array[a]=(rand()%1000)+1;
		sum=sum+array[a];
		avg=sum/200;	
	}
	
	// print the array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	
	cout << endl << "The average is: " <<avg << endl;
	
	// call sort_increasing function to sort the array
	sort_increasing(array);
	
	// print the sorted array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	
	return 0;
}

// sort_increasing function
void sort_increasing(int arr[])
{
	int temp;
	for(int x = 0; x < 200; x++) 
	{
		for(int y = 0; y < 199; y++) 
		{
			if(arr[y] > arr[y+1]) 
			{
				temp = arr[y+1];
				arr[y+1] = arr[y];
				arr[y] = temp;
			}
		}
	}		
}


Another function could sort the array so the numbers are descending. And functions can be written to find the mode and median, have go and post the code and I'll help if I can. Good luck
Last edited on
Thank for the help. I was able to get the median but still cant get the mode. Also to sort the array in decreasing order do I just do the opposite of the sort increasing function. here is what I have so far, didn't change much just added the part to find the median

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
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

// prototype for the sort_increasing function
void sort_increasing(int arr[]); 

int main()
{
	float sum=0;
	float avg=0;
	const int size = 200;
	
	int array[size];
	srand((unsigned)time(0));

	for(int a=0; a<size; a++)
	{
		array[a]=(rand()%1000)+1;
		sum=sum+array[a];
		avg=sum/200;	
	
	}
	
	// print the array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	
	cout << endl << "The average is: " <<avg << endl;
	
	double median = (array[200/2] + array[(200/2)-1])/2; 
	cout <<"The median is: " <<median<<endl;

	
	// call sort_increasing function to sort the array
	sort_increasing(array);
	
	// print the sorted array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	}

// sort_increasing function
void sort_increasing(int arr[])
{
	int temp;
	for(int x = 0; x < 200; x++) 
	{
		for(int y = 0; y < 199; y++) 
		{
			if(arr[y] > arr[y+1]) 
			{
				temp = arr[y+1];
				arr[y+1] = arr[y];
				arr[y] = temp;
			}
		}
	}		
}
Last edited on
The sort_decreasing() function os easy, it's the same as the sort_increasing() with one modification: if(arr[y] > arr[y+1]) --> if(arr[y] < arr[y+1])

The mode function is more tricky, most of the time your array will be bimodal and there may be many modes, so I'm not sure if you are supposed to give them all as your answer in this problem. Try the get_mode function I wrote and see that most of the time there are several modes...

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
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

// prototype for the sort_increasing function
void sort_increasing(int arr[]);
void sort_decreasing(int arr[]); 
int get_mode(int arr[]);

int main()
{
	float sum=0;
	float avg=0;
	const int size = 200;
	
	int array[size];
	srand((unsigned)time(0));

	for(int a=0; a<size; a++)
	{
		array[a]=(rand()%1000)+1;
		sum=sum+array[a];
		avg=sum/200;	
	
	}
	
	// print the array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	
	cout << endl << "The average is: " <<avg << endl;
	
	double median = (array[200/2] + array[(200/2)-1])/2; 
	cout <<"The median is: " <<median<<endl<<endl;
	
	// call sort_decreasing function to sort the array
	sort_decreasing(array);
	// print the sorted array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	cout << endl << endl;
	
	// call sort_increasing function to sort the array
	sort_increasing(array);
	
	// print the sorted array
	for(int a=0; a<size; a++)
	{
		cout << array[a] << " ";
	}
	cout << endl << endl;
	
	cout << endl << "The mode is " << get_mode(array);
	
	return 0;
}

// sort_increasing function
void sort_increasing(int arr[])
{
	int temp;
	for(int x = 0; x < 200; x++) 
	{
		for(int y = 0; y < 199; y++) 
		{
			if(arr[y] > arr[y+1]) 
			{
				temp = arr[y+1];
				arr[y+1] = arr[y];
				arr[y] = temp;
			}
		}
	}		
}

// sort_decreasing function
void sort_decreasing(int arr[])
{
	int temp;
	for(int x = 0; x < 200; x++) 
	{
		for(int y = 0; y < 199; y++) 
		{
			if(arr[y] < arr[y+1]) 
			{
				temp = arr[y+1];
				arr[y+1] = arr[y];
				arr[y] = temp;
			}
		}
	}		
}

int get_mode(int arr[])
{
	int highest_counter = 0;
	int mode;
	for(int x = 0; x < 200; x++)
	{
		int counter = 0;
		for(int y = 0; y < 200; y++)
		{
			if(arr[x] == arr[y]) counter++;
		}
		if(counter > highest_counter)
		{
			highest_counter = counter;
			mode = arr[x];
			cout << arr[x] << "(" << counter << ") ";
		}
		else if(counter == highest_counter)
		{
			cout << arr[x] << "(" << counter << ") ";
		}
	}
	return mode;
}
Thank you for all your help. I see what you mean about the modes. it looks correct to me. its giving me a mode and I guess that's all I can really ask for. Thanks again for helping me.
Topic archived. No new replies allowed.