Need help debugging 2 codes in C. There are test cases providing
to help
Define a function addToPile with the following prototype: cardT *addToPile(cardT *pile, cardt *card); It should place card at the top of pile., and return the new head of the pile. If card is NULL, do nothing and return the existing head. Answer: (penalty regime: 0 %) 1 cardt *addToPile(cardt *pile, cardt *card) { 2 if(card == NULL) { 3 return pile; } 5 else{ pile->next = card; return pile; } 4 9 Check Test Expected Got X x cardt *pile = addToPile(makePile("35409C"), makeCard(9,'S')); 95 9C 4D 35 90 95 showPile(pile); X X cardT *pile = addToPile(makePile("354D9C"), makeCard(2,'C')); 20 9C 4D 35 90 20 showPile(pile); x 11D x cardT *pile = addToPile(NULL, makeCard(11, 'D')); showPile(pile); ***Run error*** Segmentation fault (core dumped)
Define a function hitOrstand that takes no argument. It should have the user enter a word. If the word starts with 'h' (upper or lower case), return 'h'. If the word starts with 's' (upper or lower case), return 's'. Otherwise it should ask again until an acceptable response is entered. Note: the user shouldn't be told what to enter. Of course, for a "real" program you'd want to actually print a prompt. We can assume the auto-grader knows. Answer: (penalty regime: 0 %) 1 char hitOrStand(void) { 2. char letter[100]; 3 3 scanf("%s", letter); 4 if(letter[@]=='h'||letter[@]=='H') 5 { 6 return 'h'; 7 } 8 else if(letter[@]=='s'||letter[@]=='5') 9 { 10 return 's'; 11 } 12 else { 14 scanf("%s", letter); 15 } 16 return ; 17 ) 13 Check Test Input Expected Got ✓ printf("%c\n", hitorStand()); h h h h h printf("%c\n", hitor Stand()); s s 5 printf("%c\n", hitorStand(); Hit h h printf("%c\n", hitorStand()); Stttaand s 5 x X S S \x00 x printf("%c\n", hitorStand(); chit go on standing x X h \x00 X printf("%c\n", hitorStand(); First 2econd third Hello stand
Need help debugging 2 codes in C. There are test cases providing to help
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am