You have to complete the first part of this lab with no brackets ([ and ]). You
ID: 3671848 • Letter: Y
Question
You have to complete the first part of this lab with no brackets ([ and ]). You can use exactly 4 brackets in the second part.
A Sample:
FIRST ROUND:
ints : -10, -10, -10
counter : 1
index : 0
ints: 1, -10, -10
NEXT ROUND:
random move (up)
index: 1
counter: 2
ints: 1, 2, -10
NEXT ROUND:
random move (down)
index: 0
counter: 3
ints: 3, 2, -10
NEXT ROUND:
random move (down)
index: 2
counter: 4
ints: 3, 2, 4
END PROGRAM
Display 3, 2, 4
Random Walking in Space Allocate enough memory for 10 ints. (use malloc and free) Initialize the 10 ints to the value -10 . Make a counter set to 1 . Make an index variable Make an index variable. . Let the index randomly walk up or down. If it makes it to -1 reset it to 9, if it makes it to 10 reset it to 0. Increment the counter at each step and save that value into memory at spot inde:x. When all spaces have a positive number then display the ten values, free the memory and exit the program Version 2: Copy paste that code, and change malloc and free to new and delete (you can use 4 brackets for this).Explanation / Answer
VERSION 1:
int main(){
int i, *ptr_ten, index=0, counter=1;
*ptr_ten= (int *)malloc(10 *sizeof(int));
for(i=0; i<10; i++){
ptr_ten + i= -10;
}
while(*ptr_ten != -10){
*ptr_ten+index=counter;
counter++;
int random_index(index);
}
display(*ptr_ten);
free(ptr_ten);
return 0;
}
int random_index( int i){
if(i==-1)
i=9;
else if(i== 10)
i=0;
else
return rand(i++, i--);
}
void display(int *p){
for(int i=0; i<10; i++)
printf("%d", *(p+i));
}
VERSION 2:
int main(){
int i, *ptr_ten, index=0, counter=1;
*ptr_ten= new int[10];
for(i=0; i<10; i++){
ptr_ten + i= -10;
}
while(*ptr_ten != -10){
*ptr_ten+index=counter;
counter++;
int random_index(index);
}
display(*ptr_ten);
delete[] ptr_ten;
return 0;
}
int random_index( int i){
if(i==-1)
i=9;
else if(i== 10)
i=0;
else
return rand(i++, i--);
}
void display(int *p){
for(int i=0; i<10; i++)
printf("%d", *(p+i));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.