CS1201: Programming Language 2

Slides:



Advertisements
Similar presentations
Chapter 4 Summation.
Advertisements

1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Module 1: Array ITEI222 - Advance Programming Language.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Pointers and Arrays Dynamic Variables and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Pointers What is the data type of pointer variables?
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Two Dimensional Array Mr. Jacobs.
Two-Dimensional Arrays Lesson xx
Linked lists.
Programming fundamentals 2 Chapter 1:Array
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointer Data Type and Pointer Variables II
Multi-dimensional Array
Pointers Psst… over there.
לולאות קרן כליף.
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
CS1201: Programming Language 2
CS1201: Programming Language 2
Programming II Pointers and arrays by aalosaimi.
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
1020: Introduction to Programming Mohamed Shehata November 7, 2016
CS150 Introduction to Computer Science 1
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Pointer Data Type and Pointer Variables III
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Control Structures Part 2
Stacks Data structure Elements added, removed from one end only
CS1201: Programming Language 2
Arrays of Two-Dimensions
CHAPTER 2 Arrays and Vectors.
CS1201: Programming Language 2
Arrays Arrays A few types Structures of related data items
C Programming Pointers
CHAPTER 2 Arrays and Vectors.
Pointers and dynamic objects
Pointers & Functions.
Pointer Data Type and Pointer Variables
CS 144 Advanced C++ Programming February 12 Class Meeting
Pointer Data Type and Pointer Variables
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
CS31 Discussion 1D Winter19: week 4
Programming Fundamental
Linked lists.
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:

CS1201: Programming Language 2 By:Nouf Aljaffan Edited by : Nouf Almunyif Dynamic Arrays

Dynamic Array int * arrayptr; int arraysize; cout<<"enter the array size :" <<endl; cin >> arraysize; arrayptr = new int [arraysize];

Two-Dimensional Arrays int board[4][5]; 1 2 3 4 5

Dynamic Two-Dimensional Arrays (1) X int x ; int *p ; int x[4] P X

Dynamic Two-Dimensional Arrays (1) There are various ways you can create dynamic dimensional arrays. One way is as follows. int *p[4]; // declares an array of four pointers. P

Dynamic Two-Dimensional Arrays (1) Suppose that we want to draw a 4X6 bord How can we do that ? int *board[4]; You can now use these pointers to create the rows of board. Suppose that each row of board has six columns. Then, the following for loop creates the rows of board. for (int row = 0; row < 4; row++) board[row] = new int[6];

Row = Board[0] = new int [6] int *board[4]; for (int row = 0; row < 4; row++) board[row] = new int[6]; Row = Board[0] = new int [6] 4 2 3 1 Board[3] = new int [6] Board[1] =new int [6] Board[2] =new int [6] board

Note Note that the expression new int[6] creates an array of six components of type int and returns the base address of the array. The assignment statement then stores the returned address into board[row]. It follows that after the execution of the previous for loop, board is a two-dimensional array of four rows and six columns. However, the way board is declared, the number of rows is fixed. So in reality, board is not a true dynamic two-dimensional array.

Dynamic Two-Dimensional Arrays (2) Second approach to declare 2D array is int **board; // declares board to be a pointer to a pointer. In other words, board and *board are pointers Suppose that you want board to be an array of 10 rows and 15 columns. To accomplish this, first we create an array of 10 pointers of type int and assign the address of that array to board. board = new int* [10]; Next, we create the columns of board. The following for loop accomplishes this. for (int row = 0; row < 10; row++) board[row] = new int[15];

Example #include <iostream> using namespace std; void fill (int **p, int row ,int column); void print (int **p, int row ,int column); void main(){ int ** board; int R, C; cout<<"enter the array size :" <<endl; cout << "number of rows : " <<endl;cin >>R; cout<<"number of columns : " <<endl;cin >> C; cout <<" *************************" <<endl; board = new int*[R]; for ( int i=0;i<R;i++) board[i]= new int[C]; fill (board,R,C); print(board ,R,C); }

Example void fill (int **p, int row ,int column) { for ( int i=0;i<row;i++) cout <<" enter "<<column<<" numbers to fill row number " << i<<endl; for (int j=0;j<column ;j++) cin>>p[i][j]; cout<<endl;} }

Example void print(int **p, int row ,int column) { cout << " the board is : "<<endl; for (int i=0;i<row;i++) { for (int j=0;j<column ;j++) cout << p[i][j]<< " "; cout<< endl; }