Array Allocation

Pages: 12
There are 5 siblings. They are Bob, Charles, Ben, Darve and Jack. The gifts are a rice cooker, a camera, a set of knifes, a smart phone and a tablet. Write a porgram to to allocate the gift accordingly. The program first displays the list of gift avialble, followed by reading the gift numbers allocated for each sibling. in any enters the sane gift number for different persons, an error is display and re-enter.

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
 #include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
	int const limit=5;
	string name[limit];
	string gift[limit];
	int count[limit];
	int i,x,counts;
	
	name[0]="Bob";
	name[1]="Carlos";
	name[2]="Ben";
	name[3]="Darve";
	name[4]="Jack";

	gift[0]="Rice Cooker";
	gift[1]="Camera";
	gift[2]="Knife Set";
	gift[3]="Phone";
	gift[4]="Tablet";

	cout<<"The following gifts are available. "<<endl;
	cout<<"1. Rice Cooker"<<endl;
	cout<<"2. Camera "<<endl;
	cout<<"3. Knife Set"<<endl;
	cout<<"4. Phone"<<endl;
	cout<<"5. Tablet"<<endl;
	for(i=0; i<5; i++)
	{
		count[i]=0;
	}
	cout<<"Enter the gift number for thr following people: "<<endl;
	for(i=0; i<5; i++)
	{
		cout<<name[i]<<" : ";
		while(cin>>counts)
	{
		switch(counts)
		{
		case 1: count[0]++;
			break;
		case 2: count[1]++;
			break;
		case 3: count[2]++;
			break;
		case 4: count[3]++;
			break;
		case 5: count[5]++;
			default:
				cout <<"The gift has given to "<<name[i]<<endl;
				cout<<"Please enter another gift."<<endl;
				break;
			}
				cout<<name[i]<<" : ";
	}
	}

	for (i = 0; i < 5; i++)
		{
			for (x = 0; x < 5; x++)
			{
				if (count[i] > count[x])
				{
					swap(count[i], count[x]);
					swap(name[i], name[x]);
				}
			}

		}
	cout<<"The gift allocation is : "<<endl;
     for (i = 0; i < 5; i++)
		{
			cout<< name[i] <<"  "<< gift[i] << endl;
		}
	system("Pause");
	return 0;
}


EDIT: I just learned array. I did everything i know with array. i know my programme is wrong. Would appreciate if someone give me advice and guidance. My knowledge with array is shallow.

SAMPLE OUTPUT :
The following gifts are available:
1 Rice Cooker
2 Camera
3 Knife set
4 Phone
5 Tablet
Enter the gift number four the following people.
Bob : 2
Carlos : 1
Ben : 2
This gift has been given to Bob
Please enter another gift.
Ben : 3
Dave : 1
This gift has been given to Carlos
Please enter another gift.
Dave : 4
Jack : 2
This gift has been given to Bob
Please enter another gift.
Jack : 5
Your gift allocation is :
Bob      Camera
Carlos   Rice Cooker
Ben      Knife Set
Dave     Phone
Jack     Tablet
Last edited on
You could have lists of names (swords), gifts (words), and what to give (numbers).

For example:
1
2
3
string name[limit];
string gift[limit];
int what[limit];


Then you want to show
name[i] and corresponding gift[ what[i] ]

Obviously, the numbers in what must be in valid range (from 0 to limit-1). Therefore, when the user gives input, you must first check that the input is in valid range.

The second thing to check is whether the same input has occurred before, i.e. when considering input for what[i], you should check if any previous element of what already has that value.

It could be convenient to write a separate function for that. For example:
bool already( int * array, int count, int value )

Which could be used in some fashion:
1
2
3
4
5
if ( already( what, i, counts ) ) {
 // the counts is already in what
} else {
 what[i] = counts;
}
@keskiverto Thanks for the reply~ i will try it out
it it possible to write it without function ? because i havent learn anything about function yet.
Last edited on
@keskiverto from what i read from your reply.. i have to redo my programme right ? since i know my programme is somehow in the wrong logics. Is it about dynamic array ? because i just learn about basic arrays, and i did watch some tutorial from youtube about array. It doesn't show
as deep as this questions. Hope that you can give me more hints about doing it. :)
No, a separate function is not compulsory. Dynamic arrays are not necessary either.


There are more than one valid logic. The one I did outline marks user input invalid, if it is not one of the available gifts. That makes the last gift most challenging for the user, because there is only on valid answer (but the user can type any integer).


Meanwhile, lets look at lines 26-30. You have there string literals that suspiciously resemble the values that you did type on lines 19-23. What would this do:
1
2
3
4
5
cout << "The following gifts are available.\n";
for ( i = 0; i < 5; ++i )
{
  cout << i+1 <<".  "<< gift[i] << '\n';
}


Could we keep track of available gifts? With an array (of bool)?
1
2
3
4
5
6
7
bool available[] = { true, true, true, true, true };

cout << "The following gifts are available.\n";
for ( i = 0; i < 5; ++i )
{
  if ( available[i] ) cout << i+1 <<".  "<< gift[i] << '\n';
}


