idiomatic for loops for idiots

This may or may not be idiomatic seeming but I am confused right now with how this works. Please explain as best you can. I did not make this code I just found it in search of different ways to solve a problem and now I want to understand why this code outputs the number 3.
This is for sorting pairs.

Given an input of n being 9, 9 values are then input as 10 20 20 10 10 30 50 20 10.

directives here ect.



int main() {
int n;
int t = 0;
cin >> n;
int freq[101] = {};
for(int i = 0; i < n; i++) {
int c;
cin >> c;
freq[c]++;
}

int res = 0;
for(int i = 0; i <= 100; i++){
res += freq[i] / 2;
}
cout << res << endl;
return 0;
}
Last edited on
Idiomatic for loops for idiots

What is this title?
@ SakurasouBusters

I understood the title. Maybe you should learn how to use a dictionary instead of asking stupid questions.


Hello puffinpots,

Welcome to the forum.

If the following code comments do not explain everything, let me know. The reason the final output is 3 is due to line 18 adding 1 to the array element based on the number entered and stored in "c". The code at lines 28 - 35 does some math on each element of the array and stores the answer in res. The last cout statement prints out final value of res just once which ends up being 3.

The way I changed the code below should help you to understand how the code works.

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
int main()
{
	int n{ 0 };  // You should always initialize your variables.
	int t = 0;

        std::cout << "\n Enter the number of numbers to use: ";
        cin >> n;
	int freq[101]{ 0 };  //  Initializing with 0 will make all elements of the array equal to 0.
	
	std::cout << "\n" << endl;  // Added for space between the 1st input and 1st loop.

	std::cout << "\n Enter " << n << " different numbers each on its own line:" << endl;
	
	for (int i = 0; i < n; i++)
	{
		int c;
		cin >> c;
		freq[i] = c;  //  Changed form freq[c]++. Using c as the subscript will not work as well.
		              //  Keeping the input at the beginning of the array makes the output easier to read.
		              //  Although the original will work the numbers stored will be spaced
		              //  through the array
	}

	std::cout << "\n" << endl;  // Added for space between the input of 1st loop and output of 2nd loop.

	int res = 0;
	
	for (int i = 0; i < n; i++)  // changed to < n to shorten the output.
	{
		res += freq[i] / 2;  // Divides freq[i] by 2 and adds result to res. Same as saying
		                     // res = res + freq[i] / 2.
		
		//std::cout << endl;
		cout << res << endl;  //  Moved into the loop to show results instead of just the final result.
	}


    return 0;
}


Hope that helps,

Andy


Thank you for clarifying some things for me, and I'm sorry for not adding more to my question. I don't understand how when the input is 9 integers w/values from 10 to 50 that after the 2nd for loop runs it's course that res += freq[i] / 2 becomes 3.

So..
freq[0] / 2 is 10/2 and added into the sequence. It's hard for me to see how that works out.
After the first loop of original code you have all freq array equal zero except

freq[10]=4
freq[20]=3
freq[30]=1
freq[50]=1

Divide each of these by 2 and add them to get three.

Remember because res is an integer 3/2 = 1 and 1/2 =0.
Topic archived. No new replies allowed.