-2

I am trying to fill an array of 52 with the numbers 0 - 12. Once it hits 12, it needs to go back to 0 - 12 again. You might have already guessed it's a deck of cards. My code is below and doesn't work. It prints 0 - 12 one time, but then prints the address of the array I believe for the remainder of the iterations left.

#include<iostream>
#include<string>
using namespace std;

int main()
{
int myArray[52];

for (int j = 0; j < 4; j++)
{
    for (int i = 0; i < 13; i++)
    {
        myArray[i] = i;
    }
}

for (int k = 0; k < 52; k++)
{
    cout << myArray[k] << endl;
}

//system("pause");
return 0;
}

Can someone please help me with this brain fart?

Randy C
  • 47
  • 1
  • 8
  • I'm sorry, but I did explain the desired out come, the error it was producing, the shortest code to reproduce the problem, and etc.. What specifically are you referring to? – Randy C Oct 03 '17 at 16:33
  • I am guessing that you mean I need to include the includes and main function in with the code sample? Because otherwise, it compiles just fine. – Randy C Oct 03 '17 at 19:55
  • 4
    Please get rid of the [system("pause")](http://www.cplusplus.com/forum/articles/11153/). It makes your program needlessly interactive so that it can't be used by other programs and, worse, you have no way to know what the `pause` command might do on other people's systems. My system might have a "pause" command that pauses the cooling system on its reactor. – David Schwartz Oct 03 '17 at 20:04
  • 1
    @RandyC Yes, a MCVE should compile as-is copy-pasted into an IDE. Thank you for fixing your sample. – tambre Oct 04 '17 at 04:41
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Oct 04 '17 at 04:41
  • 1
    @tambre Thanks for the clarification and link to the article on namespace. I always wondered why I kept seeing people saying not to use it. It makes perfect sense now. – Randy C Oct 05 '17 at 16:48

2 Answers2

3
int myints[52];

for (int idx = 0; idx < 52; idx++)
{
    myints[idx] = idx % 13;
}

Modulus of 13 will range from 0 to 12.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
2

You're indexing the same first 12 elements of the array in the inner loop for every iteration of the outer loop.

Try changing it to something like this

for (int j = 0; j < 4; j++)
{
    for (int i = 0; i < 13; i++)
    {
        myArray[i + 13 * j] = i;
    }
}
Aiden Deom
  • 916
  • 5
  • 11
  • Thank you, this does work, but could you please break down what is going on in each iteration? I want to understand this – Randy C Oct 03 '17 at 16:31
  • 1
    Look here, you want to fill the 52 places of an array with the numbers from (1-12).For that u need to access all the 52 places of the array. let's start for 1st loop where (j=0), for(i from 0 to <13) myArray[*], * will be (0+13*0)=0, likewise(1,2,.....,12). first loop ended when i became 13.For 2nd loop (j=1) again for (i from 0 to <13) myArray[*], * will be(0+13*1)=13 likewise (14,15,......, 24). For 3rd and 4th loop calculate like this in your writing pad, u will clearly understand it. – Linkon Oct 03 '17 at 16:39
  • Thanks, Linkon. I did exactly that, and you are right, it does make perfect sense now. – Randy C Oct 03 '17 at 16:56