Lets say that you have:
1
2
3
int input;
cin >> input;
int index = input - 1; // user types 1-5, we use 0-4 

If index is less than 0 or more than 4, then input is clearly invalid
If available[index] is false then that gift has already been assigned and the input is invalid.

When the index is valid, you will store it what[currentperson] = index; and mark the gift used available[index] = false;

For user convenience, you can show remaining gifts on every person's turn.

Note that technically we don't need to ask what Jack gets, because by them there is only one option left.


PS. The indentation of your posted code could be more systematic. For example, it is difficult to see what is within the for-loop that starts on line 36.
@keskiverto thanks for the reply once again~ for
what[currentperson]=index;

the
currentperson
is it for example
string currentperson[5]
? or is it another delcartion to store the memory ?
An index, e.g.
1
2
3
4
for ( int currentperson = 0; currentperson < 5; ++currentperson )
{
  cout<< name[currentperson] <<"  will receive "<< gift[ what[currentperson] ] << '\n';
}
@keskiverto Thanks once again! i will try it out. Can i use switch statement to do so ? or i have to use another structure to approach ?
You don't need a switch. You do need ifs for testing invalid input.
@keskiverto Thanks once again for your reply.

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
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
	int const limit=5;
	string name[limit];
	string gift[limit];
	int count[limit];
	int i,input=0;
	int currentperson=0;
	
	name[0]="Bob";
	name[1]="Carlos";
	name[2]="Ben";
	name[3]="Darve";
	name[4]="Jack";

	gift[0]="Rice Cooker";
	gift[1]="Camera";
	gift[2]="Knife Set";
	gift[3]="Phone";
	gift[4]="Tablet";

	bool available[] = { true, true, true, true, true };

    cout << "The following gifts are available.\n";
    for ( i=0; i<5; i++)
    {
     if ( available[i] ) 
		 cout << i+1 <<".  "<< gift[i] << '\n';
    } 
	cout<<"Enter the gift number for thr following people: "<<endl;
	for(i=0; i<5; i++)
	{
		cout<<name[i]<<" : ";
		cin>>input;
		int index = input - 1;
		if(index<0 || index>4)
		{
		    count[currentperson]=index;
		    available[index]=false;
		}
		else 
		{
		cout <<"The gift has given to "<<name[i]<<endl;
		cout<<"Please enter another gift."<<endl;
		}
	}
        
	system("pause");
	return 0;
}


This is my current code. I only capable doing it till here by following your advice. I guess im stuck already.
What does 'i' represent in the loop on lines 29-33?
What does 'i' represent in the loop on lines 35-50?
What does 'currentperson' represent in the loop on lines 35-50?

When is the condition on line 40 true?

Number 5 occurs on lines 29, 35, (and in disguise on line 40). Do those values have some relation to the named constant of line 7?
@keskiverto Thanks for the reply. I will try it out .
@keskiverto im sorry to bother you again.. i spend 3 hours in fixing the code. it seems i doesnt get anywhere. Would you please tell me more about it ? sorry to bother again.
Can you answer the questions of my previous post?
@keskiverto. Okay.
In line 29 to 33 i represent for the gift[0],gift[1] and so on till gift[4].
In line 33 to 50 i represent the name[0] and so on.
The currentperson i guess is to store the input. ?
The condition is true when the index is more than 0 and less than 4.
Yes. It has some relation since there is 5 numbers which bring array 0 to 5.
In line 29 to 33 the i is an index to the array gift, specifying which gift object current iteration of the loop will refer to.

In line 33 to 50 the i is an index to the array name, specifying which person current iteration of the loop will refer to. The i and currentperson are thus one and the same. Lets make it so and move the gift-printing loop inside the other loop too:
1
2
3
4
5
6
7
8
9
10
for ( int currentperson=0; currentperson < limit; ++currentperson )
{
  cout << "The following gifts are available:\n";
    for ( int gifidx=0; gifidx < limit; ++gifidx )
    {
       if ( available[gifidx] ) cout << gifidx+1 <<".  "<< gift[gifidx] << '\n';
    } 
  cout << "Enter the gift number for " << name[currentperson] << '\n';
  // something
}

Descriptive(?) variable names might make the logic of the program easier to see.

1
2
3
index<0 || index>4
// is same as
index<0 or index>4

It is true, if:
* index is less than 0
OR
* index is more than 4

In other words, it is not true, if index is any of 0, 1, 2, 3, 4.

For the index to be valid, three conditions must be simultaneously true:
1. index is at least 0 ( 0 <= index)
AND
2. index is less than limit ( index < limit )
AND
3. that gift is still available ( available[index] )

There are two causes of invalid index:
1. index is not in range [0..limit)
2. gift is not available
They should give different error message.

In you original code you had:
1
2
3
4
while ( cin >> input )
{
  // something
}

