FP201 - PROGRAMMING FUNDAMENTALS Unit 4.1 - Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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 9: Arrays and Strings
Chapter 9: Arrays and Strings
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.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Arrays.
C ARRAYS -a collection of same type data, 1D, 2D- © 1/25.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
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 
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
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.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Module 1: Array ITEI222 - Advance Programming Language.
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Arrays Skill Area 315 Part A
CHAPTER 2 Arrays and Vectors.
CHAPTER 2 Arrays and Vectors.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS

I NDEX Objective Introduction to Array One dimensional array Two dimensional array

O BJECTIVES At the end of this module, students should be able to: Declare and use an array Use the array statement in C++ program Accessing element in an array

A RRAY D EFINITION Array is a collection of data elements of the same type that are referenced by a common name. Used to process a collection of data all of which is of the same type, such as list of name, list of temperature.

A RRAY D EFINITION Array’s element consist of memory allocation and identified by index. Array size : st Index Indexs

Two types of arrays  One-dimensional array  Two-dimensional array

D ECLARING O NE D IMENSIONAL A RRAY Will have a single row and can have any number of columns. Will have only one subscript. Subscript refers to the dimension of the array. Array declaration of 10 alphabet type array_name[size] Eg : char huruf[10];

I NITIALIZING O NE D IMENSIONAL A RRAY Initialization is the process of assigning values to the array you have created. To assign initial values to each one of array’s elements we must enclose the values in curly braces ({ }) and separate them with comma (,). Eg : char huruf[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};

I NITIALIZING O NE D IMENSIONAL A RRAY Eg: int nombor[3] = {3, 24, 31}; nombor[0];//3 nombor[1];//24 nombor[2];//31 nombor[0+1];//nombor[1];//24 nombor[3]; nombor first index

A CCESSING E LEMENT OF O NE D IMENSIONAL A RRAY  Element is accessed by its index  Array index refers to the location of the values in an array.  The first element will always have the array index as 0. Syntax : [Array index] = Value; For example: marks[0]=95; marks[1]=85; marks[2]=75;

A CCESSING E LEMENT OF O NE D IMENSIONAL A RRAY  Eg: int my_array[5] = {11, 22, 33, 44, 55};  to store the value 75 in the third element of my_array, we could write the following statement: my_array[2] = 75;  to pass the value in 4 th element of my_array and store the value into temporary variable, temp_value : int temp_value = my_array[3]; // also equals to 44

A CCESSING E LEMENT OF O NE D IMENSIONAL A RRAY  if the name of an array is name, then name[0] is the name of the element that is in position 0, name[1] is the name of the element that is in position 1, etc.  in general, the nth element is in position n-1. So if the array has n elements, their names are name[0], name[1], name[2], …, name[n- 1].  it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays: int name[5]; // declaration of a new array name[2] = 75; // access to an element of the array.

E XAMPLE Program Student_Marks.cpp will illustrate how to declare an array, initialize and access its elements.

#include using namespace std; void main() { int marks[]={95,85,75,80,65}; cout<<"marks[0] : "<<marks[0]; cout<<"\nmarks[1] : "<<marks[1]; cout<<"\nmarks[2] : "<<marks[2]; cout<<"\nmarks[3] : "<<marks[3]; cout<<"\nmarks[4] : "<<marks[4]; } marks[0] : 95 marks[1] : 85 marks[2] : 75 marks[3] : 80 marks[4] : 65

EXAMPLE Program Onedim_Int_Array.cpp illustrates how to initialize integer array and display its contents.

#include using namespace std; void main() { int y[4]={8,7,6,4}; for(int i=0;i<4;i++) { cout<<y[i]<<"\n"; }

E XAMPLE Program Onedim_Char_Array_Name.cpp illustrates how to initialize a character array and display its contents.

#include using namespace std; void main() { char stud_name[]={‘M',‘A',‘F','I','A'}; for(int i=0;i<=4;i++) { cout<<stud_name[i]; }

E NTERING D ATA INTO AN A RRAY  When more number of values are to be stored in an array, a for loop can be used.  The sample code shows how to use a for loop in an array. for(int i=0;i<5;i++) { cout<<“Enter the marks: "; cin>>marks[i]; }

R EADING D ATA FROM AN A RRAY You can use a for loop with a single cout statement to print the values from an array. for (int i=0;i<5;i++) { cout<<"Marks : "+marks[i]); }

E XAMPLE Program One_Int_Array.cpp illustrates how to accept five marks from the user and prints the values on the screen.

