Thursday, January 04, 2007 I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right. - Albert.

Slides:



Advertisements
Similar presentations
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Advertisements

Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
CS 192 Lecture 10 Winter 2003 December 22-23, 2003 Dr. Shafay Shamail.
 2003 Prentice Hall, Inc. All rights reserved 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 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 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.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Java Unit 9: Arrays Declaring and Processing 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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Pointers OVERVIEW.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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)
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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’,
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Multidimensional Arrays Computer and Programming.
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.
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.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. Arrays are objects that help us organize large amounts of information.
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 Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
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)
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Objectives You should be able to describe: One-Dimensional Arrays
Computer Programming BCT 1113
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Arrays.
7 Arrays.
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Multidimensional array
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
Presentation transcript:

Thursday, January 04, 2007 I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right. - Albert Einstein

Arrays Review Arrays used for grouping of related variables. int a1; int a[5]; int a2; int a3; int a4; int a5; §Individual elements of array accessed by indices. §Easier to use in loops.

Arrays Review int a[5] = {10, 20, 30, 40, 50}; a[0] is 10 a[1] is 20 a[2] is 30 a[3] is 40 a[4] is 50 §No boundary checking of arrays because of efficiency reasons. §Error checking can slow down the program execution.

A variable is a named memory location that may be assigned a value a[0] a[1] a[2] a[3] a[4] 0x0012F578 0x0012F57C 0x0012F580 0x0012F584 0x0012F588 What is the difference between two memory addresses?

Static array declaration requires a constant int my_array[10]; //ok const int x=10; int my_array[x]; //also ok #define size 10 int main() { int a[size]; //also ok return 0; }

What is wrong here? const int x;

§The word "algorithm" itself is quite interesting; at first glance it may look as though someone intended to write "logarithm" but jumbled up the first four letters. The word did not appear in Webster's New World Dictionary as late as 1957; we find only the older form "algorism" with its ancient meaning, i.e., the process of doing arithmetic using Arabic numerals. In the middle ages, abacists computed on the abacus and algorists computed by algorism. Following the middle ages, the origin of this word was in doubt, and early linguists attempted to guess at its derivation by making combinations like algiros [painful] + arithmos [number]; others said no, the word comes from "King Algor of Castile."

§Finally, historians of mathematics found the true origin of the word algorism: it comes from the name of a famous Persian textbook author, Abu Ja`far Mohammed ibn Mûsa al-Khowârizmî - literally, "Father of Ja´far, Mohammed, son of Moses, native of Khowârizm." Khowârizm is today the small Soviet city of Khiva. Al- Khowârizmî wrote the celebrated book Kitab al jabr w'al-muqabala ("Rules of restoration and reduction"); another word, "algebra", stems from the title of his book, although the book wasn't really very algebraic. §Gradually the form and meaning of "algorism" became corrupted; and changed from "algorism" to "algorithm”.

Sorting an array double dArray[10]= {34, 5.6, 0.9, 345.7, 54.1, 23.5, 2.5, 6.78, 12.4, 13.9};

Sorting an array int main(void){ double dArray[10]={34, 5.6, 0.9, 345.7, 54.1, 23.5, 2.5, 6.78, 12.4, 13.9}; int i, j, size=10; double temp; for (i=0; i<size; i++){ for (j=i+1; j<size; j++){ if (dArray[j]<dArray[i]){ temp=dArray[j]; dArray[j]=dArray[i]; dArray[i]=temp; } } //in-place sorting return 0; }

Self Test: Sorting an array Modify the previous program so that it does not do in-place sorting, but instead stores the sorted list in another array.

Self Test: Sorting an array // Using the bubble sort to order an array. int main() { int nums[10]; int a, b, t; int size; size = 10; // number of elements to sort // give the array some random initial values for(t=0; t<size; t++) nums[t] = rand();

Self Test: Sorting an array (Simulate) // This is the bubble sort. for(a=1; a<size; a++) for(b=size-1; b>=a; b--) { if(nums[b-1] > nums[b]) { // if out of order // exchange elements t = nums[b-1]; nums[b-1] = nums[b]; nums[b] = t; } } // This is the end of the bubble sort.

Two Dimensional Arrays §C++ supports multi-dimensional arrays l type array_name[row_size][column_size] l int matrix[3][4]; row[0] row[1] row[2]

Accessing Array Elements §int matrix[3][4]; l matrix has 12 integer elements l matrix[0][0]element in first row, first column l matrix[2][3]element in last row, last column

int main() { int i, j, num[3][4]; for(i=0; i<3; ++i) { for(j=0; j<4; ++j) { num[i][j] = (i*4)+j+1; cout << num[i][j] << “\t“; } cout << “\n”; } return 0; }// output? Two Dimensional Arrays

Output:

int main() { int i, j, num[2][6]; for(i=0; i<2; ++i) { for(j=0; j<6; ++j) { num[i][j] = (i*6)+j+1; cout << num[i][j] << “ “; } cout << “\n”; } return 0; }// output? Two Dimensional Arrays What happens if I change [3][4] to [2][6]?

Output:

Self Test: Arrays Print the transpose of a 2-D array.

Two dimensional arrays int E[][2]={ 1,2, 3,4 }; §Matrix Multiplication

Nested Loops Review int main() { int i, j; for(i=0; i<3; i++) { for(j=0; j <= 4; j++) cout<<“The value of i=” <<i<<“ and j= ”<<j<<“\n”; cout<<”Out of the inner for-loop\n"; } return 0; }

Nested Loops Review The value of i=0 and j=0 The value of i=0 and j=1 The value of i=0 and j=2 The value of i=0 and j=3 The value of i=0 and j=4 Out of the inner for-loop The value of i=1 and j=0 The value of i=1 and j=1 The value of i=1 and j=2 The value of i=1 and j=3 The value of i=1 and j=4 Out of the inner for-loop The value of i=2 and j=0 The value of i=2 and j=1 The value of i=2 and j=2 The value of i=2 and j=3 The value of i=2 and j=4 Out of the inner for-loop

Practice Exercise! §Write down the code to multiply two matrices of sizes NxM and MxQ respectively.

Strings §One dimensional arrays is used to create character strings §In C++, a string is defined as a character array that is terminated by a null §A null is specified using ‘\0’ and is zero §Because of the null terminator, it is necessary to declare a character to be one character longer than the largest string it will hold.

Character Arrays char word[6] = “fruit”;//word has size 6 char list[6] = {‘f’,’r’,’u’,’i’,’t’, ‘\0’}; //list of characters, not a string Not necessary to add null terminator at end of string constants.

Character Arrays char word[] = “fruit”; //word has size 6

§What is wrong with the following? char name[5]="class"; int MyArray[];

§cout<<'3'; §cout<<','; §cout<<'\n';

§'a' and "a" are different