CS1010: Programming Methodology

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

#include void main() { float x = 1.66, y = 1.75; printf(%f%f,ceil(x), floor(y)); }
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
CS1010: Programming Methodology
Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->
CSCE 3110 Data Structures & Algorithm Analysis
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
Programming In C++ Spring Semester 2013 Practice 1-3 Lectures Programming In C++, Lecture 3 By Umer Rana.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Lectures 10 & 11.
WEEK 12 Class Activities Lecturer’s slides.
UNIT 15 File Processing.
CS1010 Programming Methodology
WEEK 13 Class Activities Lecturer’s slides.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
UNIT 16 (Extra) Characters and Strings.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
Case studies over structure types and strings
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
Character Input and Output C and Data Structures Baojian Hua
Introduction to C Programming CE Lecture 13 Strings in C.
Character Input and Output
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
WEEK 8 Class Activities Lecturer’s slides.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
ECE Application Programming
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
CSC215 Lecture Input and Output.
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Strings Dr. Sajib Datta
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
File Input and Output.
Character Arrays char string1[] = “first”;
ETE 132 Lecture 8 By Munirul Haque.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Files Chapter 8.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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) Week13 - 10 10

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) Week13 - 11 11

CS1010 Programming Methodology End of File