#include using namespace std; void main() { int marks[5]; //Accepting the marks for(int i=0;i<5;i++){ cout<<"Enter mark :"; cin>>marks[i]; } cout<<"\nThe marks you have enter is"<<endl; //Displaying the array for(int i=0;i<5;i++){ cout<<"Marks:"<<marks[i]<<endl; }

I N C LASS E XERCISE 4.1 Declare an array alpha of 15 elements of type int. Access the value of tenth element of array alpha. Set the value of fifth element of array alpha to 35. Set the value of ninth element of array alpha to the sum of fifth and sixth element of array alpha.

Declare an array alpha of 15 elements of type int. int alpha [15]; Access the value of tenth element of array alpha. alpha [9]; Set the value of fifth element of array alpha to 35. alpha [4] = 35;

Set the value of ninth element of array alpha to the sum of fifth and sixth element of array alpha. alpha [8] = alpha [4] + alpha [5]

What is the output #include using namespace std; void main() { double num []= {2.0, 4.0, 6.5, 8.7}; cout<<num[1+2]; }

 How to fill in value into array #include using namespace std; void main() { for(int i = 0; i < 10; ++i) { cout << i+1 << “\n"; }  Output: //program output

#include using namespace std; void main() { int num[]={1,2,3,4,5,6,7,8,9,10}; for(int i = 0; i < 10; ++i) { cout << num[i]<< "\n"; }

TWO-D IMENSIONAL A RRAY Two-dimensional arrays can be described as "arrays of arrays". For example, a two-dimensional array can be imagined as a two-dimensional table made of elements of a same uniform data type.

P RESENTATION OF TWO - DIMENSIONAL ARRAY

Assume that there are 5 students in a class and each of them study three different subjects, for example Mathematics, Physics and Chemistry.

Example int marks_table [5][3]; Syntax [Row][Column];

TWO-D IMENSIONAL A RRAY Table jimmy represents a bidimensional array of 3 by 5 elements of type int. The way to declare this array in C++ would be: int jimmy [3][5]; row column

I NITIALIZING TWO-D IMENSIONAL A RRAY Eg: int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; Output: int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; Output: int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; Output:

A CCESSING E LEMENT IN TWO-D IMENSIONAL A RRAY Element is accessed by the index of its row and column. Eg: To access the element in the 2 nd row and at the 4 th column of this two-dimentional array, we can used the following code: jimmy[1][3];

#include using namespace std; void main() { int array2[ 2 ][ 3 ] = {{ 1, 2, 3},{4, 5,6 }}; for(int index1=0;index1<2;index1++) { for(int index2=0;index2<3;index2++) cout<<array2[index1][index2] << " "; cout<<endl; }

O UTPUT

W HAT IS OUTPUT ? #include using namespace std; void main() { int marks_table[5][3] = {{83,99,74}, {88,90,72},{89,88,82}, {98,93,75},{78,60,65}}; cout<<marks_table[1][2]; }

#include using namespace std; void main() { string Data [2][3]; //For first fow Data[0][0] = "Lisa"; //lastname Data[0][1] = "Sulaiman"; //firstname Data[0][2] = "Kedah"; //location //Second row Data[1][0] = "Ali"; //lastname Data[1][1] = "Muhammad"; //firstname Data[1][2] = "Johor"; //location cout<<"Lastname\tFirstname\tLocation\n"; for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ cout<<Data[i][j]<<"\t\t"; } cout<<"\n";//move to new line }

#include using namespace std; void main() { int array2[ 23 ][ 4 ]; //Accepting the marks for (int row=0; row<2; row++) { for(int col=0; col<3; col++){ cout<<"Enter mark ["<<(row)<<"][" <<col <<"]: "; cin>>array2[row][col]; } cout<<endl; } //display for(int row=0; row<2; row++){ for(int col=0; col<3; col++) cout<<array2[row][col] << " "; cout<<endl; }

O UTPUT

I N C LASS E XERCISE 4.2 Declare an array beta of 10 rows and 20 columns of type int. Examine the following: double values[ ] [ ] = { {1.2, 9.0, 3.2}, {9.2, 0.5, 1.5}, {7.3, 7.9, 4.8} } ; What is the value of values[2][1]?

Which of the following statements constructs an array with 5 rows of 7 columns? long stuff[5][7]; long[5][7]; long stuff[7][5]; long [7][5];

Declare an array beta of 10 rows and 20 columns of type int. int beta [10][20] o Value of values[2][1]? 7.9 o long stuff[5][7];

S UMMARY An array is a structured data type with a fixed number of elements. Every element of an array is of the same type and can be accessed by their index. Array index started with 0. Array can be initialized during declaration. A one-dimensional array has one subscript. In two-dimensional array, elements are arranged in table form. To access element from two-dimensional array, pair of indices is needed (index for row and index for column).