DOC PREVIEW
UIUC ECE 190 - Programming Studio

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming Studio #10 ECE 190Programming Studio #10 • Topics this week: • File I/O • Recursion • Maze Walk ExampleAnnouncements • Exam 3 – Review session 3-5 pm Sat. in Everitt 151 – Check Compass tonight for room assignment • Exam 2 – Written regrade requests due in the ECE 190 dropbox by 5pm TODAY (Friday 4/8) – Programming regrade request due by 5pm on April 15 (check your email tonight for more info) • MP4 – MP4.1 feedback: early next week – MP4.2 due Thursday 5pmOpening a File #include <stdio.h> FILE *file; file = fopen(char * filename, char * mode); Declare file pointer if fopen fails, it returns NULL Modes Can Read Can Write File must exist Clears File/ Creates File “r” Y N Y N “w” N Y N Y “a” Y Y N N “r+” Y Y Y N “w+” Y Y N Y • “a”, append always writes data to end of file (never overwrites) • File must exist means that if the file doesnʼt exist, fopen returns NULL • Clears file/Creates file • If file exists, ALL DATA IS REMOVED, an empty file WILL be createdWhere does it Read/Write? Files are read/written to current working directory unless a path is specified When you open a file in “r” or “w” mode, read/ write starts from beginning of file (offset zero) When you open a file in “a” mode, write starts from end of file When reading or writing, the offset is incremented based on the size of the read or write When writing to file, data is overwritten if it previously existed.Basic File Read/Write • Works like standard I/O • scanf -> fscanf, printf -> fprintf Write to file Read from file FILE * file = fopen(“file.txt”, “w”); int first_int = 1; fprintf(file, “%d”, first_int); FILE * file = fopen(“file.txt”, “r”); int first_int; fscanf(file, “%d”, &first_int); printf(“The first number is %d”, first_int); “file.txt” before write “file.txt” after write 8 10 67 100 1 Console prints: The first number is 1Close File and End of File Example: int nums[100]; FILE* file = fopen(“list.txt”, “r”); if(file != NULL) { int i; for(i = 0; (i < 100) && (!feof(file)); i++) fscanf(file, “%d”, &nums[i]); for(; i < 100; i++) nums[i] = 0; fclose(file); } Check end of file Close file int feof(FILE *file) fclose(FILE *file) returns non-zero when the end of the file has been reached returns zero if the file is successfully closedFunctions to know for the exam • fopen, fclose • feof (and how it relates to EOF) • fprintf, fscanf • fputs, fgets, fputc, fgetc • fread, fwriteRecursion • A function calls itself • Pros: – Better solution for some problems • Searching – Fewer lines of code and elegant logic • Cons: – Not an easy programming concept – Memory overhead (stack frames) – Longer execution timeHow to Write a Recursion • Composition – Base case: the termination condition – Recursive step: the reduction case • Implementing a recursion is easy – Recall computing factorial, binary search, etc. • Designing a recursion algorithm is not easy – Identify recursive patternsExample – Maze Walk • Find if one can exit a maze from a chosen position • Download the file maze.c from the website • Hints: – If a position (x,y) can exit the maze, then nearby positions (x±1 or y±1), if valid and accessible from (x,y), can also exit the maze – Each move is a recursive step – Remember visited places to avoid infinite loops exitBasic Algorithm • Terminal cases – If maze[i][j] = 2, exit found; return 1 – If maze[i][j] = 1, wall hit; return 0 • Reduction cases – Return 1 if any valid, unvisited neighbors can exit the


View Full Document
Download Programming Studio
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Studio and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Studio 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?