Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming Go through this program. Trace the code for at least 5 iteration a

ID: 3846382 • Letter: C

Question

C programming

Go through this program. Trace the code for at least 5 iteration and give your comment on what this is doing ?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ( )
{

// card is an array with 24 cells, each cell is of type char pointer

// The strings are constant, the address of the first character
// is constant and can't be changed.

// we assign that address to the cells in the array
// This is similar to char *p = "Sample String" ;


char *card[24] = { "spade-one",
"spade-two",
"spade-three",
"spade-four",
"spade-five",
"spade-six",
"heart-one",
"heart-two",
"heart-three",
"heart-four",
"heart-five",
"heart-six",
"diamond-one",
"diamond-second",
"diamond-three",
"diamond-four",
"diamond-five",
"diamond-six",
"club-one",
"club-two",
"club-three",
"club-four",
"club-five",
"club-six" } ;

char *player1[6], *player2[6], *player3[6], *player4[6];

int i ;


// Question1: TRACE THIS CODE FOR AT LEAST FIVE ITERATIONS
// WHAT AM I DOING ?
// WHAT AM I SWAPPING , VALUES, ADDRESSES?
// GIVE AS MUCH AS DETAILS AS POSSIBLE
  
srand( time ( NULL ) );
for ( i = 0 ; i < 24 ; i++ )
{
char *temp ;
int cardNo = rand( ) % ( 24 - i ) ;
temp = card [ cardNo ] ;
card [ cardNo ] = card [ 24 - i - 1 ] ;
card [ 24 - i - 1 ] = temp ;
}



}

Explanation / Answer

First of all,


1. You have declared an array of characters --> It has strings value namely spade-one, spade-two etc.
2. Then you have declared char *player1[6], *player2[6], *player3[6], *player4[6] which are not used.
3. Then you have used srand(time(NULL)), this will give generate different random numbers based on time.
4) rand() % ( 24 - i ) will generate random numbers between 0 to ( 24 - i -1)
5) Then we are swapping the value present in that random generated position with 24 - i - 1 location

Thanks, let me know if there is anything. This is the summary of what program does.