Obiectives 1. To learn how to use 2 dimensional arrays to store and retrieve dat
ID: 3727157 • Letter: O
Question
Obiectives 1. To learn how to use 2 dimensional arrays to store and retrieve data to help solving problems. Movies about dragons and dragon training were very popular this summer. Your friend has not stopperd talking about how awesome dragons are and how cool it would be to train them. To amuse your friend you have decided to create a series of programs about dragons Now that your dragon has participated in the Celebration of the First Flight, they are a fully recognized dragon! You've just found out that several of your friends just had their dragons pass their Celebration too. Now it's a race to see which of you will become the first dragon rider. To start riding a dragon, you need the following items: 0) Fleece Blanket 1) Saddle 2) Leather Jacket 3) Flight Harness 4) Goggles 5) Helmet At night, you and your friends will try to sneak out and obtain these pieces from the Dragon Island equipment shed. Each of you will take turns trying to gain a piece of equipment, simulated by a randomly generated number from O to 6. Six of those numbers (0-5) correspond to the numbers above, while 6 corresponds to "You've been caught! Lost a piece of equipment If you get a number that corresponds to an item you already have, nothing happens. If you get a saddle (item 1), you may only pick up the item if you already have a blanket (item O). If you get a flight harness (item 3), you may only pick up the item if you already have a jacket (item 2). This is because you cannot put a saddle on your dragon without the blanket underneath and you cannot put a flight harness orn yourself without a jacket underneath. If you land on "You've been caughti", then you must choose which piece of equipment to discard. If you have no pieces to begin with, then you don't lose anything. If you have a saddle(1), then you cannot discard the blanket(0) and if you have a harness(3), then you cannot discard the jacket(2) The first person to obtain all six items will become the first dragonrider You must use a 2D array to solve the problem. There will be no more than 6 people trying to "borrow equipment. You may wish to use these data structures to help you solve the problem int cont = 1; int riders[6l(6): char ITEMS6][20] = {"BLANKET", "SADOLE", "JACKET", "HARNESS", "GOGGLES", "HELMET"); For example, to print "SADDLE-we could The values in-ITEMS" are strings and may be printed with %s. use print"%s". ITEMS[ 1]);
Explanation / Answer
This is a simple program. Here all the details of the code are given in the question itself. The implementation details are also given in the question.
Algorithm:
initially, you need to create a riders 2d array which stores the information about each rider’s items. store the item details in items array. Initialize flag =0
For flag ==0 and each turn of rider:
Generate a random number using rand()
if number generated is less than 6
then check for the conditions given in the question and update the rider’s array
else
then ask rider for the item to discard
update the rider’s array accordingly
check if the user has achieved all the items.. then update the flag, flag=1
print the details of the rider that has achieved all the items.
Implementation in C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
//n for taking number of riders
//riders for storing the information what each rider contains, intially it zero
//rturn which we will use it for looping
//rnum is getting the random value
int n,riders[6][6]={0},rturn=0,temp,rnum,flag=0,cnt,last;
//store the details in items
char items[6][20] ={"BLANKET","SADDLE","JACKET","HARNESS","GOGGLES","HELMET"};
//Take input from user
printf("How many will be trying to gain the equipment? ");
scanf("%d",&n);
//check if flag is zero
//flag =1 when one rider gets all items
while(flag==0){
//scan for 0
printf("Rider %d, it is your turn. Enter 0 to look for an item. ", rturn+1);
scanf("%d",&temp);
//generate random number
rnum = rand()%7;
//check if it one of the item or caught
if(rnum!=6){
printf("Rider %d, you have found %s ",rturn+1,items[rnum]);
//check if user has the item
if(riders[rturn][rnum]==1){
printf("You already have that item ");
}else if(rnum == 4 || rnum == 5 || rnum==0 || rnum==2){ //for 0,2,4,5 there are no pre requisities
printf("Congrats, you now have an %s ",items[rnum] );
riders[rturn][rnum] = 1; //update hte riders array
}else if(riders[rturn][rnum-1]==1){ //check if it has the pre requisities
printf("Congrats, you now have an %s ",items[rnum] );
riders[rturn][rnum] = 1; //update the riders array
}else if(riders[rturn][rnum-1]==0){
printf("Sorry, you can't obtain %s ",items[rnum] );
}
}else{
//if the riders 6 as random number
int check = 0;
printf("You've been caught ");
//check if the rider has anything to lose
//just check if rider has atleast one item
for(int i=0;i<6;i++)
if(riders[rturn][i]==1) //check if rider has that item
check=1;
//check =1 : rider has any item
if(check==1){
//print all the items the rider has
printf("Which piece would you like to lose? ");
if(riders[rturn][1]==1) printf("1 %s ",items[1]);
else if(riders[rturn][0]==1) printf("0 %s ",items[0] );
if(riders[rturn][3]==1) printf("3 %s ",items[3] );
else if(riders[rturn][2]) printf("2 %s ",items[2] );
if(riders[rturn][4]==1) printf("4 %s ",items[4] );
if(riders[rturn][5]==1) printf("5 %s ",items[5] );
//scan for the item that rider want to loose
scanf("%d",&rnum);
riders[rturn][rnum] = 0; //update the rider array
printf("You have selected %s to discard ",items[rnum] );
}else{
//if rider has no items to loose
printf("Lucky you, sort of. ");
}
}
//print the current items that rider have
printf("Ride %d, here is what you currently have ", rturn+1);
for(int i=0;i<6;i++){
if(riders[rturn][i]==1) //check if rider has that item
printf("%d %s ", i, items[i]);
}
cnt=0;
//check if this rider has achieved all the items
for(int j=0;j<6;j++)
if(riders[rturn][j]==1) cnt++;
//if so then update the flag variable
if(cnt==6) flag=1;
//store the current rider in variable
last = rturn;
//get the next rider
rturn++;
rturn=rturn%n;
}
//last rider contains the rider that has achieved
//print the rider
printf("Congratulations, Rider %d, you are the first dragon rider! ",last+1 );
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.