One Dimensional Arrays

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Fundamentals of Computer and programming in C Programming Languages, Programs and Programming Rohit Khokher.
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
MATH 224 – Discrete Mathematics
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Programming and Data Structure
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.
Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Character Arrays 4 Integer Arrays 4 Floating Point Number Arrays 4.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 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.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Data Structures Using C++ 2E
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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
Lists in Python.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
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 Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth 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.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 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.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Data Structures Arrays and Lists Part 2 More List Operations.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Lecture 2. Algorithms and Algorithm Convention 1.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Programming BCT 1113
Operation performed by Linear Structure
Chapter 7 Arrays.
Introduction to C Programming
Arrays.
Presentation transcript:

One Dimensional Arrays Rohit Khokher

One Dimensional Arrays Definition A one-dimensional array is a linear structure of components. Components A linear structure 1 2 3 4 5 6 7 8 9

One Dimensional Arrays Points to remember An array must have a variable name Every component must hold a data element. All components must hold the same types of data elements. Data elements must be accessed by specifying the component positions that hold the data element. Positions must be specified with a single index value (also called as subscript). An index must lie within the lower (Position of the left-most component) and upper (Position of the right-most component) bounds of array size

One Dimensional Arrays Array name X X[1] X[2] X[3] X[4] X[5] X[6] X[N] X is the variable name X1], X[2], …, X[N] are referred to as subscripted variable names Integers 1, 2, 3, …, N are referred to as indices/subscripts An index/subscript must be a constant or variable name of integer type The value of an index/subscript must be non-negative integer An index/subscript value is also referred to as an offset value Program loader allocates consecutive memory locations in data segment for all the components of an array

One Dimensional Arrays X[1] X[2] X[3] X[4] X[5] X[6] X[N] Base address: The address of the first components of the array in the data segment X Component address =Base address + Offset , or Component address =Base address + index value, or Component address =Base address + Subscript value Example Address of X[i] =Base address of X + value of i Suppose the loader loads the array X of size 50 components starting at address, say, (0250)10 and the value of the index i is (20)10 then the address of X[20] will be : Address of X[20] = (0250)10 + (20)10 = (0270)10

One Dimensional Arrays Component type All the components must be of same type (data/structure) X[1] X[2] X[3] X[4] X[5] X[6] X[N] 100 240 034 345 004 012 00001 All integer numbers X[1] X[2] X[3] X[4] X[5] X[6] X[N] 1.00 2.40 03.4 34.5 0.04 01.2 .00001 All floating point numbers Data type mixing not allowed

One Dimensional Arrays Operations on 1-D Arrays create array (by explicit declarations in C like programming languages) Destroy array Initialize array elements Update array elements Insert elements in an array Delete array elements Search an array for an specified element Find if an array is full Find if an array is empty Find the largest/smallest elements of an array Add elements of an array Add elements of two arrays Sort elements of an array in ascending/descending order …………

One Dimensional Arrays (Algorithms) Add elements of an array of size N Memory addresses Start Set N = 5 Declare array X[N] Read X S = 0 For i = 1 to N do S=S + X[i] Print S Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 1.5 2.5 3.5 0.5 1.0 5 1.5 2.5 3.5 0.5 1.0 S 5 1.5 2.5 3.5 0.5 1.0 i 5 1.5 2.5 3.5 0.5 1.0 1 5 1.5 2.5 3.5 0.5 1.0 9.0 5 1.5 2.5 3.5 0.5 1.0 4.0 2 5 1.5 2.5 3.5 0.5 1.0 7.5 3 5 1.5 2.5 3.5 0.5 1.0 8.0 4

Algorithm & C Code Can we write it as scanf (“%f”, &x[i]);? void main () { int i, n; float s; float x[5], value; n = 5; for ( i=0; i < n; i++) { scanf (“%f”, &value); x[i] = value; } s=0; s= s + x[i]; printf (“\n sum = %5.2f \n”, s); } Must declare all the variables used in the program Start Set N = 5 Declare array X[N] Read X S = 0 For i = 1 to N do S=S + X[i] Print S Stop Can we write it as scanf (“%f”, &x[i]);? Read Ch.4 on Input / Output in the text book prog. In ANSI C by E. Balgurusamy dd.dd

