there is a game that this program needs to play X-boom its supposed to take out the x player each turn and put it in the back of the array and then shorten the array so its no longer a part of it in that function. for example
player 1 2 3 4 and the x for the game is 3
players remaining: 1 2 4
players remaining: 1 4
players remaining: 1
i made this code that i think should do it but im not sure why its not working properly
i have a photo to how the output is supposed to look like but i dont know how to share it here if any one knows please tell me in the comments my problem is mostly with the play function notice that is number 3 in the array was kicked you are supposed to keep on playing from number 3 just that there will be a new pointer there
i tried to solve this be just going to the one that needed to be kicked and doing a swap for it to go back while moving everyone else forward and then shortening the array by 1
in the end the main function will print the array and it needs to be in order that the first player to be kicked will be last in the array
void get_boom_number(int * i){
printf("enter the boom number\n");
scanf("%d",i);
return ;
}
#define LENGTH 31
void play(char * players[],int length,int boomnum){
int count=-1;
count = (count+boomnum-1)%(length);
char * temp;
for (int i = 0; i<30; i++) {
printf("%d\n",count);
for (int start=count; start<length-1; start++) {
temp=*(players+start);
*(players+start)=*(players+start+1);
*(players+start+1)=temp;
}
count=(count+boomnum-1)%(length);
length=length-1;
}
return ;
}
int main()
{
char * players[LENGTH]={"Tyrion Lannister","Daenerys Targaryen","Jon Snow","Arya Stark","Theon Greyjoy", "Joffrey Baratheon","Khal Drogo","Ted Mosby","Marshall Eriksen","Robin Scherbatsky","Barney Stinson", "Lily Aldrin", "Tracy McConnell", "Ted Mosby", "Leonard Hofstadter","Sheldon Cooper", "Penny", "Howard Wolowitz", "Raj Koothrappali", "Bernadette Rostenkowski-Wolowitz","Amy Farrah Fowler", "Gregory House", "Lisa Cuddy", "James Wilson","Eric Foreman", "Allison Cameron", "Robert Chase" ,"Lawrence Kutner", "Chris Taub","Remy 13 Hadley", "Amber Volakis"};
int boom_number, i;
get_boom_number(&boom_number);
play(players,LENGTH,boom_number);
and then just printing the array also we are not alowed to use "[]" for the hole thing.
p
and indexi
, the expression*(p + i)
is exactly equal top[i]
. The latter (p[i]
) is usually easier to read and understand (and also a couple or characters less to write). Please consider this when you do e.g.*(players+start)
.players
is a pointer, what syntax you choose to use for dereferencing it should not matter. Pointer dereference is pointer dereference either way.