9/21/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Files, writing filesFiles, writing files 1D Array Recap1D.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to C Programming
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.
Lectures 10 & 11.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
March R. Smith - University of St Thomas - Minnesota Today’s Class Quiz RecapQuiz Recap Exam ReviewExam Review C MaterialC Material Matlab MaterialMatlab.
March R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class ProjectsProjects 2 dimensional arrays2 dimensional arrays Higher dimensionsHigher.
March R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Lab 16Lab 16 Filling in an arrayFilling in an array Searching for a.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Exam TopicsExam Topics.
March /18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class The project assignment, againThe project assignment, again Arrays.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Functions and Array ArgumentsFunctions and Array Arguments ExamExam.
Spring R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Where we areWhere we are Strings and TextStrings and Text ConditionalsConditionals.
Spring R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Final Exam ReviewFinal Exam Review –Assessment –Exam Format –C –Matlab.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Exam StatusExam Status Recap of Lab 7 ExampleRecap of Lab 7 Example.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Spring R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Return HomeworkReturn Homework Class ScheduleClass Schedule Functions:
March /18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Exam ReviewExam Review Arithmetic - I think everyone gets thisArithmetic.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Upcoming Exam – April 8Upcoming Exam – April 8 Structures ReviewStructures.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class RecapRecap Functions and CFunctions and C Using FunctionsUsing Functions.
March R. Smith - University of St Thomas - Minnesota Today’s Class Lab 1 commentsLab 1 comments Data Types in Chapter 4Data Types in Chapter 4 Lab.
Chapter 9: Arrays and Strings
6/28/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper recapHistory Paper recap Finish the movieFinish the movie.
Chapter 9: Arrays and Strings
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class StatusStatus –Survey –Returning homework ArraysArrays –What are they?
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Finish up Lab 13Finish up Lab 13 Lab 14 – DetailsLab 14 – Details.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class The examThe exam –Open book, no notes, like last time Exam TopicsExam.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Homework & GradesHomework & Grades Multiple DimensionsMultiple Dimensions.
March R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Peek at Lab 16Peek at Lab 16 Side effects and arraysSide effects and.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class AssignmentsAssignments Logical operationsLogical operations Summations.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
File IO and command line input CSE 2451 Rong Shi.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Grades & Lab 12Grades & Lab 12 Upcoming ExamUpcoming Exam StructuresStructures.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1/28/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Assignments 7 and 8Assignments 7 and 8 ExamExam History Paper AssignmentHistory.
1/31/2016 1R. Smith - University of St Thomas - Minnesota CISC Today’s Class RecapRecap Problem 11Problem 11 Problem 12Problem 12.
2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 9 Introduction to Arrays Fundamentals of Java.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
7/6/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Collect PapersCollect Papers Review: Our ZeroesReview: Our Zeroes Review:
7/6/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Computer HardwareComputer Hardware Recap: next programRecap: next program.
7/7/2016 1R. Smith - University of St Thomas - Minnesota CISC Today’s Class RecapRecap Project ProposalProject Proposal Problem 12Problem 12 Final.
Strings CSCI 112: Programming in C.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
Arrays in C.
Strings #include <stdio.h>
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

9/21/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Files, writing filesFiles, writing files 1D Array Recap1D Array Recap 2D Arrays2D Arrays Assignment 11Assignment 11 Searching for a stringSearching for a string

Recap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical Histogram Working with FilesWorking with Files 9/21/2015 2R. Smith - University of St Thomas - Minnesota

9/21/2015 3R. Smith - University of St Thomas - Minnesota Recap: 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

9/21/2015 4R. Smith - University of St Thomas - Minnesota Example: Writing a File FILE *fp;// file pointer char lin[LEN];// line buffer int i;// 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);

9/21/2015 5R. Smith - University of St Thomas - Minnesota Writing a File Writing a File Using putc(c, fp) to write charactersUsing putc(c, fp) to write characters –First argument: char; second argument: file pointer Asking for a file nameAsking 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?Bad file name – what happens? –Handling the error condition File name loopFile name loop –Write a do or while loop to repeat till the fopen() works Retrofitting an Existing ProgramRetrofitting an Existing Program –Convert ‘histogram’ to print to a file –Or, convert your paystub printer to go to a file

