Loop

How can I print 2 line like this to console?
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20

The first line is i loop from 1 to 10
The second line is j loop from 11 to 20?
The requirement must be 2 loop.
Last edited on
Your output looks like 10 lines, not two. Would you like to explain again?
Going by your example and not your description:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <string>
using std::cout;
int main()
{
    for (int i = 0; i < 10; i++)
    {
        cout << (i+1) << " " << (i+11) << '\n';
 
        for (int lol_second_loop = 0; lol_second_loop < 1; lol_second_loop++)
        {
            // nop
        }
    }
}

1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20


If you actually wanted the first LINE to be from 1 to 10, and second to be from 11 to 20, this actually makes more sense as far as having "two loops" goes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
using std::cout;
int main()
{
    for (int i = 0; i < 10; i++)
    {
        cout << (i+1) << " ";
    }
    cout << '\n';
    for (int i = 0; i < 10; i++)
    {
        cout << (i+11) << " ";
    }
    cout << '\n';
}

1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 


Or, perhaps if you didn't find my first example funny,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <string>
using std::cout;
int main()
{
    for (int i = 0; i < 10; i++)
    {
    	for (int j = 0; j < 11; j += 10)
    	{
    		cout << (i + 1 + j) << " ";
    	}
    	cout << '\n';
    }
}

1 11 
2 12 
3 13 
4 14 
5 15 
6 16 
7 17 
8 18 
9 19 
10 20 
Last edited on
My actual project need to print like this
1
2
3
4
5
6
7
8
for (auto pair : huffmanCode)
{
	cout << pair.first << "\t\t\t\t"<<pair.second << endl;
}
for (int i = 0; i < n-1; i++)
{
	cout << "\t\t" << arr[i] << endl;
}


I need to put arr[i] between pair.first and pair.second.
How can I do it?
Last edited on
Whatever type your huffmanCode is, create an iterator for it.

Maybe
auto hit = huffmanCode.begin();

Then - assuming that your arr array and your huffmanCode container have the same length,
1
2
3
4
for (int i = 0; i < n-1; i++, ++hit)
	{
		cout << *hit.first << "\t"<<*hit.second << "\t" << arr[i] << endl;
	}

Is the number of pairs equal to n - 1?

If so, just take the easy way out and do:
1
2
3
4
for (size_t i = 0; i < huffmanCode.size(); i++)
{
    cout << pair.first << "\t\t" << arr[i] << "\t\t" << pair.second << endl;
}

(Edit: Of course, using an iterator as salem c suggests is the more general solution, for example in case huffmanCode is an std::list)

Edit 2: My code is not complete, and therefore doesn't make sense.
Last edited on
@ganado
do you meant like this?
1
2
3
4
5
6
7
for (auto pair : huffmanCode)
{
	for (size_t i = 0; i < huffmanCode.size(); i++)
	{
	     cout << pair.first << "\t\t" << arr[i] << "\t\t" << pair.second << endl;
	}
}


Then the answer will show wrong.
I'm calculating the frequency so if I put the code like that, it's gonna loop every single letter.
Here is my full function
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
void buildHuffmanTree(string text, map<char, int> freq)
{
	int arr[100];
	int n = 0;
	ifstream my_file1("text.txt");
	if (my_file1.fail())
	{
		cout << "Failed to open this file!" << endl;
	}
	while (!my_file1.eof())
	{
		my_file1 >> arr[n];
		n++;
	}
	my_file1.close();


	//map<char, int> freq;
	for (char ch : text)
	{
		freq[ch]++;
	}
	priority_queue<huffman_node*, vector<huffman_node*>, huffman_node> pq;
	for (auto pair : freq)
	{
		pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
	}
	while (pq.size() != 1)
	{
		huffman_node* left = pq.top(); pq.pop();
		huffman_node* right = pq.top();	pq.pop();
		int sum = left->priority + right->priority;
		pq.push(getNode('\0', sum, left, right));
	}
	huffman_node* root = pq.top();
	map<char, string> huffmanCode;
	encode(root, "", huffmanCode);
	cout << "char" << "\t\t" << "frequency" << "\t" << "encoding" << endl;
	cout << "-----------------------------------------" << endl;
		for (auto pair : huffmanCode)
		{
			cout << pair.first << "\t\t\t\t"<<pair.second << endl;
		}
		for (int i = 0; i < n-1; i++)
		{
			cout << "\t\t" << arr[i] << endl;
		}
Last edited on
Wow, guess I'm tired, that code I made indeed doesn't make sense. No, I didn't mean that.

Go with salem c's code.
Last edited on
> while (!my_file1.eof())
Don't use eof() to control a loop. It doesn't mean what you think it does.
eof() is a state - caused by the action of a previous operation, not a prediction of a future outcome.
If you have a 10 character file, and you read 10 characters, eof() is still false.

TLDR
1
2
3
4
while (n < 100 && my_file1 >> arr[n])
	{
		n++;
	}


Then you don't need that messy n-1 later on in your code and you can just use i<n like you would for any correctly sized element count.

How can I print 2 line like this to console?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
int main()
{   
    for (int row = 1; row < 11; ++row) {
	for (int val = row; val <= row+10; val += 10) {
	    std::cout << val << ' ';
	}
	std::cout << '\n';
    }
}

or
1
2
3
4
5
6
    for (int row = 1; row < 11; ++row) {
	for (int col=0; col<2; ++col) {
	    std::cout << row+col*10 << ' ';
	}
	std::cout << '\n';
    }


1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20


I'm not sure how that relates to your Huffman coding example. Do you want something like this?:
1
2
3
4
    unsigned idx = 0;
    for (auto pair : huffmanCode) {
        cout << pair.first << "\t\t" << arr[idx++] << "\t\t"<<pair.second << endl;
    }


Note that this assumes that arr has at least as many items as huffmanCode.
try this ....
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main()
{
    for(int i=1;i<=10;i++)
    {
        for(int j=10;j==10;j++)
        {
            cout<<i<<"  "<<j+i<<endl;
        }
    }

}
Topic archived. No new replies allowed.