Arrays

Hello all I have a problem with a program that I have to write. I started it already but I cannot finish it. I don't know where to go from where I am. Any help would be greatly appreciated. Here is the description of what I have to do:

Write a program which will get 10 random numbers onto one array1 of size 10. Get 10 more random numbers and place them in array2 of size 10. The range of the numbers should be 1-10 only (no zero).

Ask for the names of two players.

The first array is for first player. The second array is for second player. You will compare the numbers for each person and whoever has the bigger number wins. You will compare the same element in the array for each player, so comparing element 0 from array1 with element 0 from array2. Keep count of how many wins and display the persons name of the one who won. *in case of a tie, keep rolling a random number for each player and compare to see who wins...they may tie again so keep going till someone wins.


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

using namespace std;

int main()
{
	int array1[10];
	int array2[10];
	int player1;
	int player2;

	cout<<"Please enter player ones name:"<<endl;
	cin>>player1;

	cout<<"Please enter player twos name:"<<endl;
	cin>>player2;

	srand((unsigned)time(0));

	for(int a=0; a<10; a++)
	{
		array1[a]=(rand()%10)+1;
	}

	for(int b=0; b<10; b++)
	{
		array2[b]=(rand()%10)+1;
	}

	//see out array1
	for(int a=0; a<10; a++)
	{
		cout<<array1[a]<< " ";
	}

	cout<<endl;

	//see out array2
	for(int b=0; b<10; b++)
	{
		cout<<array2[b]<< " ";
	}
	return 0;
}
1
2
3
4
5
6
7
for (int a=0; a<10; a++) {
  // three possibilities here:
  // 1. array1[a] < array2[a]
  // 2. array2[a] < array1[a]
  // 3. tie
  // Thus, the win count of at most one player can increment by one
}

You obviously have to have the two win counters.


In case of final tie do a loop that rolls new pairs and updates the win counters.
closed account (j3Rz8vqX)
Create two accumulator counters; set equal to 0.

Create a loop that compares each element (one at a time) of array1 and array2.

If array1 wins, increment the accumulator counter for array1.
if array2 wins, increment the accumulator counter for array2.
Else, if neither, retest

Then print out whoever wins, but comparing their counts.

To simplify, you could test everything in a single loop, since every loop is starting at 0 and incrementing to 10; patterned behavior.

Example:
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
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
    int array1[10], array2[10], player1 = 1,
    player2 = 2, count1=0, count2=0;

    srand((unsigned)time(0));
    for(int i=0; i<10; i++)
    {
        array1[i]=(rand()%10)+1;
        array2[i]=(rand()%10)+1;
        if(array1[i]>array2[i])
            ++count1;
        else if(array1[i]<array2[i])
            ++count2;
        else
            --i;//Redo this test.
    }
    //Compare count1 and count2
        //If count1 greater: print player1
        //If count2 greater: print player2
    return 0;
}

By the way, normally we would use strings from names but if this is the design then so be it.
I was able to get this far... Im not sure if it is correct as I am not even sure what my professor is asking us for.

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

using namespace std;

int main()
{
	int array1[10];
	int array2[10];
	string player1;
	string player2;
	int count1=0;
	int count2=0;

	cout<<"Please enter player ones name:"<<endl;
	cin>>player1;

	cout<<"Please enter player twos name:"<<endl;
	cin>>player2;
	cout<<endl;

	cout<<"The winner per element:"<<endl;

	srand((unsigned)time(0));
	  for(int i=0; i<10; i++)
    {
        array1[i]=(rand()%10)+1;
        array2[i]=(rand()%10)+1;
       	
		if(array1[i]>array2[i])
            cout<<player1<<endl;
			//++count1;
        else if(array1[i]<array2[i])
            cout<<player2<<endl;
			//++count2;
        else
            --i;//Redo this test.
	  }
	//see out array1
	for(int a=0; a<10; a++)
	{
		cout<<array1[a]<< " ";
	}

	cout<<endl;

	//see out array2
	for(int b=0; b<10; b++)
	{
		cout<<array2[b]<< " ";
	}
	return 0;
}
closed account (j3Rz8vqX)
You were suppose to ask the user for two names.
Write a program which will get 10 random numbers onto one array1 of size 10. Get 10 more random numbers and place them in array2 of size 10
You will compare the numbers for each person and whoever has the bigger number wins. You will compare the same element in the array for each player, so comparing element 0 from array1 with element 0 from array2. Keep count of how many wins and display the persons name of the one who won.
Tip:
in case of a tie, keep rolling a random number for each player and compare to see who wins...they may tie again so keep going till someone wins.


Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//...
        cout<<"Round["<<i+1<<"]: "<<array1[i]<<" to "<<array2[i]<<" = ";

        if(array1[i]>array2[i])
        {
            ++count1;
            cout<<player1<<" won the round!"<<endl;
        }
        else if(array1[i]<array2[i])
        {
            ++count2;
            cout<<player2<<" won the round!"<<endl;
        }
        else
        {
            --i;//Redo this test.
            cout<<"There was a tie, Rematch!"<<endl;
        }
//...
Please enter player ones name: Bob

Please enter player twos name: Steve

The winner per element:
Round[1]: 4 to 3 = Bob won the round!
Round[2]: 7 to 10 = Steve won the round!
Round[3]: 3 to 10 = Steve won the round!
Round[4]: 4 to 6 = Steve won the round!
Round[5]: 2 to 10 = Steve won the round!
Round[6]: 3 to 9 = Steve won the round!
Round[7]: 2 to 9 = Steve won the round!
Round[8]: 3 to 1 = Bob won the round!
Round[9]: 9 to 2 = Bob won the round!
Round[10]: 6 to 4 = Bob won the round!

Bob won 4 times.
Steve won 6 times.

The winner is Steve

Process returned 0 (0x0)   execution time : 4.228 s
Press any key to continue.
Keep count of how many wins and display the persons name of the one who won. * in case of a tie, ...

To me that is a bit ambiguous. Do we want an overall winner or a winner for each element/round?

If the overall is all that is required, then max(count1,count2) is all that is interesting and single elements can tie. We thus roll 10 pairs and then an unknown number of additional pairs, if the first 10 sum up to a tie.

If the overall is not required to win, then we can limit to creating 10 pairs (rounds), where each has a winner (and show each). Dput's loop does that. This can result in overall tie (count1==count2==5).


In principle, one could enforce that both each round has a winner and that the whole match has a winner. It gets real tricky if the number of reported rounds must remain 10.
Last edited on
closed account (j3Rz8vqX)
Yes, as kesk said, you need to ask your professor: what to do in an even that both players have the same number of wins.

Personally, I would:

Prompt that there were no winners, because there weren't.
OR
Have a "death match", because a winner is necessary.
OR
do-A-Work-Around and Display the first person to achieve at least 5 wins.

None of which may be a part of this programs design.
in case of a tie, keep rolling a random number for each player and compare to see who wins...they may tie again so keep going till someone wins

But if we assume that the continuous combating of numbers, refer to round wins and absolute win, then a "death match" may fit the description.

Ask your professor for clarity and assurance.
Last edited on
Topic archived. No new replies allowed.