Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming Languages and Paradigms The C Programming Language.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Chapter 10.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
CS150 Introduction to Computer Science 1
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Lecture 7: Microcontrollers & I/O Bryan Burlingame 14 October 2015.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays.
Computer Programming for Engineers
Module 1: Array ITEI222 - Advance Programming Language.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
A First Book of ANSI C Fourth Edition Chapter 9 Character Strings.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
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)
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Lecture 5: How to think like a programmer, Arrays & Strings
Computer Organization and Design Pointers, Arrays and Strings in C
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Fundamentals of Characters and Strings
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Languages and Paradigms
CSE 303 Concepts and Tools for Software Development
Lecture 9: Pointers B Burlingame 25 October 2017.
A First Book of ANSI C Fourth Edition
CSE 303 Lecture 14 Strings in C
Programming Paradigms
Arrays in C.
Lecture 11: Strings B Burlingame 11 April 2018.
Lecture 10: Strings B Burlingame 4 April 2018.
Lecture 8b: Strings BJ Furman 15OCT2012.
Chapter 2 Array and String Visit to more Learning Resources.
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Arrays.
C++ Programming Lecture 20 Strings
Programming Languages and Paradigms
Presentation transcript:

Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016

Announcements Homework #3 (due next week) Homework #4 (exam review) posted (due after spring break, but don’t wait!) Midterm in Lab next week  Open book  Open notes  Selective web resources Bring your lab kit!

Midterm Through today’s lecture Chapters 1 – 6, 9 Bitwise arithmetic (&, |, ^, >, ~) binary to decimal conversion decimal to binary conversions Boolean logic (T vs F)

What is an Array? So far we've dealt with scalar variables  contain just one value Arrays are collections of data of the same type that occupy contiguous memory locations  Individual values in the collection are called elements of the array  Need to declare before use: #include int main() { int i=0; double test[4] = {0}; test[0]=95.5; test[1]=74.0; test[2]=88.5; test[3]=91.0; for(i=0; i<4; i++) { printf("test[%d]=%lf",i,test[i]); 0x%p\n",&test[i]); } return 0; } Format (1D array)  type array_name [num_elements]; Ex. Array of 4 doubles named, 'test'

What is an array? - 2 int nums [10];  10 element array of integers Element no. 3 is accessed by: nums [2] because indexing begins at 0

Accessing Array Elements Individual elements of an array are accessed by their index number ** Important Note**  Array indexing starts at zero (take note of this, it is easy to forget!) char test[4];  the first element is test [0]  the fourth element is test [3] #include int main() { int i=0; doube test[4]={0}; test[0]=95.5; test[1]=74.0; test[2]=82.75; test[3]=91.5; for(i=0; i<4; i++) { printf("test[%d]=%lf",i,test[i]); 0x%p\n",&test[i]); } return 0; } Be careful!  The C compiler may not stop you from indexing beyond the boundary of your array. (What could happen?) What is test[4]? array_practice2.c

Initializing Array Elements Use braces to enclose the elements Separate elements by commas Can set number of elements in declaration:  Explicit: int nums[5]  Implicit: int imp[ ] = {1, 2}; /* array_practice3.c */ #include int main() { int i=0; int nums[5]={0,1,2,3,4}; for(i=0; i<5; i++) { printf("nums[%d]=%d",i,nums[i]); 0x%p\n",&nums[i]); } return 0; }

Initializing Array Elements For long, non-zero initializations, for loop is usually more efficient to program Note the use of a variable index /* array_practice3.c */ #include const int BIG_INDEX = 5000; int main() { int i=0; int nums[BIG_INDEX]={0}; for(i=0; i < BIG_INDEX; i++) { nums[i] = 34; } return 0; }

Strings Strings are encoded arrays of characters designed to hold language Recall that all characters are encoded as numeric values  Most modern systems use a variant of ASCII or Unicode  We’ll assume ASCII

ASCII Table

Strings By convention, strings in C are a null terminated array of characters  There is no intrinsic string datatype Null equals character value 0  i.e. char z = 0; z has a null character  written \0 Declaration: char string[] = “Hello world”;  Stored: Hello World\0 (ie 12 characters) All string handling expects this

Working with strings Much like working with pointers and arrays Many helper routines in string.h  strcmp – tests strings for equivalence  strcat – concatenates two strings  strstr – locates a string within a string  Etc. char string[] = “Hello World”; for( int i = 0; string[i] != 0 ; ++i ) { if( string[i] >= ‘A’ && string[i] <= ‘Z’ ) { string[i] = string[i] + (97 – 65); }

Formatted Input (1) We use two commands to properly obtain input from the keyboard fgets & sscanf fgets places data collected form the keyboard into a buffer stored as a character string up to and including a carriage return (when the user presses return or enter) fgets( buffer, sizeof(buffer), stdin );

Formatted Input (2) sscanf splits the input into variables  Space delimited Uses conversion metasymbols like printf sscanf( buffer, “%d %f”, &x, &y); Note the ambersands (&) & is commonly required for input & is not required for strings sscanf( buffer, “%s %s”, &city, &state);

Formatted Input (3) #include #define BUFF_SIZE 100 //const int BUFF_SIZE = 100; int main( void ) { float x = 0; float y = 0; char buffer[BUFF_SIZE] = { 0 }; printf( "Enter two floats(x y): "); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f %f", &x, &y );

Formatted Input (4) #include #define BUFF_SIZE 100 #define NUM_SIZE 100 int main( void ) { float x[NUM_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NUM_SIZE; ++i ) { printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f", &x[i]); }

Formatted Input (5) #include #define BUFF_SIZE 100 #define NAME_SIZE 100 int main( void ) { char name[NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; printf("Enter name:” ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", name); // note, no &

Formatted Input (5) #include #define BUFF_SIZE 100 #define NAME_SIZE 100 #define NAME_QTY 5 int main( void ) { char names[NAME_QTY][NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NAME_QTY; ++i ) { printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", names[i]); }

References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3 rd ed., Springer, New York, p Visited 23OCT