Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1010: Programming Methodology

Similar presentations


Presentation on theme: "CS1010: Programming Methodology"— Presentation transcript:

1 CS1010: Programming Methodology http://www.comp.nus.edu.sg/~cs1010/ http://www.comp.nus.edu.sg/~cs1010/

2 CS1010 (AY2012/3 Semester 1)Week13 - 2 AY2010/11 Semester 1 Q1.3 void foo(char *fname){ FILE *fp; int c; int count = 0; if ((fp = fopen(fname, "r")) == NULL) return; while (((c = fgetc(fp)) != EOF) && (count < 3)) { if (c == '\n') count++; putchar(c); } fclose(fp); } If dat.txt is a text file containing 100 characters, which of the following statements is the most appropriate about the output of foo("dat.txt")? A.The output produced by foo contains two characters. B.The output produced by foo contains three characters. C.The output produced by foo contains two newline characters. D.The output produced by foo contains three newline characters. E.None of the above. abcde fghijklm nop qrstu... abcdefghijk...

3 CS1010 (AY2012/3 Semester 1)Week13 - 3 AY2010/11 Semester 1 Q2(b) #include int functionXYZ(char *, char); int main(void) { char s[] = "abbacadaba"; printf("%d\n", functionXYZ(s, 'b')); return 0; } int functionXYZ(char *str, char ch) { int i=0, j=0; while (str[i]) if (str[i++] == ch) j++; return j; } abbacadaba\0 s str 3 Output:

4 CS1010 (AY2012/3 Semester 1)Week13 - 4 AY2010/11 Semester 1 Q2(c) #include void swap(int []); int main(void) { int a[3] = {1, 2, 3}; swap(a); printf("%d %d %d\n", a[0], a[1], a[2]); swap(&a[1]); printf("%d %d %d\n", a[0], a[1], a[2]); return 0; } void swap(int x[]){ int temp = x[0]; x[0] = x[1]; x[1] = temp; } 2 1 3 Output: 2 3 1 123 a x Swap x[0] with x[1] = Swap a[0] with a[1] 2131 x Swap x[0] with x[1] = Swap a[1] with a[2]

5 CS1010 (AY2012/3 Semester 1)Week13 - 5 AY2010/11 Semester 1 Q6 (1/2) Given RxC integer array M, find maximum number of pairs of consecutive elements within the same row or column. Assume elements are integers between 0 and 9 inclusive. Write a function to return the maximum number of pairs of the same value in the array int max_pairs(int mat[][C]) Example: 4x4 array 8112 5510 4216 1882 0123 0 1 2 3 Function returns 3

6 CS1010 (AY2012/3 Semester 1)Week13 - 6 AY2010/11 Semester 1 Q6 (2/2) 8112 5510 4216 1882 0123 0 1 2 3 #define ROWS 100 #define COLS 100 int max_pairs(int mat[][COLS]) { } int i, j, value, max; int count[10] = {0}; for (i=0; i max) max = count[i]; return max; 0000 0000 00 0123456789 311

7 CS1010 (AY2012/3 Semester 1)Week13 - 7 AY2010/11 Semester 1 Q8 (1/5) #include #define DECK_SIZE 52 // define your card_t structure // here // function prototypes void initDeck(card_t []); void shuffleDeck(card_t []); void printDeck(card_t []); int computePoints(card_t []); int main(void) { // declare your deck of cards here // initialize the deck of cards initDeck(deck); printf("The original deck:\n"); printDeck(deck); // shuffle the deck shuffleDeck(deck); // print the deck of cards printf("\nThe shuffled deck:\n"); printDeck(deck); printf("\nPlayer's points = %d\n", computePoints(deck)); return 0; } typedef struct { char rank; char suit; } card_t; card_t deck[DECK_SIZE];

8 CS1010 (AY2012/3 Semester 1)Week13 - 8 AY2010/11 Semester 1 Q8 (2/5) // printDeck function given; for your reference only void printDeck(card_t deck[]) { int loop; // print the deck one card at a time for (loop = 0; loop < DECK_SIZE; loop++) { if (loop !=0 && loop % 13 == 0) printf("\n"); printf("%c%c ", deck[loop].suit, deck[loop].rank); } printf("\n"); } printDeck function given

9 CS1010 (AY2012/3 Semester 1)Week13 - 9 AY2010/11 Semester 1 Q8 (3/5) void initDeck(card_t deck[]) { } Write a function initDeck to initialize the deck int loop; char ranks[] = {'2','3','4','5','6','7','8','9', 'T','J','Q','K','A'}; char suits[] = {'C','D','H','S'}; // assign initial values to the deck of cards for (loop = 0; loop < DECK_SIZE; loop++) { deck[loop].rank = ranks[loop % 13]; deck[loop].suit = suits[loop / 13]; } C2 C3 C4 C5 C6 C7 C8 C9 CT CJ CQ CK CA D2 D3 D4 D5 D6 D7 D8 D9 DT DJ DQ DK DA H2 H3 H4 H5 H6 H7 H8 H9 HT HJ HQ HK HA S2 S3 S4 S5 S6 S7 S8 S9 ST SJ SQ SK SA

10 CS1010 (AY2012/3 Semester 1)Week13 - 10 AY2010/11 Semester 1 Q8 (4/5) void shuffleDeck(card_t deck[]) { } Write a function shuffleDeck to shuffle the deck Write aloop with 52 iterations. At iteration i we generate a random integer r in the range [0,52] and swap deck[r] with deck[i]. int loop, randNum; card_t temp; srand(time(NULL)); for (loop = 0; loop < DECK_SIZE; loop++) { randNum = rand() % DECK_SIZE; // swap the card at the current position with // the one at the randomly selected position temp = deck[loop]; deck[loop] = deck[randNum]; deck[randNum] = temp; }

11 CS1010 (AY2012/3 Semester 1)Week13 - 11 AY2010/11 Semester 1 Q8 (5/5) int computePoints(card_t deck[]) { Write a function computePoints to determine the total point of 13 cards given to a player no-card suit: 3 pts; 1-card suit: 2 pts; 2-card suit: 1 pt int loop, suit, points = 0, counts[4] = { 0 }; for (loop = 0; loop < 13; loop++) switch (deck[loop].suit) { case 'C': counts[0]++; break; case 'D': counts[1]++; break; case 'H': counts[2]++; break; case 'S': counts[3]++; break; } } for (suit = 0; suit < 4; suit++) if (counts[suit] == 0) points += 3; else if (counts[suit] == 1) points += 2; else if (counts[suit] == 2) points++; return points; D9 C2 H9 S6 C8 D3 H2 DJ S3 D7 S8 H3 HQ 4 diamonds, 4 hearts, 3 spades no point 2 clubs 1 point

12 End of File


Download ppt "CS1010: Programming Methodology"

Similar presentations


Ads by Google