CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 10.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Introduction to C Programming CE Lecture 13 Strings in C.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS 201 String Debzani Deb.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng 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.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
Chapter 15 Strings as Character Arrays
C++ Programming Basics
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.
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.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Computer Programming for Engineers
CSCI 130 Basic Input and Output Chapter 9. The printf ( ) function Printf(“\nThe value of x is %d”, x); Displayed to screen (assume x = 12): –The value.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
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)
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
Department of Computer Science Western Michigan University
Strings CSCI 112: Programming in C.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Module 2 Arrays and strings – example programs.
5. Arrays, Pointers and Strings
Multidimensional Arrays
Chapter 8 Character Arrays and Strings
Multidimensional array
Arrays.
Character Arrays char string1[] = “first”;
Dr. Khizar Hayat Associate Prof. of Computer Science
Files Chapter 8.
Visit for more Learning Resources
Presentation transcript:

CSCI 130 More on Arrays

Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized via a for within a for –increase exponentially in complexity with each new dimension –are sometimes called arrays of arrays of...

Declaring M-D arrays int x[3][4]; –has 3 rows –has 4 columns –has up to 12 integer elements –first element is x[0][0] –last element is x[2][3]

Initializing M-D arrays All values can be directly initialized: –int x[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} –int x[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12}}; Can be initialized with a for within a for for (i = 0; i < 3; i++) for (j = 0; j < 4; j++) x[i][j] = 0;

Order of elements in an array int x[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} implies the following: x[0][0][0] = 1 x[1][0][0] = 7 x[0][0][1] = 2 x[1][0][1] = 8 x[0][1][0] = 3 x[1][1][0] = 9 x[0][1][1] = 4 x[1][1][1] = 10 x[0][2][0] = 5 x[1][2][0] = 11 x[0][2][1] = 6 x[1][2][1] = 12

Strings and Characters Type char can only hold a single character char x = ‘H’; To hold a collection of 5 characters (i.e. a string), we need an array char x[6] = {‘H’, ‘E’, ‘L’, ‘L’, ‘O’, ‘\0’}; String - sequence of characters ending with a null character ( \0 )

Initializing Strings char c[6] = {“HELLO”}; char c[] = {“HELLO”}; char c[] = “HELLO”; char x[6] = {‘H’, ‘E’, ‘L’, ‘L’, ‘O’, ‘\0’};

Displaying strings Can use either printf, or puts char x[] = {“HELLO”}; printf(“%s”, x); puts(x);

Receiving strings as input Can either use scanf, or gets scanf only scans up to the first occurrence of whitespace (return key or a space) char x[10]; scanf(“%s”, x); // Notice no & is necessary gets retrieves everything up to the return key char x[10]; gets(x);

String functions Copying strcpy(dest, source) Concatenation –to append y onto the end of x: strcat(x, y) Comparing –strcmp(x, y) returns a value < 0 if x < y returns a value > 0 if x > y returns a value = 0 if x = y

More on string functions Chapter 17 CIS 131 CIS 221