How to make program more effective

One of my homeworks required me to create 30 random numbers from 0 - 9 and count how many times each number appeared.

I made a long if-else statement to check each number condition (as can be seen on the code). I'm just wondering if there is a much faster way to do this?

instead of if ( x = 0)
else if (x = 1) ...



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
/* ==========================================================================================================
File: Number3.cpp
==========================================================================================================
Programmer: Alexandros Panayi
Date: 10/20/2014
Class: CPSC 121 ("Programming Concepts")
Time: TR 2:30 PM
Instructor: Dr. Ray Ahmadnia
Project: Project No. 7

Description:
Use members of <cstring> to manipulate string data


==========================================================================================================
*/

#include <ctime>
#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

void Display(int *x);
void DisplayData(int *x);

struct NUM
{
	int n;
	int freq;
};

NUM ALL[10];

int main()
{
	srand(time(0));

	int random[30];

	for (int i = 0; i < 30; i++) //assign random numbers 0-9 to n
	{
		random[i] = rand() % 10;
	}

	Display(random); // show the random numbers assigned

	cout << endl;

	for (int i = 0; i < 10; i++) //set all numbers to integers from 0 - 9
	{
		ALL[i].n = i;
	}

	for (int i = 0; i < 30; i++)
	{
		if (random[i] == 0)
		{
			ALL[0].freq++;
		}
		else if (random[i] == 1)
		{
			ALL[1].freq++;
		}
		else if (random[i] == 2)
		{
			ALL[2].freq++;
		}
		else if (random[i] == 3)
		{
			ALL[3].freq++;
		}
		else if (random[i] == 4)
		{
			ALL[4].freq++;
		}
		else if (random[i] == 5)
		{
			ALL[5].freq++;
		}
		else if (random[i] == 6)
		{
			ALL[6].freq++;
		}
		else if (random[i] == 7)
		{
			ALL[7].freq++;
		}
		else if (random[i] == 8)
		{
			ALL[8].freq++;
		}
		else if (random[i] == 9)
		{
			ALL[9].freq++;
		}
	}

	DisplayData(random);

	system("PAUSE");
	return 0;

}

void Display(int *x)
{
	for (int i = 0; i < 30; i++)
	{
		cout << i[x] << " ";
	}
}

void DisplayData(int *x)
{
	cout << "\tNUMBER \tFREQUENCY" << endl;
	for (int i = 0; i < 30; i++)
	{
		cout << char(196);
	}
	cout << endl;
	cout << setfill(' ');
	cout << fixed << showpoint << setprecision(2);
	for (int i = 0; i < 10; i++)
	{
		cout << left << setw(10) << ALL[i].n << right << setw(10) << ALL[i].freq;
		cout << endl;
	}
}
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
	for (int i = 0; i < 30; i++)
	{
		if (random[i] == 0)
		{
			ALL[0].freq++;
		}
		else if (random[i] == 1)
		{
			ALL[1].freq++;
		}
		else if (random[i] == 2)
		{
			ALL[2].freq++;
		}
		else if (random[i] == 3)
		{
			ALL[3].freq++;
		}
		else if (random[i] == 4)
		{
			ALL[4].freq++;
		}
		else if (random[i] == 5)
		{
			ALL[5].freq++;
		}
		else if (random[i] == 6)
		{
			ALL[6].freq++;
		}
		else if (random[i] == 7)
		{
			ALL[7].freq++;
		}
		else if (random[i] == 8)
		{
			ALL[8].freq++;
		}
		else if (random[i] == 9)
		{
			ALL[9].freq++;
		}
	}


all of that can be accomplished with just:
1
2
for (int i = 0; i < 30; i++)
	ALL[random[i]].freq++;
Last edited on
Well, you could just do...

ALL[random[i]].freq++;
What is the statement, ALL[random[i]].freq++; saying?

I know it says increase the increment by 1 of freq associated with the random number, but I don't see how that works in the statement.

If I'm trying to find the max and min frequency, in other words, which number appears the most and how many times, can I say;

ALL[max[i].n] and
ALL[min[i].n]

to say which number appears the most and the least?
well look at some part of what you're already doing:
1
2
3
4
		else if (random[i] == 8)
		{
			ALL[8].freq++;
		}

ALL[8].freq++; is the same as ALL[ random[i] ].freq++;

it just uses the value of a specific element in random, as the index into ALL.
Ah I see, that makes it very clear thank you.

I need to show which number appeared the most and how many times, however when I compile my program, the max number which appears and the min number doesn't show. What's wrong with my condition statement?

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

using namespace std;

void Display(int *x);
void DisplayData(int *x);
void FindMaxMin(int *x);

struct NUM
{
	int n;
	int freq;
};

NUM ALL[10];

int main()
{
	srand(time(0));

	int random[30];

	for (int i = 0; i < 30; i++) //assign random numbers 0-9 to n
	{
		random[i] = rand() % 10;
	}

	Display(random); // show the random numbers assigned

	cout << endl;

	for (int i = 0; i < 10; i++) //set all numbers to integers from 0 - 9
	{
		ALL[i].n = i;
	}

	for (int i = 0; i < 30; i++)
	{
		
		ALL[random[i]].freq++;
	}

	DisplayData(random); //display the random numbers and their frequency 

	FindMaxMin(random);

	system("PAUSE");
	return 0;

}

void Display(int *x)
{
	for (int i = 0; i < 30; i++)
	{
		cout << i[x] << " ";
	}
}

void DisplayData(int *x)
{
	cout << "\tNUMBER \tFREQUENCY" << endl;
	for (int i = 0; i < 30; i++)
	{
		cout << char(196);
	}
	cout << endl;
	cout << setfill(' ');
	cout << fixed << showpoint << setprecision(2);
	for (int i = 0; i < 10; i++)
	{
		cout << left << setw(10) << ALL[i].n << right << setw(10) << ALL[i].freq;
		cout << endl;
	}
}

void FindMaxMin(int *x)
{
	int max = 0;
	int min = 9;

	for (int i = 0; i < 10; i++)
	{
		if (ALL[i].freq > max)
		{
			max = ALL[i].freq;
			if (x[i] == max)
			{
				cout << "Number(s) with the largest frequency of " << max << " is/are: " << x[i] << endl;
			}
		}
		if (ALL[i].freq < min)
		{
			min = ALL[i].freq;
			if (x[i] == min)
			{
				cout << "Number(s) with the lowest frequency of " << min << " is/are: " << x[i] << endl;
			}
		}
	}
}
Topic archived. No new replies allowed.