Thread: New Smaller and Smaller Arrays.

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    New Smaller and Smaller Arrays.

    I want to make a card game, where you pick a card, and then, you pick another card from the cards that are left. I started by making an array with another smaller array where the numbers(later cards) are assigned to it using the numbers(later cards) that are left.

    My questions are this?
    1.) does this program compile and run on other people's systems, it seems to work okay on mine?

    2) How come it seems like C just makes the second array longer? Is that going to cause me problems down the road when I put the cards and more stuff in the program?

    3) I've read things about some card shuffling algorithms. I've read some of the more popular ones are really bad and not random enough cause some cards end up being picked more than others. One I found, I tried using 5 cards and a sheet of paper, and found it was just as bad as it said. I figure with my algorithm of just picking a card, then another card, then another card randomly, it should always be random.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    int main (){
    int tablestu [] = { 10, 20, 30, 40, 50};
    int table2 [4];
    /*for (int i= 0; i< 5; i++) */
    int i = 0;
    
    while (i < 5 ){
    printf("Hi, %d hello %d \n", i, tablestu[i]);
    i++;
    }
    
    unsigned int iseed = (unsigned int)time(NULL);
    srand(iseed);
     int genme = (rand() % 5 );
    
    printf("Int genme %d \n",genme);
    
    int h = 0;
    while( h < 5){
    if (h = tablestu[genme])
    printf("this is the card %d \n",tablestu[genme]);
    h++;
    }
    
    
    
    int k = 0;
    while (k < 5) {
    if (k == genme)
    {
    table2[k] = tablestu[k+1];
    }
    else if ( k < genme)
    {
    table2[k] = tablestu[k];
    }
    else {
    table2[k] = tablestu[k+1];
    }
    k++;
    }
    int j = 0;
    
    while (j < 5 ){
    printf("Hi, %d hello2 %d \n", j, table2[j]);
    j++;
    }
    int m = 0;
    
    while (m < 5 ){
    printf("Hi, %d hellostu %d \n", m, tablestu[m]);
    m++;
    }
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by jastiv View Post
    1.) does this program compile and run on other people's systems, it seems to work okay on mine?
    Turn on compiler warnings and it will tell you about problems with line 25 (see below)

    2) How come it seems like C just makes the second array longer? Is that going to cause me problems down the road when I put the cards and more stuff in the program?
    Your array indices are out of bounds and thus undefined behaviour.

    Code:
    int tablestu [] = { 10, 20, 30, 40, 50};
    ...
    while( h < 5){
    if (h = tablestu[genme])
    printf("this is the card %d \n",tablestu[genme]);
    h++;
    }
    This only seems to work because in your case h is always bigger than 5 after the first iteration. The problem is your if-statement:
    Code:
    if (h = tablestu[genme])
    will assign the value of tablestu[genme] to h, than checks if it is true (it is because all values in tablestu are not zero), prints the line and then exits the while loop because the smallest value in the array is 10, so h is always bigger than 5.
    You probably wanted to write
    Code:
    if (h == tablestu[genme])
    but as you have (unintentionally) shown the whole while-loop is unnecessary.

    Code:
    int table2 [4];
    ...
    while (k < 5) {
    if (k == genme)
    {
    table2[k] = tablestu[k+1];
    }
    else if ( k < genme)
    {
    table2[k] = tablestu[k];
    }
    else {
    table2[k] = tablestu[k+1];
    }
    k++;
    }
    When k=4 you are out of bounds for table2 (and in two out of three cases also for tablestu).

    Code:
    while (j < 5 ){
    printf("Hi, %d hello2 %d \n", j, table2[j]);
    j++;
    }
    Same again, 4 isn't a legal index for table2.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make something smaller.
    By Yuri in forum C++ Programming
    Replies: 11
    Last Post: 09-29-2005, 02:19 PM
  2. Smaller File Size?
    By exoeight in forum C Programming
    Replies: 3
    Last Post: 03-31-2005, 12:04 PM
  3. Smaller XBox
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 02-04-2003, 03:58 PM
  4. smaller compilers ?
    By black in forum C++ Programming
    Replies: 21
    Last Post: 05-16-2002, 12:13 AM
  5. how to cut a string into smaller strings?
    By ss3x in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2002, 10:32 PM

Tags for this Thread