Multiple values on an array

Pages: 12
Hello, I need to make a memory game where I have a static array with max of 20 strings and then I need to allocate it to a dynamic array and have the words multiple by 2 so that they appear 2 times. The problem is I have no clue how to do that. I would really appreciate some answers, here's how long I have gotten so far.

Apologize for my newbie coding, I recently started.

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
  int nrOfStrings = 0;
	string *game = nullptr;
	game = new string[nrOfStrings * 2];
	const int CAP = 20;
	string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake", "Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };
	bool *answer = nullptr;
	string choice = "";

	cout << "How many words do you want to use? " << endl;
	cin >> nrOfStrings;


	cout << "Words in use are: ";
	/*for (int i = 0; i < nrOfStrings; i++)
	{
	cout << str[i] << "  ";
	}
	cout << endl;*/


	/*for (int i = 0; i < nrOfStrings; i++)
	{
		cout << str[i] << "  ";
	}
	cout << endl;*/

	for (int i = 0; i < nrOfStrings*2; i++)
	{
		game[i++] = str[i];
		cout << game[i] << " ";
	}
	cout << endl;
	


	return 0;
Last edited on
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
#include <iostream>
#include <string>
using std::cout; using std::cin; using std::endl;
using std::string;

int main() 
{
	int nrOfStrings = 0;
	const int CAP = 20;
	const string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake", 
					"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };
	
	cout << "How many words do you want to use? " << endl;
	cin >> nrOfStrings;

	string *game = new string[nrOfStrings * 2];
	
	for(int n = 0; n < 2; n++)		//loop thru array twice
	{
		for(int i = 0; i < nrOfStrings; i++)
		{
			game[i] = str[i];		//copy str[] into game[]
			cout << game[i] << "   ";		//spaces for formatting
		}
		cout << endl;		//new line after first iteration for formatting
	}

	delete[] game;		//remember to use [] when deleting arrays!
	
	return 0;
}
Last edited on
But this does not store double the words in the dynamic array does it? Like lets say the user prints in that he wants to use 5 words, the array known as game would only be storing those 5 words in that case. But it would need to store 10, since the 5 words that are chosen needs to appear twice.

I mean this is just the beginning of the program, but this would basically be the base of it.
Last edited on
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
#include <iostream>
#include <string>
#include <vector>

int main()
{
        const std::size_t CAP = 20;
        std::string words[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado",
	                           "Cracker", "Cake", "Chocolate", "Car", "Bus", "Bird", "Airplane", "House",
	                           "Umbrella", "Cat", "Dog", "Mouse" };
       // shuffle the words randomly, perhaps

	std::cout << "How many words do you want to use? " ;
	std::size_t nwords ;
	std::cin >> nwords ;

	if( nwords > CAP ) nwords = CAP ;

	// https://cal-linux.com/tutorials//vectors.html
	// http://en.cppreference.com/w/cpp/container/vector/vector constructor (4)
	std::vector<std::string> game_words( words, words+nwords ) ; // initialise the vector with the first 'nwords' words

	// http://en.cppreference.com/w/cpp/container/vector/insert overload (4)
	game_words.insert( game_words.end(), words, words+nwords ) ; // add the same words once again

        // http://www.stroustrup.com/C++11FAQ.html#auto
        // http://www.stroustrup.com/C++11FAQ.html#for
	for( const auto& word : game_words ) std::cout << word << '\n' ;
}

http://coliru.stacked-crooked.com/a/bb4b9d5f66be0c05
Ah, vectors aren't allowed. Basically the requirement is to have a static array with 20 words and a dynamic array which holds the chosen words + duplicate words and also to have a bool array which compares the 2 words to check if they are the same when chosen by the user.
Last edited on
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
#include <iostream>
#include <string>
#include <memory>
#include <algorithm>
#include <random>

int main()
{
    const std::size_t CAP = 20;
    std::string words[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado",
                               "Cracker", "Cake", "Chocolate", "Car", "Bus", "Bird", "Airplane", "House",
                               "Umbrella", "Cat", "Dog", "Mouse" };

    std::cout << "How many words do you want to use? " ;
    std::size_t nwords ;
    std::cin >> nwords ;

    if( nwords > CAP ) nwords = CAP ;
    std::cout << "nwords == " << nwords << "\n\n" ;
    const std::size_t array_sz = nwords*2 ;

    // http://en.cppreference.com/w/cpp/memory/unique_ptr (2)
    std::unique_ptr< std::string[] > game_words( new std::string[array_sz] ) ;
    std::unique_ptr< bool[] > same( new  bool[array_sz]{} ) ;

    // http://en.cppreference.com/w/cpp/algorithm/copy
    std::copy( words, words+nwords, game_words.get() ) ; // copy the first 'nwords' words
    std::copy( words, words+nwords, game_words.get()+nwords ) ; // append the same set of words

    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    // http://en.cppreference.com/w/cpp/numeric/random
    std::shuffle(game_words.get(), game_words.get() + array_sz, std::mt19937( std::random_device{}() ) ) ;

    for( std::size_t i = 0 ; i <array_sz ; ++i ) std::cout << i+1 << ". " << game_words[i] << '\n' ;
}

