Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
 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.
Multiple-Subscripted Array
Chapter 8 Arrays and Strings
Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to programming in java Lecture 21 Arrays – Part 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter VII: Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
BASIC ELEMENTS OF A COMPUTER PROGRAM
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays in C.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
Introduction to C++ Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
C Programming Pointers
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Arrays

Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops 2

Introduction to Arrays Arrays acts to store related data under a single variable name with an index. It is easiest to think of an array as simply a list or ordered grouping of variables. Index is zero Based. 3

Arrays Example Using normal variables we can only store one value per variable at a given time. There are situations when we need to store multiple values. Example : In a program you need to store marks of 20 students and then find out the maximum, minimum and the average marks. If we use normal variables you need 20 variables to store the values. 4

Arrays Example contd. The code will look like: 5 int marks1, marks2, marks3,…,marks20; Cout<<"Enter marks for student 1”; Cin>>marks1; Cout<<“Enter marks for student 2”; Cin>>marks2; ……

Array Declaration Syntax NAME[size]; Ex: int numbers[6]; If we wish to initialize as we declare, we write int vector[6]={0,0,1,0,0,0}; 6

Declaring Arrays Just as with variables, arrays must also be declared before they are used. The declaration of an array involves declaring the: ◦ Data type of the array elements such as int, float, or char ◦ The maximum number of elements that will be stored in the array. The C++ compiler needs this information to determine the amount of memory that need to be reserved for an array 7

Example : Arrays Note that array index numbers starts at 0 If the array has n elements, the last array index number is n-1 You can use an integer variable to refer to the index number. Example: 8 int min; int r; r=0; min = marks[r]; r=1; if (min >marks[r]) min = marks[r];

Declaring Arrays As an example, the declaration of an array to store marks of 20 students will look like: int marks[20]; Valid index numbers for this array will be from ??? 0 through 19. 9

Arrays Think about evaluating the maximum and minimum marks, you will have to write a series of if statements. Soon you will realize the program is unnecessarily complex. We can overcome this unnecessary complexity by using a structure that can hold multiple values. Array is a data structure that holds multiple values of the same type. 10

Example : Arrays You can define a variable called marks, which represents not a single value of a marks, but an entire set of grades. Each element of the set can then be referenced by means of a number called an index If you need the marks of the 1 st student it is at: marks[0] If you need the marks of the 4 th student it is at: marks[3] marks Index Number

Assigning Values to Array Elements marks[0] = 14 marks[1] = 56 marks[2] = 44 marks[3] = 36 marks[4] = 89 marks[5] = 43 The values may come as an input from user or from a file etc marks Index Number

Assigning Values to Array Elements You can also assign the values at the time you are declaring the array. int marks[6] = {14,56,44,36,89,43} Determined the number of elements automatically based on the number of initialization elements int marks[] = {14,56,44,36,89,43} marks Index Number

Array Exercise Write a program that has an integer array to store marks of 10 students. The marks for each student will be input by the user. 14

Array Exercise Improve your program for exercise Q.61 such that when the marks reading is over you will print all the values to the screen. 15

Array Exercise Write a program that has an integer array to store marks of 10 students. The marks for each student will be input by the user. when the marks reading is over display the following details: ◦ Average marks ◦ Maximum marks ◦ Minimum marks 16

Break a Continue Statements You must have realized that it is very easy to manipulate an array using loops Most of the time we use the for loop with an array break and continue statements are also useful in array manipulation. 17

Array Exercise Declare an integer array that has 10 numbers. Store the values 45,56,67,78,89,90,98,0,0,0 in the array Declare another integer array that has 10 elements Initially all the elements in this array are empty Create a copy of 1 st array to 2 nd array until a zero(0) is encountered. Display the content of the 2 nd array. 18

Arrays of Other Data Types Even though we looked at only integer arrays so far you can store any element of a valid data type in C to an array Example : A character array char word[]={'H','e','l','l','o','!'}; Example : A float array float data[] = {1.0f, 100.0f, 200.0f}; 19