Using Arrays on Wargame

I'm making a WarGame program (a game where you use cards). Anyways I pretty much have the whole main function but I am stuck on two of the functions needed for my header file.
Comments are included to guide on the functions but I just don't know how to start the last two functions that I have.

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

#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<time.h>

using namespace std;

//
// Generate random numbers in the range between one and the given range
// to fill an array
//
// DO NOT MODIFY THIS FUNCTION
//
void getNums(int arr[], int range, int size)
{
    int i; // A counter
    
    for( i = 0; i < size; i++ )
    {
        // Add a random number within the specified range to the array
        arr[i] = rand() % range + 1;
    }
}


//
// Display the cards each player pulled and the number of cards they have left
//
// DO NOT MODIFY THIS FUNCTION
//
void showCard(int hand_one[], int hand_two[], int one, int two)
{
    cout << "*****Number of cards in each player's deck*****\n";
    cout << setw(15) << "You: " << one;
    cout << setw(20) << "Computer: " << two << endl;
    cout << "You have the card: " << hand_one[0] << endl;
    cout << "The computer has the card: " << hand_two[0] << endl;
}


//
// Split the elements of a source array into two destination arrays
// The given size is the size of the destination arrays (they are the same size)
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
void splitArray(int dest_one[], int dest_two[], int source[], int size)
{
    int ctr = 0; // Counter that will go through the source[] array.
    int i; // A counter

    // Loop through the destination arrays.
    // Assign each element in a destination array the next element
    // in the source array. Use the ctr variable to keep track of the
    // current position in the source array.
    
    
    
    
}


//
// Shifts the items in an array one place to the left
// Throws out the first element in the array and sets the last element
// in the array to zero.
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
void shift(int arr[], int size)
{
    int i; // A counter
    
	
    // shift the array
    

    
    // Set the last element in the array to zero


}


Any help whatsoever involving how to start the last two functions will be very helpful. If I do leave out anything that seems to be a problem or complicates things let me know.
void splitArray may loop through all items in source array and determine if current index is odd or even and send them to the right dest array. You could either assume source array size is equal to 2* size or pass it as another argument to make the function less error prone. Please make your version without such assumption, becasue assumptions are bad.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int sourceSize = 2*size
// This assumes the sourceSize, but if there is
// not enough elements in source, the program will crash.
for (int i = 0; i < sourceSize; i++){
{

    int destIndex = (i+1) / 2; // (+1 because int rounds halves down)
    if(i%2) // for odd i (1,3,5...)
    {
         dest_one[destIndex] = source[i];
    }
     else // for odd i (0,2,4)
     {
         dest_two[destIndex] = source[i];
     }
}

Or the other way - loop through all items in destinations and assign them value from the right source array index:
1
2
3
4
5
for(int i = 0; i  < size; i++)
{
    dest_one[i] = source [ i*2 ];
    dest_two[i] = source [ i*2 +1 ];
}

Note, the same source size limitations apply.

The other funtion is easier. For each index i in array make the element at i equal to the element at i + 1 as long as i+1 is less than size. Then set element at index size-1 to 0. That is one line of code inside for loop + one line outside of loop.
Last edited on
Topic archived. No new replies allowed.