http://coliru.stacked-crooked.com/a/143dcdf697667ffe
closed account (48T7M4Gy)
Modifying integralfx's good work a bit.

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
#include <iostream>
#include <string>
using std::cout; using std::cin; using std::endl;
using std::string;

int main() 
{
	const int CAP = 20;
	const string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake", 
					"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };
					
	int sz = sizeof(str);
	
	string *game = new string[sz];
	
	for(int i = 0; i < CAP; i ++ )
	{
	    game[i] = str[i];
		game[i+CAP] = str[i];
	}
	
	for(int i = 0; i < 2 * sz; i ++ )
	    cout << game[i] << " " << endl;

        delete [] game;
	    
	return 0;
}
Last edited on
I made some changes to it. This one almost works for me, now the only problem is that it's only giving me each word once while it needs to give me the word twice. And it's also it's giving all 10 words if I write in 5, but it should only give me the 5 first words and then the 5 first words again.

Basically what the output SHOULD be is the following:
Apple, Banana, Peach, Lemon, Coconut, Apple, Banana, Peach, Lemon, Coconut.

Also the following #includes are the only ones that are allowed to be used.

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 <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	int nrOfStrings = 0;
	string *game = nullptr;
	bool *answer = nullptr;
	string choice = "";
	const int CAP = 20;
	string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake",
		"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };

	cout << "How many words do you want to use? " << endl;
	cin >> nrOfStrings;

	cout << "The words in use are: " << endl;

	for (int i = 0; i < nrOfStrings; i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	game = new string[nrOfStrings * 2];
	game = str;

	for (int i = 0; i < nrOfStrings; i++)
	{
		game[i] = str[i];
	}

	for (int i = 0; i < nrOfStrings * 2; i++)
	{
	cout << game[i] << " " << endl;
	}

	return 0;
}
Last edited on
closed account (48T7M4Gy)
now it's jumping 2 spaces in the array


That's because of i++ in line 26

Why don't you show an example of what you are trying to do instead.
closed account (48T7M4Gy)
Hmmm must be a secret.
1
2
3
4
5
6
7
8
9
10
11
12
13
cin >> nrOfStrings;
if( nrOfStrings > CAP ) nrOfStrings = CAP ; // *** required, added ***
if( nrOfStrings < 0 ) nrOfStrings = 0 ; // *** required, added ***

game = new string[nrOfStrings * 2]; // need delete[] game ; at the end
// game = str; // *** no. NO ***

