110 likes | 256 Views
CISC 130: Today’s Class. Recap Drawing in 2 dimensions Vertical Histogram Working with Files. Recap. Collect Papers Review: Our Zeroes Review: Function Declaration Arrays and Side Effects Example: counting digits typed. Sample “drawing” function.
E N D
CISC 130: Today’s Class • Recap • Drawing in 2 dimensions • Vertical Histogram • Working with Files R. Smith - University of St Thomas - Minnesota
Recap • Collect Papers • Review: Our Zeroes • Review: Function Declaration • Arrays and Side Effects • Example: counting digits typed R. Smith - University of St Thomas - Minnesota
Sample “drawing” function • Draw right triangles with nested for loops • How does this ‘really’ work? • We have a 2D image • We can say mathematically which * we should print • Most printing involves horizontal ‘scanning’ • The program looks at blanks from left to right • For each blank, it decides to fill it in or not R. Smith - University of St Thomas - Minnesota
Horizontal vs Vertical Histogram • Horizontal: • The domain of the answer appears vertically • Domain = the array index • Example: the length of a word • We don’t have to worry about domain’s size • It grows downwards as much as it needs to • The range appears horizontally • Range = content of the array element • Example: number of words of a given length • Range can be as long as the line width – long enough here • Vertical: • Domain is restricted by the line’s width • Range is restricted by the histogram’s height R. Smith - University of St Thomas - Minnesota
Drawing the Histogram • Think in terms of domain and range • If we print out the contents of an array • Domain = index values to choose individual array elements • Range = range of values stored in individual array elements • Horizontal Histogram • Domain = index values for the array • Range = series of markers printed from left to right • Implementation: Print value in domain, then markers within range • Vertical histogram • Domain maximum = # of elements in the array (less that display) • Maximum range = # of lines of histogram we draw • Implementation: print horizontally, filling in upper ranges first • We’ll look at this later R. Smith - University of St Thomas - Minnesota
The Vertical Histogram • We must plan the layout ahead of time • How wide can it be? (acceptable domain) • How high can it be? (acceptable range) • Example: displaying a small array • #define DOM 8 • #define RANGE 10 • intvh[DOM] = {9, 0, 3, 7, 6, 5, 4, 6}; • How do we do this by drawing horizontally? • “Raster scan” concept • Let’s try it. – tell me what to do!
Working with files • Instead of getchar(), putchar(), printf() • Use getc(), putc(), fprintf() • “FILE” type variable with weird syntax • Function fopen() opens a file • First argument: string with file name • Second argument: “r” for read, “w” for write • Result: “FILE pointer” if success, else “NULL” literal value • Function fclose(fp) called when done R. Smith - University of St Thomas - Minnesota
Printing a file to the console FILE *fp; // file variable intch; // input character fp = fopen(“input.txt”, “r”); if (fp == NULL) printf(“Open failed! \n”); else { ch = getc(fp); while(ch != EOF) { putchar(ch); ch = getc(fp); } fclose(fp); } R. Smith - University of St Thomas - Minnesota
The getfline() function • This is like getline() but with a third argument: • line – the char array to receive the input text • len – the maximum length of the ‘line’ char array • fp – file variable “pointer” • Let’s write a ‘cat’ function – prints a file. • Prompt for the file’s name • Read in the file’s name • Now, open the file and read its contents • Problems: • Are we handling a ‘missing file’ error? • How do we handle ‘end of file’? R. Smith - University of St Thomas - Minnesota
Reading and writing a file • Create a file of names and numbers • First, let’s write a loop to read and print the file • Next, add operations to copy contents to another file R. Smith - University of St Thomas - Minnesota
Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. R. Smith - University of St Thomas - Minnesota