One Dimensional Arrays (Algorithms) Find the largest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = 0 For i = 1 to N do If X[i]> L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 1.5 2.5 3.5 0.5 1.0 5 1.5 2.5 3.5 0.5 1.0 L 5 1.5 2.5 3.5 0.5 1.0 i 5 1.5 2.5 3.5 0.5 1.0 5 1.5 2.5 3.5 0.5 1.0 4 5 1.5 2.5 3.5 0.5 1.0 1 5 1.5 2.5 3.5 0.5 1.0 2 5 1.5 2.5 3.5 0.5 1.0 3

One Dimensional Arrays (Algorithms) Find the largest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = 0.0 For i = 1 to N do If X[i]> L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 -1.5 -2.5 -3.5 -0.5 -1.0 5 -1.5 -2.5 -3.5 -0.5 -1.0 L 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 i -1.5 -2.5 -3.5 -0.5 -1.0 0.0 5 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 4 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 2 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 3 5 -1.5 -2.5 -3.5 -0.5 -1.0 0.0 1 Wrong ?

One Dimensional Arrays (Algorithms) Find the largest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = X[1] For i = 2 to N do If X[i]> L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 -1.5 -2.5 -3.5 -0.5 -1.0 5 -1.5 -2.5 -3.5 -0.5 -1.0 L 5 -1.5 -2.5 -3.5 -0.5 -1.0 i -1.5 -2.5 -3.5 -0.5 -1.0 5 5 -1.5 -2.5 -3.5 -0.5 -1.0 4 5 -1.5 -2.5 -3.5 -0.5 -1.0 2 5 -1.5 -2.5 -3.5 -0.5 -1.0 3 Correct?

One Dimensional Arrays (Algorithms) Find the smallest element of an 1-D array Start Set N = 5 Declare array X[N] Read X L = X[1] For i = 2 to N do If X[i]< L then L=X[i] Print L Stop N 5 X[1] X[2] X[3] X[4] X[5] 5 -1.5 -2.5 -3.5 -0.5 -1.0 5 -1.5 -2.5 -3.5 -0.5 -1.0 L 5 -1.5 -2.5 -3.5 -0.5 -1.0 i -1.5 -2.5 -3.5 -0.5 -1.0 5 5 -1.5 -2.5 -3.5 -0.5 -1.0 4 5 -1.5 -2.5 -3.5 -0.5 -1.0 2 5 -1.5 -2.5 -3.5 -0.5 -1.0 3 Correct?

One Dimensional Arrays (Algorithms) Find the difference between the largest and smallest elements of an 1-D array For i = 2 to N do Begin If X[i]< smallest then smallest =X[i] If X[i] > largest then largest = x[i] end Print | largest –smallest| Stop Start Set N = 5 Declare array X[N] Read X smallest = X[1] largest = X[1] A block/range/scope of the for loop Trace the algorithm for X= -1.5 -2.5 -3.5 -0.5 -1.0

One Dimensional Arrays (Algorithms) Find which element is the largest element of an 1-D array For i = 2 to N do If X[i] > largest then Begin largest = x[i] k=i end Print “The “ k “th element” Stop Start Set N = 5 Declare array X[N] Read X largest = X[1] k=1 A block/range/scope of the for loop Trace the algorithm for X= -1.5 -2.5 -3.5 -0.5 -1.0

One Dimensional Arrays (Algorithms) Trace the algorithm for X= Start Set N = 5 Declare array X[N] Read X For i=1 to N-1 do For j = i+1 to N do If X[j] > X[i] then Begin T= X[i] X[i]= x[j] X[j] = T end Print X Stop -1.5 -2.5 -3.5 -0.5 -1.0 -0.5 -2.5 -3.5 -1.5 -1.0 -0.5 -1.5 -3.5 -2.5 -1.0 -0.5 -1.0 -3.5 -2.5 -1.5 -0.5 -1.0 -2.5 -3.5 -1.5 -0.5 -1.0 -1.5 -3.5 -2.5 -0.5 -1.0 -1.5 -2.5 -3.5

Practice problem: Starting from pass 1 to pass 4, the following figure illustrates operations on an array. Give a pseudo code that gives an algorithmic representation of the operations depicted in this figure. 7 2 8 5 4 85 Stop No Exchange Takes Place Pass 1 Pass 2 Pass 3 Pass 4 Hint: While change do set change to false begin for all the array elements do if element x[i] > x[i+1] then swap (X[i] and x[i+1] change is true end

Summary You should know Variable declarations One dimensional array declarations Reading elements of 1-D arrays (Formatted input) Writing elements of 1-D arrays (formatted output) Array based simple algorithms 1-D array manipulation using for statements 1-D array manipulation using while and do while statements 1-D array manipulation using if and switch statements