// for (int i = 0; i < CAP; i++)
for (int i = 0; i < nrOfStrings ; ++i )
{
    // game[i++] = str[i];
    game[i] = game[nrOfStrings+i] = str[i] ;
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>
using std::cout; using std::cin; using std::endl;
using std::string;

int main() 
{
	const int CAP = 20;
	const string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake", 
					"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };
					
	int wordCount = 0;
	cout << "How many words? ";
	cin >> wordCount; // need extra lines to check this
	
	int sz = wordCount; //sizeof(str);
	
	string *game = new string[2 * sz];
	
	for(int i = 0; i < sz ; i++) //CAP; i ++ )
	{
	    game[i] = str[i];
		//game[i+CAP] = str[i];
		game[i+sz] = str[i];
	}
	
	for(int i = 0; i < 2 * sz; i ++ )
	    cout << game[i] << " " << endl;

       delete [] game;
	    
	return 0;
}


How many words? 3
Apple 
Banana 
Peach 
Apple 
Banana 
Peach 
Last edited on
It still only prints the strings out once.

1
2
3
4
5
6
7
8
9
10
11
12
13
cin >> nrOfStrings;
if( nrOfStrings > CAP ) nrOfStrings = CAP ; // *** required, added ***
if( nrOfStrings < 0 ) nrOfStrings = 0 ; // *** required, added ***

game = new string[nrOfStrings * 2]; // need delete[] game ; at the end
// game = str; // *** no. NO ***

// for (int i = 0; i < CAP; i++)
for (int i = 0; i < nrOfStrings ; ++i )
{
    // game[i++] = str[i];
    game[i] = game[nrOfStrings+i] = str[i] ;
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>
using std::cout; using std::cin; using std::endl;
using std::string;

int main() 
{
	const int CAP = 20;
	const string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake", 
					"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };
					
	int wordCount = 0;
	cout << "How many words? ";
	cin >> wordCount; // need extra lines to check this
	
	int sz = wordCount; //sizeof(str);
	
	string *game = new string[2 * sz];
	
	for(int i = 0; i < 2 * sz ; i += 2) //CAP; i ++ )
	{
	    game[i] = str[i];
		//game[i+CAP] = str[i];
		//game[i+sz] = str[i];
		game[i+1] = str[i];
	}
	
	for(int i = 0; i < 2 * sz; i ++ )
	    cout << game[i] << " " << endl;

       delete [] game;
	    
	return 0;
}


How many words? 3
Apple 
Apple 
Peach 
Peach 
Coconut 
Coconut 
Last edited on
#kemort
Well basically it's a memory game. And in a memory game you have 1 card appear twice and when all cards are taken the one with most completed sets wins.

So basically I have 1 static array and 1 dynamic array. The static array holds all the 20 different cards. Then the user inputs how many cards out of 20 that he wants to use in the game. Lets say the user inputs 5.
Then the dynamic array should be able to store 10 strings in it, because the dynamic array needs to hold the 5 cards that are chosen + another 5 cards that are the same like the first 5.
Then when a user inputs lets say 2 and 6, then I need to have another bool array which checks position 2 and 6 in the dynamic array and compares them if they are the same or not. And if they are the same, 2 and 6 are removed from the array. And if they are not the same, the game continues like nothing happend.
closed account (48T7M4Gy)
Then the dynamic array should be able to store 10 strings in it, because the dynamic array needs to hold the 5 cards that are chosen + another 5 cards that are the same like the first 5.


OK each of the alternatives ppl have given you seem to produce an array of duplicate cards.
ie select 3 cards say 1,2 and 3 and then duplicate cards 1, 2 and 3 stored in a new array called game - 1 2 3 1 2 3 if you like or 1 1 2 2 3 3 whatever.

What's wrong so far, forgetting about the later step with bool etc.
Last edited on
Seems like it might be working now

It gives me the output when I input 5.

Apple
Banana
Peach
Lemon
Coconut
Apple
Banana
Peach
Lemon
Coconut


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

using namespace std;

int main()
{
	int nrOfStrings = 0;
	string playerOne;
	string playerTwo;
	bool p1Turn = 0;
	bool p2Turn = 0;
	bool gameOn = 1;
	string *game = nullptr;
	bool *answer = nullptr;
	int choice1 = -1;
	int choice2 = -1;
	const int CAP = 20;
	string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake",
		"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };

	cout << "How many words do you want to use? " << endl;
	cin >> nrOfStrings;

	if (nrOfStrings > CAP)
	{
		nrOfStrings = CAP;
	}
	if (nrOfStrings < 0)
	{
		nrOfStrings = 0;
	}

	cout << "The words in use are: " << endl;

	for (int i = 0; i < nrOfStrings; i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	game = new string[nrOfStrings * 2];

	for (int i = 0; i < nrOfStrings; ++i)
	{
		game[i] = game[nrOfStrings + i] = str[i];
	}

	for (int i = 0; i < nrOfStrings * 2; i++)
	{
	cout << game[i] << " " << endl;
	}
  delete [] game;
	    
	return 0;
}





Now I just need to figure out how to compare the 2 positions in the array with each other. >.<
Last edited on
closed account (48T7M4Gy)
If this is right all you have to do is randomize selection from str[] and game after it is set up

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>
using std::cout; using std::cin; using std::endl;
using std::string;

int main() 
{
	const int CAP = 20;
	const string str[CAP] = { "Apple", "Banana", "Peach", "Lemon", "Coconut", "Mango", "Orange", "Avokado", "Cracker", "Cake", 
					"Chocolate", "Car", "Bus", "Bird", "Airplane", "House", "Umbrella", "Cat", "Dog", "Mouse" };
					
	int wordCount = 0;
	cout << "How many words? ";
	cin >> wordCount; // need extra lines to check this
	
	int sz = wordCount; //sizeof(str);
	
	string *game = new string[2 * sz];
	
	for(int i = 0; i < 2 * sz ; i += 2) //CAP; i ++ )
	{
	    game[i] = str[i];
		//game[i+CAP] = str[i];
		//game[i+sz] = str[i];
		game[i+1] = str[i];
	}
	
	for(int i = 0; i < 2 * sz; i ++ )
	    cout << game[i] << " " << endl;
	    
	    int a = 0, b = 0;
	    cout << "Enter two positions: ";
	    cin >> a >> b; // need to add lines for validation
	    
	    if ( game[a] == game[b] )
	        cout << "Bingo";
	   else
	       cout << "Loser";

       delete [] game;
	    
	return 0;
}


How many words? 3
Apple
Apple
Peach
Peach
Coconut
Coconut
Enter two positions: 1 2
Loser


How many words? 4
Apple 
Apple 
Peach 
Peach 
Coconut 
Coconut 
Orange 
Orange 
Enter two positions: 0 1
Bingo 
Last edited on
I see, but that does not explain how the words are inputted in to a bool dynamic array.

Since I need to have a 2 dynamic array, 1 array of the type string and 1 of the type bool. And then bool array is supposed to mark the position of the words that are taken.

The whole compare part makes sense though.
closed account (48T7M4Gy)
a bool dynamic array can only be filled with elements that are either true or false. it can't hold strings.

what is producing the element in the bool array?
Pages: 12