Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Khizar Hayat Associate Prof. of Computer Science

Similar presentations


Presentation on theme: "Dr. Khizar Hayat Associate Prof. of Computer Science"— Presentation transcript:

1 Dr. Khizar Hayat Associate Prof. of Computer Science
Arrays Dr. Khizar Hayat Associate Prof. of Computer Science

2 Arrays An array is a group of elements of the same data type that are placed in contiguous (adjacent) memory locations. Each element of the array is identified by an index. The number of rows/columns of an array are called its dimensions: One Dimensional Array Two Dimensional Array

3 Declaring the C++ arrays
One dimensional arrays are declared in C++ by first specifying the data type, then name and then the number of locations in square brackets ([]), e.g. int varname[50];//declares an integer array of size 50 float real[30];//declares a float array of size 30 char ch[25];//declares a character array of size 25

4 Initializing array You can declare and initialize arrays in a single statement, e.g. int foo[5]={16, 2, 77, 40, 12071}; This statement declares an array (named foo) that can be represented like this: 1 2 3 4 16 77 40 12071

5 Initializing array - 2 The number of values between braces, {}, must not be greater than the number of elements. The number may however be lesser, e.g. int bar[6]={10, 20, 30}; This statement declares an array (named bar) that can be represented like this: Another example: int baz[5]={}; 1 2 3 4 5 10 20 30 1 2 3 4 Note that exceeding the element limit is not a syntax error.

6 Accessing the array values
Remember: int foo[5]={16, 2, 77, 40, 12071}; This array is outlined like below: Index Value The index is also called the subscript. 1 2 3 4 foo[0] foo[1] foo[2] foo[3] foo[4]

7 Example #include<iostream> using namespace std; int main () { int a[5]; a[0]=10; a[1]=20; cout<<“Enter the value of 3rd element”<<endl; cin>>a[2]; a[0]=a[0]*5; cout<<a[0]<<“\t”<<a[1]<<“\t”<<a[2]<<endl; return 0; }

8 Accessing array elements: Example
#include<iostream> using namespace std; int main () { int a[10],i; for (i=0;i<10;i++) //To read and store the inputs cout<<“Enter the value of <<i<<“th element”<<endl; cin>>a[i]; } for (i=0;i<10;i++) //To display the contents of array cout<<“The value of a[“<<i<<“] is”<<a[i]<<endl; return 0; Output at page 8-9 of notes

9 Two-Dimensional Arrays
Two-dimensional Array: a collection of a fixed number of components arranged in two dimensions All components are of the same type The syntax for declaring a two-dimensional array is: dataType arrayName[rowsize][colsize]; where rowsize and colsize are expressions yielding positive integer values

10 Two-Dimensional Arrays (continued)
The two expressions rowsize and colsize specify the number of rows and the number of columns, respectively, in the array Two-dimensional arrays are sometimes called matrices or tables

11

12 Two-Dimensional Arrays (continued)
A First Book of C++: From Here To There, Third Edition

13 Accessing Array Components
The syntax to access a component of a two-dimensional array is: arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values indexexp1 specifies the row position and indexexp2 specifies the column position

14

15 Processing Two-Dimensional Arrays
A two-dimensional array can be processed in three different ways: Process the entire array Process a particular row of the array, called row processing Process a particular column of the array, called column processing

16 Processing Two-Dimensional Arrays (continued)
Each row and each column of a two- dimensional array is a one-dimensional array When processing a particular row or column of a two-dimensional array we use algorithms similar to processing one- dimensional arrays

17 Examples Adding two matrices Multiplying two matrices
Column sums and row sums

18 Strings A C-style string is a one-dimensional array of characters which is terminated by a null character '\0‘, e.g. char greeting[6] = {'H','e','l','l','o','\0'}; Another easier way: char greeting[] = "Hello"; With this type of initialization, no need to place the null character at the end of a string constant. The C++ compiler automatically places the '\0' at the end of the string when it initializes the array. In C++, any thing enclosed inside double quotes (“”) is a string of enclosed characters plus ‘\0’

19 Strings: Example #include <iostream> using namespace std; int main () { char greeting[6]={'H','e','l','l','o','\0'}; cout << "Greeting message: "; cout << greeting << endl; return 0; }

20 Strings: Example #include <iostream> using namespace std; int main () { char greeting[]=“Hello”; cout << "Greeting message: "; cout << greeting << endl; return 0; }

21 Accessing string elements
You can access elements of a string via for loop and array subscripts but: Manipulation of Character Arrays or Strings do not require any for loop You can access the string by just using its identifier (see example - next slide) Note, However, when giving input to a char array, blank spaces are not allowed. Blank space in input means the end of array and anything after it will not be stored.

22 Accessing string elements
#include <iostream> using namespace std; int main () { char question[]=“Please, enter your first name:”; char greeting[]=“Hello ”; char fname[80]; cout<<question<<endl; cin>>fname; cout<<greeting <<“Your first name is…”<<fname<<endl; return 0; } Output Please enter your first name: Asma Hello Your first name is…Asma

23 String Functions from <cstring>
C++ supports a wide range of functions that manipulate null-terminated strings: S.N. Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.


Download ppt "Dr. Khizar Hayat Associate Prof. of Computer Science"

Similar presentations


Ads by Google