Cisc 130 today s class
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

CISC 130: Today’s Class PowerPoint PPT Presentation


  • 95 Views
  • Uploaded on
  • Presentation posted in: General

CISC 130: Today’s Class. Recap Files, writing files 1D Array Recap 2D Arrays Assignment 11 Searching for a string. Recap. Drawing in 2 dimensions Vertical Histogram Working with Files. Recap: Working with files. Instead of getchar (), putchar (), printf ()

Download Presentation

CISC 130: Today’s Class

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cisc 130 today s class

CISC 130: Today’s Class

  • Recap

  • Files, writing files

  • 1D Array Recap

  • 2D Arrays

  • Assignment 11

  • Searching for a string

R. Smith - University of St Thomas - Minnesota


Recap

Recap

  • Drawing in 2 dimensions

  • Vertical Histogram

  • Working with Files

R. Smith - University of St Thomas - Minnesota


Recap working with files

Recap: 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


Example writing a file

Example: Writing a File

FILE *fp;// file pointer

char lin[LEN];// line buffer

inti;// loop index

printf("file name: ");

getline(lin, LEN);

fp = fopen(lin, "w");

for (i = 0; i < 10; i++)

fprintf(fp, "Line %d\n", i);

fclose(fp);

R. Smith - University of St Thomas - Minnesota


Writing a file

Writing a File

  • Using putc(c, fp) to write characters

    • First argument: char; second argument: file pointer

  • Asking for a file name

    • It goes to the same folder unless you type in a different folder

    • You should check for

  • Bad file name – what happens?

    • Handling the error condition

  • File name loop

    • Write a do or while loop to repeat till the fopen() works

  • Retrofitting an Existing Program

    • Convert ‘histogram’ to print to a file

    • Or, convert your paystub printer to go to a file

R. Smith - University of St Thomas - Minnesota


Checklist for side effects

Checklist for Side Effects

  • Does it do input (from keyboard, for example)?

  • Does it do output (to display, for example)?

  • Does it modify its arguments (arrays)?

  • Any YES to the above => side effects

  • OK to “look” at arrays, but not to change them

    • Changing an array argument = Side Effect

R. Smith - University of St Thomas - Minnesota


Recap on 1d array functions

Recap on 1D Array functions

  • There’s the ‘maximum’ length and ‘real’ length

    • Maximum length = the most elements it can possibly hold

    • Real length = actual number of elements in the array

  • When we DECLARE the function

    • The array argument:

      • We specify the array Type and Size: intnums[NSIZE]

    • We may have a separate argument for the ‘real’ length

  • When we USE the function

    • The array argument:

      • We specify JUST THE ARRAY NAME, no size or index

    • If we have a separate ‘length’ argument, it’s given as an integer

R. Smith - University of St Thomas - Minnesota


Using arrays

Using Arrays

  • For 1D arrays, there are TWO Cases:

  • Case #1: doing something to an ELEMENT

    • We have a single item in the array we need to work on

    • We include its INDEX (in brackets: num[i]) to pick it out

  • Case #2: working on the Whole Array

    • Always done by a Function

    • We pass the Array Name Only to the function

  • Just for old times’ sake, write a ‘sum’ function

R. Smith - University of St Thomas - Minnesota


Two dimensional arrays

Two dimensional arrays

  • Declare with two indices

    • First one selects the row

    • Second index selects the column

    • Square brackets around each index: a[1][2]

  • For a list of strings

    • First index picks a string

    • Second index is a character in that string

R. Smith - University of St Thomas - Minnesota


2d arrays and functions

2D Arrays and Functions

  • Function doesn’t need size of first index

  • Function does need size of other indices

    • Must appear in argument declaration

  • Write a sample program that fills in a string array

R. Smith - University of St Thomas - Minnesota


Assignment 11

Assignment 11

  • Number lookup program

  • Functions

    • Read a file of name/number pairs into arrays

    • Extract strings from strings

    • Search the array for a name typed in

    • Read a line of text from a file

    • Compare the “prefix” of a string

    • Main loop

R. Smith - University of St Thomas - Minnesota


Suggested strategy

Suggested Strategy

  • Phase 1

    • Write a function to read strings into an array

      • A sentinel loop, looks for ‘null’ line

    • Write a function to print strings from an array

      • Can take

    • Call them from main

  • Phase 2

    • Write a ‘split’ function to split lines

    • Rewrite the ‘read’ function to call the ‘split’ function

  • Phase 3

    • Have it read the data from a file

R. Smith - University of St Thomas - Minnesota


Let s work on phase 1

Let’s work on Phase 1

  • What variables?

  • What functions?

R. Smith - University of St Thomas - Minnesota


Standard string h functions

Standard string.h functions

  • strcmp() – described yesterday

    • Input: 2 strings

    • Output: neg, zero, pos = difference between mismatch

  • strspn() – skips over a set of chars

    • Input1: string to check

    • Input2: string of chars to skip over

    • Output: offset to first char that’s not in Input2

  • strncpy() – copies a string to a destination

    • Input1: destination

    • Input2: source

    • Input3: size of destination

  • strlen() – length of a string

R. Smith - University of St Thomas - Minnesota


Writing the extract function

Writing the extract() function

  • We need 2 array indices

  • We need a variable for the ‘split’ index

  • First, find the split point

  • Next, copy the number and mark the end

  • Finally, copy the name using the 2 indices

R. Smith - University of St Thomas - Minnesota


How the assignment works

How the assignment works

  • Call a function to read the number/name strings into 2 separate arrays

    • One keeps the name strings

    • One keeps the number strings

    • Name[i] is the person whose number is in number[i]

  • Do a loop till a blank line is entered

    • Read a line from input

    • Look it up in the ‘names’ array; retrieve the index

    • If it’s a valid index, print out the name and number

  • Start with typed-in numbers/names

  • Create a file of numbers/names for next step

R. Smith - University of St Thomas - Minnesota


The extract function

The extract() function

void extract (char str[], char name[], char number[]) {

inti, j;// indices to copy name

int split;// split point between name and number

split = strspn(str, "01234567890- ");

strncpy(number, str, SIZE);

number[split] = '\0';

j = 0;

i = split;

while (str[i] != '\0') {

name[j] = str[i];

j = j + 1;

i = i + 1;

}

name[j] = '\0';

}

R. Smith - University of St Thomas - Minnesota


Creative commons license

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


  • Login