That something should test for index validity and either end the loop (when index is valid) or give appropriate error message (and continue the loop). For example:
1
2
3
4
5
6
7
8
9
if ( /* out of range */ ) {
  // tell that valid range is from 1 to limit
}
else if ( /* not available */ ) {
  // tell that gift has already been given to somebody
}
else {
  break;
}
@keskiverto thanks for the reply once again.
in this loop right. i tested it out. it loop few times.

1
2
3
4
5
6
7
8
9
10
for ( int currentperson=0; currentperson < limit; ++currentperson )
{
  cout << "The following gifts are available:\n";
    for ( int gifidx=0; gifidx < limit; ++gifidx )
    {
       if ( available[gifidx] ) cout << gifidx+1 <<".  "<< gift[gifidx] << '\n';
    } 
  cout << "Enter the gift number for " << name[currentperson] << '\n';

}



4.Phone
5.Tablet
Enter the gift number four the following people.
Carlos : The following gifts are available:
1 Rice Cooker
2 Camera
3 Knife set
4 Phone
5 Tablet
Enter the gift number four the following people.
Ben : The following gifts are available:
1 Rice Cooker
2 Camera
3 Knife set
4 Phone
5 Tablet
Enter the gift number four the following people.
Darve : The following gifts are available:
1 Rice Cooker
2 Camera
3 Knife set
4 Phone
5 Tablet
Enter the gift number four the following people.
Jack :
Last edited on
Hi, I'm fairly new to c++ and still learning it too. I tried some of your code, and "improved" it (but still far for what you want to), so here's mine


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

using namespace std;

const int limit = 5;
int main() {
	std::array<string,limit> Name = {"Bob","Carlos","Ben","Dave","Jack"};
	std::array<string,limit> Gift = { "Rice Cooker", "Camera","Knife Set","Phone","Tablet" };
	bool available[] = { true,true,true,true,true };
	
	
	cout << "The following gifts are available: " << endl;
	for (int gifidx = 0; gifidx < Gift.size(); gifidx++) {
		cout << gifidx + 1 << ") " << Gift[gifidx] << endl;
	}
	cout << "Enter the gift number for the following people." << endl;
	
	for (int selectGift = 0; selectGift < limit; selectGift++) {
		int number;
		
		cout << Name[selectGift] << " : ";
		cin >> number;
		int index = number - 1;

		
			if (available[index] == true) {
				cout << Gift[index] << endl;
				available[index] = false;
			}
			else if (available[index] == false) {
				cout << "This gift has been given" << endl;
			}
			else {
				cout << "error" << endl;
				break;
			}

		}
	
	return 0;
};


still have to working with while for the numbers though and after the gift has been to "someone"
@Rasdopi Thanks for the reply. ~

here my code : stll it has some error as well.

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<string>
#include<iomanip>

using namespace std;
int main() { 
    int const limit=5;
    int count[limit];
    string name[limit];
    string gift[limit];  
    int i,input=0; 
    int currentperson=0;
    name[0]="Bob";
    name[1]="Carlos";
    name[2]="Ben";
    name[3]="Dave";
    name[4]="Jack";

    gift[0]="Rice Cooker";
    gift[1]="Camera";
    gift[2]="Knife Set";
    gift[3]="Phone";
    gift[4]="Tablet";

    bool available[] = { true, true, true, true, true };

    cout << "The following gifts are available.\n";
    for ( i=0; i<limit; i++) 
	{
        if ( available[i] ) 
            cout << i+1 <<".  "<< gift[i] << '\n';
        } 

    cout<<"Enter the gift number for the following people: "<<endl;

    for(i=0; i<limit; i++) 
	{
        cout<<name[i]<<" : ";
        cin>>input;
        int index = input - 1;
        if(index<0 || index>4) 
		{
            cout<<"Invalid Range"<<endl;
        }
        else if(available[index])
		{
            available[index]=false;
            count[currentperson]=index;
                } 
		else 
		{
            cout<<"The gift has already given to "<<name[index]<<endl;
            i--;
               }
     }
	for (i = 0; i < 5; i++)
		{
			cout<< name[i] <<"  "<< gift[i] << endl;
		}
    system("pause");
    return 0;
}


The output was not same :

The following gifts are available:
1 Rice Cooker
2 Camera
3 Knife set
4 Phone
5 Tablet
Enter the gift number four the following people.
Bob : 2
Carlos : 1
Ben : 2
This gift has been given to Carlos
Please enter another gift.
Ben : 3
Dave : 1
This gift has been given to Bob
Please enter another gift.
Dave : 4
Jack : 2
This gift has been given to Carlos
Please enter another gift.
Jack : 5
Your gift allocation is :
Bob      Rice Cooker
Carlos   Camera
Ben      Knife Set
Dave     Phone
Jack     Tablet




EDIT :
anyone could kindly look at my code and fix it ?
Last edited on
You have a good comment on line 51. Is that the only case when the 'i' should not increase?

Where is array 'count' used? You set values to it, but never read from it.

What did the 'currentperson' represent? What does the 'i' in that same loop represent? The very same thing. Replace the 'i' with 'currentperson' (in that loop).
Pages: 12