Introduction to C Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Arrays.
Programming and Data Structure
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
 2007 Pearson Education, Inc. All rights reserved C Arrays.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays Chapter 7.
An Introduction to Programming with C++ Sixth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
JavaScript: Functions.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
JavaScript Arrays.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
7 Arrays.
C++ Array 1.
Presentation transcript:

Introduction to C Programming ET2560 Introduction to C Programming Introduction to C Programming Unit 8 Arrays and Strings Unit 1 Presentations

Unit 7 Review Unit 8: Review of Past Material

Functions - Review Function Header Function Body Return Type Identifier - name of function Parameter List Data type and Identifier for each parameter Comma-separated list, enclosed in parentheses Function Body Compound Statement in braces Must contain a return statement unless return type is void

Variable Scope Global variable Local variable Block scope Declared at top of a file Accessible throughout the compilation Local variable Declared inside a function at top of function body Known only within the function Block scope Declared inside a nested compound statement (block of code) Known only within the block Two or more variables with same name - the one at the smaller scope hides the one(s) at larger scope(s)

Pointers Pointer has a data type corresponding to the variable pointed to Pointers are declared using the "*" asterisk character The address of a variable is obtained using the "&" character To use a pointer to access memory, precede it with "*" Used to create output parameters for functions Each output parameter is declared as a pointer The function call must use the address of a variable as the argument Inside the function, the external variable is accessed by pointer

Arrays Unit 8: Arrays and Strings

Data Structures Group of related data items Stored in one contiguous area of memory Can be accessed and manipulated as a group or unit Record (C language "struct") Combination of data items of various data types Each data item called a "field" in the record Array (C language "array") Multiple data items of matching data type Each data item called an "element" in the array

Arrays - Declaration Data type, variable identifier, size in square brackets Size must be a constant expression, not a run-time expression Size must be a positive integer int numbers [ 5 ]; The example above declares an array variable of 5 elements The elements are numbered starting from 0: numbers[0], numbers[1], numbers[2], numbers[3], numbers[4] Notice that the last element is "4", not "5" !

Arrays - Access to Array Elements The element is identified by its index (numerical position) Name of array (identifier) followed by subscript Subscript is index in square brackets Index value must be integer expression Examples using the numbers array: value = numbers[10]; value = numbers[i+2]; numbers[4] = value + 3; numbers[0] =

Arrays - Caution! Array element numbering (index) starts at 0, not 1 The elements are numbered [0], [1], [2], … [n-1] int numbers[5]; numbers[0], numbers[1], numbers[2], numbers[3], numbers[4] Common Programming Error: Accessing past end of array - logic error numbers[5] C Language does not check for this error

Array Initializers Follow array declaration with a list of expressions in braces int numbers[5] = {3, 12, -42, 0, 6}; If a size is specified, and list is too short, zeros are used at end If a size is specified, and list is too long, this is an error If a size is not specified (empty square brackets) The compiler counts the number of initializers The count is used as the size int numbers[] = {3, 12, -42, 0, 6};

Array Usage - Parallel Arrays Parallel Arrays - Arrays of related data items Example: A list of 100 rectangles, dimensions length and width Use two arrays of doubles, each size 100 double length[100], width[100]; length[i] = 40; width[i] = 25;

Array Usage - Use with Functions Arrays and Functions An element can be passed as an argument to a function area = calcArea(length[3], width[3]); An entire array can be passed as an argument to a function Size not passed automatically, must be an explicit separate parameter f(numbers, 5); An array cannot be returned from a function

Multi-Dimension Arrays Arrays can have one to six dimensions A two-dimensional array is like a grid The first dimension is the row, second is the column Specify each size in separate square brackets double grid[50][25]; When accessing, provide separate subscripts grid[r][c] = 42.5;

Multi-Dimension Arrays When initializing, initialize one row at a time Initializer is "list of lists" Use braces for the entire list Use braces for list of initializers for each row double xy[4][3] = { {0, 12.2, 14.7}, {2.5, 8.3, -6}, {2.0}, /* "Short" list filled with zeros */ {1E6, 3.2E-1, 47.9} };

Searching and Sorting Unit 8: Arrays and Strings

Searching Often-used algorithm Goal: Find a value, discover it's index May have result of "not found" Array is list of values, look through the list Is the list unsorted (random order)? Linear Search pp. 411-413 Look through each element from beginning Finds the first occurrence Is the list sorted (sequential order) ? Binary Search Divide list in half Finds one occurrence, not necessarily the first More efficient than linear search (over the long run)

Sorting Common, often-used algorithms Many sort algorithms have been developed Simple, but less efficient Complex, faster performance Example: Selection Sort pp. 413-415 Find smallest element (first occurrence) in list [0] … [n-1] Swap smallest element with first element Now, use Selection Sort on remaining list [1] … [n-1] Continue until the "remaining unsorted list" has 1 element

String Variables: Arrays of Characters Unit 8: Arrays and Strings

Strings Important, commonly-used data type Group of characters treated as a unit Examples of common uses Names of people, places, devices, etc Instructions to user Error messages Web addresses and URLs

C Language Strings C does not actually have a string data type A C string uses a character array The string begins with the 0th element, might not fill array To indicate end of string, a sentinel 0-value character is used The sentinel must always be present char s1[10] = "George"; The format specifier for printf and scanf is "%s" G e o r g \0