Arrays Ethan Cerami New York University 1998. Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.

Slides:



Advertisements
Similar presentations
BNF <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Advertisements

C Language.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
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 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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Sort the given string, without using string handling functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
 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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Basic Input/Output and Variables Ethan Cerami New York
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.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Computer Programming for Engineers
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Introduction to programming in java Lecture 21 Arrays – Part 1.
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)
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Dale Roberts, Lecturer IUPUI
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays Strings and Parameter Passing CSCI N305
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
ARRAYS ..
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays Ethan Cerami New York University 1998

Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings

Array Basics n What’s an Array? – –a group of related data. – –all data must share the same data type. n n Examples: – –int temp[5]; – –float stock[5];

Initializing Arrays: 3 Options n 1) You know the data ahead of time: n 2) Initialize all data to 0: n 3) Let compiler determine size of array: int temp[5] = {45, 47, 44, 56, 49}; int temp[5] = {0}; int temp[] = {45, 47, 44, 56};

Referencing Array Elements n n var_name [index]; n n index always starts at 0 n n ends at N-1 n n C does not provide any array bounds checking. int temp[25] = {0}; int x = temp[100]; /* This will compile, but it is a bug */ /* It is outside the range

Random Number Example n Program simulates the rolling of a single die 6,000 times. n Reports statistical results. Face Frequency

Step through Program n First, create an array of integers, size=7; initialize array to 0. –int face, roll, frequency[SIZE] = {0}; n Initialize the random number generator: –srand(time(NULL));

Step through Program for (roll = 1; roll <= 6000; roll++) { face = rand() % 6 + 1; ++frequency[face]; } Suppose we roll a 5: face = 5; ++frequency[5]; This bucket is not ever used, because we are only tracking 1-6.

Passing Variables to Functions #include void test (int); main () { int x = 5; printf ("In main x: %d\n", x); test (x); printf ("In main x: %d\n", x); } void test (int x) { x += 10; printf ("In Test x: %d\n", x); } Output: In main x: 5 In Test x: 15 In main x: 5 When you increment x within test, it does not affect the x within main().

Call by Value n Call by Value: – –When you pass a variable, you pass a "copy" of the variable. – –Changes to the copy variable do not affect the original variable.

Call by Reference n n Call by Reference: – –When you pass a variable, you pass a reference to the original variable. – –Changes to the reference do affect the original variable. n n Arrays are passed via call by reference.

Temperature Example Temperature Example n Program creates an array of temperature values. n The function makeHot() receives an array of temperature values, and increases each temperature by 10 degrees.

The makeHot Function void makeHot (int thermo[], int size) { int i; printf ("Making hot!\n"); for (i=0; i<SIZE; i++)thermo[i] += 10; } n Function Prototypes/Definitions –To specify an array parameter, indicate it with brackets. –No need to include size in brackets. (In fact, size is ignored.) –Specify the size of the array as a separate value.

Program Output n The Big Picture: makeHot() changes the values of the temp array. Since arrays are passed by reference, the changes do affect the original variable. Output: Making hot!

Call by Value v. Call by Reference n Call by Value: – –When you pass a variable, you pass a "copy" of the variable. – –Changes to the copy variable do not affect the original variable. n Call by Reference: – –When you pass a variable, you pass a reference to the original variable. – –Changes to the reference do affect the original variable.

Strings n Creating Strings: –A string is an array of char variables. char name[] = "ETHAN"; This will create the following array of 6 characters: ETHNA\0 This is the NULL terminator. Indicates the end of a string.

Creating/Using Strings char name[] = {'E', 't', 'h', 'a', 'n', '\0'}; You can also reference individual elements within a string: printf ("%c", name[2]) This will print: H char name[] = "ETHAN"; has the same functionality as:

Inputting Strings n To input string, use scanf() or gets() – –scanf ("%s", name); n n Scanf reads in character data until the first white space character. n n Whitespace = space, tab, new line character. Note: There is no & needed when reading in strings.

Inputting Strings (Cont) #include main () { char name[255]; printf ("Enter your name: "); scanf ("%s", name); printf ("Hi %s!", name); getche(); }

Inputting Strings (Cont.) n n gets: reads in character data until the new line character. – –It therefore reads in spaces, tabs, etc. –gets (name);

Comparing Strings n n strcmp: takes two strings, returns 0 if they are equal. #include main () { char password[255]; printf ("Enter password: "); scanf ("%s", password); if (strcmp (password, "bluemoon")== 0) printf ("Welcome!\n"); else printf ("Access Denied.\n"); getche(); }