9/21/2015 6R. Smith - University of St Thomas - Minnesota Checklist for Side Effects Does it do input (from keyboard, for example)?Does it do input (from keyboard, for example)? Does it do output (to display, for example)?Does it do output (to display, for example)? Does it modify its arguments (arrays)?Does it modify its arguments (arrays)? Any YES to the above => side effectsAny YES to the above => side effects OK to “look” at arrays, but not to change themOK to “look” at arrays, but not to change them –Changing an array argument = Side Effect

Recap on 1D Array functions There’s the ‘maximum’ length and ‘real’ lengthThere’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 functionWhen we DECLARE the function –The array argument: We specify the array Type and Size: int nums[NSIZE]We specify the array Type and Size: int nums[NSIZE] –We may have a separate argument for the ‘real’ length When we USE the functionWhen we USE the function –The array argument: We specify JUST THE ARRAY NAME, no size or indexWe specify JUST THE ARRAY NAME, no size or index –If we have a separate ‘length’ argument, it’s given as an integer 9/21/2015 7R. Smith - University of St Thomas - Minnesota

Using Arrays For 1D arrays, there are TWO Cases:For 1D arrays, there are TWO Cases: Case #1: doing something to an ELEMENTCase #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 ArrayCase #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’ functionJust for old times’ sake, write a ‘sum’ function 9/21/2015 8R. Smith - University of St Thomas - Minnesota

9/21/2015 9R. Smith - University of St Thomas - Minnesota Two dimensional arrays Declare with two indicesDeclare 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 stringsFor a list of strings –First index picks a string –Second index is a character in that string

9/21/ R. Smith - University of St Thomas - Minnesota 2D Arrays and Functions Function doesn’t need size of first indexFunction doesn’t need size of first index Function does need size of other indicesFunction does need size of other indices –Must appear in argument declaration Write a sample program that fills in a string arrayWrite a sample program that fills in a string array

9/21/ R. Smith - University of St Thomas - Minnesota Assignment 11 Number lookup programNumber lookup program FunctionsFunctions –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

Suggested Strategy Phase 1Phase 1 –Write a function to read strings into an array A sentinel loop, looks for ‘null’ lineA sentinel loop, looks for ‘null’ line –Write a function to print strings from an array Can takeCan take –Call them from main Phase 2Phase 2 –Write a ‘split’ function to split lines –Rewrite the ‘read’ function to call the ‘split’ function Phase 3Phase 3 –Have it read the data from a file 9/21/ R. Smith - University of St Thomas - Minnesota

Let’s work on Phase 1 What variables?What variables? What functions?What functions? 9/21/ R. Smith - University of St Thomas - Minnesota

9/21/ R. Smith - University of St Thomas - Minnesota Standard string.h functions strcmp() – described yesterdaystrcmp() – described yesterday –Input: 2 strings –Output: neg, zero, pos = difference between mismatch strspn() – skips over a set of charsstrspn() – 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 destinationstrncpy() – copies a string to a destination –Input1: destination –Input2: source –Input3: size of destination strlen() – length of a stringstrlen() – length of a string

9/21/ R. Smith - University of St Thomas - Minnesota Writing the extract() function We need 2 array indicesWe need 2 array indices We need a variable for the ‘split’ indexWe need a variable for the ‘split’ index First, find the split pointFirst, find the split point Next, copy the number and mark the endNext, copy the number and mark the end Finally, copy the name using the 2 indicesFinally, copy the name using the 2 indices

9/21/ R. Smith - University of St Thomas - Minnesota How the assignment works Call a function to read the number/name strings into 2 separate arraysCall 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 enteredDo 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/namesStart with typed-in numbers/names Create a file of numbers/names for next stepCreate a file of numbers/names for next step

9/21/ R. Smith - University of St Thomas - Minnesota The extract() function void extract (char str[], char name[], char number[]) { int i, j;// indices to copy name int split;// split point between name and number split = strspn(str, " "); 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'; }

9/21/ 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 sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.