Presentation is loading. Please wait.

Presentation is loading. Please wait.

5/27/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical.

Similar presentations


Presentation on theme: "5/27/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical."— Presentation transcript:

1 5/27/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical Histogram Working with FilesWorking with Files

2 Recap Collect PapersCollect Papers Review: Our ZeroesReview: Our Zeroes Review: Function DeclarationReview: Function Declaration Arrays and Side EffectsArrays and Side Effects –Example: counting digits typed 5/27/2016 2R. Smith - University of St Thomas - Minnesota

3 Sample “drawing” function Draw right triangles with nested for loopsDraw right triangles with nested for loops How does this ‘really’ work?How does this ‘really’ work? –We have a 2D image –We can say mathematically which * we should print Most printing involves horizontal ‘scanning’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 5/27/2016 3R. Smith - University of St Thomas - Minnesota

4 Horizontal vs Vertical Histogram Horizontal:Horizontal: –The domain of the answer appears vertically Domain = the array indexDomain = the array index –Example: the length of a word We don’t have to worry about domain’s sizeWe 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 elementRange = content of the array element –Example: number of words of a given length Range can be as long as the line width – long enough hereRange can be as long as the line width – long enough here Vertical:Vertical: –Domain is restricted by the line’s width –Range is restricted by the histogram’s height 5/27/2016 4R. Smith - University of St Thomas - Minnesota

5 5/27/2016 5R. Smith - University of St Thomas - Minnesota Drawing the Histogram Think in terms of domain and rangeThink 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 HistogramHorizontal 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 histogramVertical 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

6 The Vertical Histogram We must plan the layout ahead of timeWe 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 arrayExample: displaying a small array –#define DOM 8 –#define RANGE 10 –int vh[DOM] = {9, 0, 3, 7, 6, 5, 4, 6}; How do we do this by drawing horizontally?How do we do this by drawing horizontally? –“Raster scan” concept Let’s try it. – tell me what to do!Let’s try it. – tell me what to do!

7 5/27/2016 7R. Smith - University of St Thomas - Minnesota Working with files Instead of getchar(), putchar(), printf()Instead of getchar(), putchar(), printf() –Use getc(), putc(), fprintf() “FILE” type variable with weird syntax“FILE” type variable with weird syntax Function fopen() opens a fileFunction 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 doneFunction fclose(fp) called when done

8 5/27/2016 8R. Smith - University of St Thomas - Minnesota Printing a file to the console FILE *fp; // file variable int ch; // 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);}

9 5/27/2016 9R. Smith - University of St Thomas - Minnesota The getfline() function This is like getline() but with a third argument: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.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?Are we handling a ‘missing file’ error? How do we handle ‘end of file’?How do we handle ‘end of file’?

10 5/27/2016 10R. Smith - University of St Thomas - Minnesota Reading and writing a file Create a file of names and numbersCreate a file of names and numbers First, let’s write a loop to read and print the fileFirst, let’s write a loop to read and print the file Next, add operations to copy contents to another fileNext, add operations to copy contents to another file

11 5/27/2016 11R. 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.


Download ppt "5/27/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical."

Similar presentations


Ads by Google