Originally Posted by
jastiv
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