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/

2 AY2010/11 Semester 1 Q1.3 void foo(char *fname){ abcde FILE *fp;
CS1010 Programming Methodology 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); abcde fghijklm nop qrstu... abcdefghijk... 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. CS1010 (AY2012/3 Semester 1) Week13 - 2 2

3 AY2010/11 Semester 1 Q2(b) 3 s str Output: #include <stdio.h>
CS1010 Programming Methodology AY2010/11 Semester 1 Q2(b) #include <stdio.h> 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; a b c d \0 s str Output: 3 CS1010 (AY2012/3 Semester 1) Week13 - 3 3

4 AY2010/11 Semester 1 Q2(c) 2 1 3 2 3 1 a x x Output:
CS1010 Programming Methodology AY2010/11 Semester 1 Q2(c) #include <stdio.h> 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]); return 0; } void swap(int x[]){ int temp = x[0]; x[0] = x[1]; x[1] = temp; a 2 1 1 3 2 1 3 Swap x[0] with x[1] = Swap a[0] with a[1] x x Swap x[0] with x[1] = Swap a[1] with a[2] Output: 2 1 3 2 3 1 CS1010 (AY2012/3 Semester 1) Week13 - 4 4

5 CS1010 Programming Methodology
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 8 1 2 5 4 6 3 Function returns 3 CS1010 (AY2012/3 Semester 1) Week13 - 5 5

6 CS1010 Programming Methodology
8 1 2 5 4 6 3 AY2010/11 Semester 1 Q6 (2/2) #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<ROWS; i++) { for (j=0; j<COLS; j++) { value = mat[i][j]; if ((j+1 < COLS) && (mat[i][j+1] == value)) count[value]++; if ((i+1 < ROWS) && (mat[i+1][j] == value)) } max = count[0]; for (i=1; i<10; i++) if (count[i] > max) max = count[i]; return max; 1 2 3 4 5 6 7 8 9 3 1 CS1010 (AY2012/3 Semester 1) Week13 - 6 6

7 AY2010/11 Semester 1 Q8 (1/5) card_t deck[DECK_SIZE]; typedef struct {
CS1010 Programming Methodology AY2010/11 Semester 1 Q8 (1/5) #include <stdio.h> #include <stdlib.h> #include <time.h> #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"); printf("\nPlayer's points = %d\n", computePoints(deck)); return 0; } card_t deck[DECK_SIZE]; typedef struct { char rank; char suit; } card_t; CS1010 (AY2012/3 Semester 1) Week13 - 7 7

8 AY2010/11 Semester 1 Q8 (2/5) printDeck function given
CS1010 Programming Methodology AY2010/11 Semester 1 Q8 (2/5) printDeck function given // 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); } CS1010 (AY2012/3 Semester 1) Week13 - 8 8

9 CS1010 Programming Methodology
AY2010/11 Semester 1 Q8 (3/5) Write a function initDeck to initialize the deck 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 void initDeck(card_t 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]; } CS1010 (AY2012/3 Semester 1) Week13 - 9 9

10 CS1010 Programming Methodology
AY2010/11 Semester 1 Q8 (4/5) 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]. void shuffleDeck(card_t deck[]) { } 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; } CS1010 (AY2012/3 Semester 1) Week 10

11 CS1010 Programming Methodology
AY2010/11 Semester 1 Q8 (5/5) 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 4 diamonds, 4 hearts, 3 spades  no point 2 clubs  1 point D9 C2 H9 S6 C8 D3 H2 DJ S3 D7 S8 H3 HQ int computePoints(card_t deck[]) { } 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; 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; } CS1010 (AY2012/3 Semester 1) Week 11

12 CS1010 Programming Methodology
End of File


Download ppt "CS1010: Programming Methodology"

Similar presentations


Ads by Google