Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Similar documents
type arrayname [ size type arrayname [ size ] = { item ,, item size size

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Chapter 7 Array. Array. C++, How to Program

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

ARRAYS(II Unit Part II)

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Functions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014.

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Module 6: Array in C

ONE DIMENSIONAL ARRAYS

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Array Initialization

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Array. Prepared By - Rifat Shahriyar

Storage Class Specifiers Scope and Lifetime of Variables CMPE-013/L. Modules and Scope. Storage Class Specifiers Automatic Variables

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Arrays, Pointers and Memory Management

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

BITG 1113: Array (Part 1) LECTURE 8

Personal SE. Functions, Arrays, Strings & Command Line Arguments

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Object Oriented Programming. Java-Lecture 6 - Arrays

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Arrays and Strings. Arash Rafiey. September 12, 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Fundamentals of Programming

Create a Program in C (Last Class)

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Fundamental of Programming (C)

BITG 1113: Array (Part 2) LECTURE 9

UNIT-I Fundamental Notations

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Computers Programming Course 10. Iulian Năstac

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

SBE201 Data Structures and Algorithms in C

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Arrays and Pointers in C. Alan L. Cox

Procedural Programming

Arrays. Week 4. Assylbek Jumagaliyev

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Introduction to Scientific Computing and Problem Solving

Pointers (continued), arrays and strings

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Review of Important Topics in CS1600. Functions Arrays C-strings

Programming for Electrical and Computer Engineers. Pointers and Arrays

Memory, Arrays & Pointers

CMPT 102 Introduction to Scientific Computer Programming

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 10 INTRODUCTION TO ARRAYS

Pointers (continued), arrays and strings

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Array. Arrays. Declaring Arrays. Using Arrays

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Write a C program using arrays and structure

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

C: How to Program. Week /Apr/23

Fundamental of Programming (C)

Lecture 12. We have already used strings. Strings. Hello Class is a string.

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Principles of Programming. Chapter 6: Arrays

Pointers, Arrays, and Strings. CS449 Spring 2016


Pointers CMPE-013/L. Pointers. Pointers What do they do? Pointers What are pointers? Gabriel Hugh Elkaim Winter 2014

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Model Viva Questions for Programming in C lab

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

5.2 Defining and Initializing Arrays Statically in the Data Segment

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

For example, let s say we define an array of char of size six:


11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Pointers. Pointer References

Syntax Syntax for file I/O error detection system function (not in Annex A)

Arrays and Pointers (part 1)

How to declare an array in C?

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

SCSP Programming Technique C

Single Dimension Arrays

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Transcription:

1

2

3

CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4

Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are uniquely identified by the array index (sometimes called a subscript). : May contain any number of elements Elements must be of the same type The index is zero based Array size (number of elements) must be specified at declaration Syntax How to Create an Array are declared much like ordinary variables: type arrayname[size]; size refers to the number of elements size must be a constant integer int a[10]; char s[25]; // An array that can hold 10 integers // An array that can hold 25 characters 5

Syntax How to Initialize an Array at Declaration may be initialized with a list when declared: type arrayname[size] = {item 1,,item n }; The items must all match the type of the array int a[5] = {10, 20, 30, 40, 50}; char b[5] = {'a', 'b', 'c', 'd', 'e'}; Syntax How to Use an Array are accessed like variables, but with an index: arrayname[index] index may be a variable or a constant The first element in the array has an index of 0 C does not provide any bounds checking int i, a[10]; //An array that can hold 10 integers for(i = 0; i < 10; i++) { a[i] = 0; //Initialize all array elements to 0 } a[4] = 42; //Set fifth element to 42 6

Syntax Creating Multidimensional Add additional dimensions to an array declaration: type arrayname[size 1 ]...[size n ]; may have any number of dimensions Three dimensions tend to be the largest used in common practice int a[10][10]; //10x10 array for 100 integers float b[10][10][10]; //10x10x10 array for 1000 floats Syntax Initializing Multidimensional at Declaration may be initialized with lists within a list: type arrayname[size 0 ] [size n ] = {{item,,item}, {item,,item}}; char a[3][3] = {{'X', 'O', 'X'}, {'O', 'O', 'X'}, {'X', 'X', 'O'}}; int b[2][2][2] = {{{0, 1},{2, 3}},{{4, 5},{6, 7}}}; 7

Row 0 Row 1 Row 2 a[0][0] = 0; a[0][1] = 1; a[0][2] = 2; a[1][0] = 3; a[1][1] = 4; a[1][2] = 5; a[2][0] = 6; a[2][1] = 7; a[2][2] = 8; Visualizing 2 Dimensional int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; Row, Column a[y][x] Row 0 1 2 y 0 6 Column 1 0 1 3 4 2 2 0,0 0,1 0,2 5 1,0 1,1 1,2 7 8 2,0 2,1 2,2 x Plane 0 Plane 1 a[0][0][0] = 0; a[0][0][1] = 1; a[0][1][0] = 2; a[0][1][1] = 3; a[1][0][0] = 4; a[1][0][1] = 5; a[1][1][0] = 6; a[1][1][1] = 7; Visualizing 3 Dimensional int a[2][2][2] = {{{0, 1},{2, 3}}, {{4, 5},{6, 7}}}; Plane, Row, Column a[z][y][x] 0 Row 1 y 0 0 0 1 2 3 1 x 1 4 5 7 z 8

of Array Processing /************************************************** * Print out 0 to 90 in increments of 10 **************************************************/ int main(void) { int i = 0; int a[10] = {0,1,2,3,4,5,6,7,8,9}; while (i < 10) { a[i] *= 10; printf("%d\n", a[i]); i++; } } while (1); Definition Strings Character and Strings Strings are arrays of char whose last element is a null character '\0' with an ASCII value of 0. C has no native string data type, so strings must always be treated as character arrays. Strings: Are enclosed in double quotes "string" Are terminated by a null character '\0' Must be manipulated as arrays of characters (treated element by element) May be initialized with a string literal 9

Syntax Strings Creating a String Character Array Strings are created like any other array of char: char arrayname[length]; length must be one larger than the length of the string to accommodate the terminating null character '\0' A char array with n elements holds strings with n 1 char char str1[10]; char str2[6]; //Holds 9 characters plus '\0' //Holds 5 characters plus '\0' Syntax Strings How to Initialize a String at Declaration Character arrays may be initialized with string literals: char arrayname[] = "Microchip"; Array size is not required Size automatically determined by length of string NULL character '\0' is automatically appended char str1[] = "Microchip"; //10 chars "Microchip\0" char str2[6] = "Hello"; //6 chars "Hello\0" //Alternative string declaration size required char str3[4] = {'P', 'I', 'C', '\0'}; 10

Syntax Strings How to Initialize a String in Code In code, strings must be initialized element by element: arrayname[0] = char 1 ; arrayname[1] = char 2 ; arrayname[n] = '\0'; Null character '\0' must be appended manually str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = '\0'; Strings Comparing Strings Strings cannot be compared using relational operators (==,!=, etc.) Must use standard C library string manipulation functions strcmp() returns 0 if strings equal char str[] = "Hello"; if (!strcmp( str, "Hello")) printf("the string is \"%s\".\n", str); 11

Functions Array Parameters are passed by reference rather than by value for greater efficiency A pointer to the array, rather than the array itself is passed to the function This declaration void WriteLCD(char greetings[]){ } is equivalent to this declaration. void WriteLCD(char *greetings){ } Exercise 10 12

Open the lab Project: Lab 10 On the class website /s/lab10.zip > Load Lab10.X 1 Open MPLAB X and select Open Project Icon (Ctrl + Shift + O) Open the Project listed above. If you already have a project open in MPLAB X, close it by right clicking on the open project and selecting Close Exercise 10 Solution: Step 1 /*############################################################################ # STEP 1: Create two initialized arrays with 10 elements each named array1 and # array2 (you may use the pre-defined constant ARRAY_SIZE as part of # the array declaration). # The arrays should be initialized with the following values: # + array1: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 # + array2: 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 # Note: the elements are all of type int ############################################################################*/ // array1 declaration & definition int array1[array_size] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // array2 declaration & definition int array2[array_size] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; 13

Exercise 10 Solution: Step 2 /*############################################################################ # STEP 2: Pass the two arrays you declared above (array1 & array2) to the # function add_function() (see its definition below). Store the # result of the function call in the array result[]. The idea here is # to add each corresponding element of array1 and array2 and store the # result in result[]. In other words, add the first element of # array1[] to the first element of array2[] and store the result in # the first element of result[]. Next add the second elements ############################################################################*/ // result = sum of elements of array1 & array2 result[i] = add_function(array1[i], array2[i]); i++; Exercise 10 Conclusions may be used to store a group of related variables of the same type under a common name Individual elements are accessed by using the array index in conjunction with the array name may be used in many places that an ordinary variable would be used 14

Questions? 15