Permutations and combinations

Hi I need help trying to figure out what I am doing wrong here. My assignment is to generate permutations and combinations of 4 people. However, when I run it and I ask the user to enter 4 names, it stops. Can someone help me figure out what is wrong? Or is there a better way to write it? Thanks
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
#include<iostream>
#include<string>

using namespace std;

int main()
{
	string names[4];

	for (int i = 1; i <= 4; i++)
	{
		cout << "Enter the first name: ";
		cin >> names[i];
		for (int j = 2; j <= 4; j++)
		{
			cout << "Enter the second name: ";
			cin >> names[j ];
			for (int k = 3; k <= 4; k++)
			{
				cout << "Enter the third name: ";
				cin >> names[k];
				for (int l = 4; l <= 4; l++)
				{
					cout << "Enter the fourth name: ";
					cin >> names[l];
				}
			}
		}
	}

	// PRINTS ALL COMBINATIONs OF FOUR PEOPLE //
	cout << "All Combinations including four people" << endl;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			for (int k = 0; k < 4; k++)
			{
				for (int l = 0; l < 4; l++)
				{
					cout << names[i] << " " << names[j] << " " << names[k] << " " << names[l] << " " << endl;
				}
			}
		}
	}

	// PRINTS ALL COMBINATIONs OF TWO PEOPLE //
	cout << "All Combinations including two people" << endl;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			cout << names[i] << " " << names[j] << endl;
		}
	}

	// PRINTS ALL COMBINATIONs OF THREE PEOPLE //
	cout << "All Combinations including three people" << endl;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{

			for (int k = 0; k<4; k++)
			{
				cout << names[i] << " " << names[j] << " " << names[k] << endl;
			}
		}
	}

	// PRINTS ALL PERMUTATIONS OF FOUR PEOPLE //
	cout << "All Permutations including four people" << endl;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (j == i)
				continue;
			for (int k = 0; k<4; k++)
			{
				if (k == j || k == i)
					continue;
				for (int l = 0; l<4; l++)
				{
					if (l == i || l == k || l == j)
						continue;
					else
						cout << names[i] << " " << names[j] << " " << names[k] << " " << names[l] << " " << endl;
				}
			}
		}
	}
	system("Pause");
	return 0;
}
Last edited on
Well for starters, you're not getting the names correctly and it's crashing your program.
C++ arrays are accessed starting at index 0, not 1. So if there are 4 items in the array, you access them with indices 0,1,2,3, not 1,2,3,4. This is usually coded in a for loop as:
for (i=0; i<4; ++i)
Learn it. Know it. Love it. Always think of the range from A through B as the range that includes A, and goes up to but not including B+1. This is the way the standard library always does it.

You only want to enter the first name once, so why do you prompt for it inside a loop that runs 4 times? The same point holds for entering the other names. Right now, your code attempts to prompt for the 4th name 256 times.
Topic archived. No new replies allowed.