Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.

Similar presentations


Presentation on theme: "1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh."— Presentation transcript:

1 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

2 Arrays and Strings Array Fundamentals Two Dimensional Arrays Function Declaration with Array Argument Arrays of Structures Arrays As Class Member Data Arrays of Objects C-Strings The Standard C++ string Class 2

3 Arrays and Strings Array Fundamentals Definitions of array Use of Arrays Arrays Elements Initialization of Arrays Programs: Basic Array with for loop Calculate no. of days of year Calculate average sales Average of marks Copying array Array of characters 3

4 Arrays Fundamentals Definition: An array is an aggregate data type that lets you access multiple variables of same type through a single name by use of an index. OR An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. 4

5 Arrays Fundamentals  Use of Arrays: When multiple, similar type of variables are used to store data. Example 1: We can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. Example 2: To store the record of students in class we need 50 variables with different names, by using array we could manage data by just one name. 5

6 Arrays Fundamentals Arrays Elements: The items in an array are called elements (in contrast to the items in a structure, called members). As we noted, all the elements in an array are of the same type; only the values vary. Figure 7.2 shows the elements of the array age. Arrays are LIFO (Last-in First-out) structure. It means elements included in Last will be manipulated First. 6

7 Arrays Fundamentals Definition of Arrays Definition of arrays includes three parts: 1. Datatype of array: Type of data elements included in array. 2. Name of array: Any name relevant to data elements. 3. Size of array: It defines how many data elements are included in array. It is antonym of structure. Structure is collection of different types of data members. 7

8 Arrays Fundamentals 8 Figure7.1: Definition of Array (Datatype, Name & Size) Figure 7.2: Arrays Elements

9 Arrays Fundamentals (Index) The elements of the array are manipulated using the index. The index of array starts from zero and is one less than array's size. Index of array is also called subscript. 9 The indexing of the array starts from zero, not from one. So in the above example, the index of the array C will be from C[0] to C[9].

10 Arrays (Declaration) Every array has a data type i.e. name and size. The rules of variable naming convention apply to array names. The size of the array tells how many elements are there in the array. The arrays occupy the memory depending upon their size and have contiguous area of memory. Declaration: Syntax: data_typearray_name [size] ; Example: int ages[10]; The array occupies the contiguous area, in this case it will occupy forty bytes (one int = 4 bytes) 10

11 Arrays (Declaration) Arrays may be declared with simple variables in a single line. int i, age [10]; int height [10], length [10] ; To access array, we can’t use the whole array at a time. We access arrays element by element. An index (subscript) may be used to access the first element of the array. In this case, to access first element we write like age[0]. To access the 5 th element, we will write age[4] and so on. 11

12 Arrays Fundamentals Initialization of Arrays It includes definition and starting values of each element of array. Note: Don't use the default initialization of arrays. Compiler may assign some value to each declared array. 12 Figure 7.3: Syntax of Initialization of Arrays

13 Arrays Fundamentals (Initialization…) 13 int i, age [10]; for ( i = 0; i < 10 ; i++ ) { age[i] = 0; } With the help of this simple loop, we have initialized all the elements of array age to zero. int age [10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; With the help of this simple loop, we have initialized all the elements of array age to zero. int age [10] = { 0}; Same could be performed by using following shortcut. int age [ ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; The compiler detect the initialization list which consists of ten 0’s. Therefore, it creates an array of 10 integers and initializes all the elements with zero.

14 Arrays Fundamentals (Program: Basic Array with for loop) #include int main() { int age[4] ; //array ‘age’ of 4 integers for(int j=0; j<4; j++) //get 4 ages { cout << “Enter an age: “; cin >> age[j]; //access array element } for(j=0; j<4; j++) //display 4 ages cout << “You entered “ << age[j] << endl; } 14 Write a program using array, which get ages of four persons and return back all ages in same sequence.

15 Arrays Fundamentals (Program: Calculate no. of days of year) #include int main() { int month, day, total_days; int days_per_month[12] = { 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 }; cout << “\nEnter month (1 to 12): “; //get date cin >> month; cout << “Enter day (1 to 31): “; //insert date of month cin >> day;//date of month saved in day variable total_days = day; //separate days for(int j=0; j<month-1; j++) //add days each month total_days += days_per_month[j]; cout << “Total days from start of year is: “ << total_days<< endl; } 15 Write a program which calculates the number of days from the beginning of the year to a date specified by the user.

16 Arrays Fundamentals (Program: Calculate average sales) //Sales.cpp #include int main() { const int SIZE = 6; //size of array double sales[SIZE]; //array of 6 variables cout << "Enter sales for 6 days\n"; for(int j=0; j<SIZE; j++) //put figures in array cin >> sales[j]; double total = 0; for(j=0; j<SIZE; j++) //read figures from array total += sales[j]; //to find total double average = total / SIZE; // find average cout << "Average = " << average << endl; } 16 Write a program which calculates the number of average sales of six days.

17 Arrays Fundamentals (Program: average of marks) #include int main() { int sum=0; int marks[3]; for(int a=0;a<3;a++) { cout<<"please insert marks of subject "<<endl; cin>>marks[a]; cout<<endl; sum=sum+marks[a]; } cout<<"The average is: "<<sum/(sizeof(marks)/2)<<endl; } 17 Write a program using array, which gets marks of 4 subjects and provide sum and average of marks. Also use sizeof() function.

18 Arrays Fundamentals (Program: Copying arrays) #include int main() { int ages[4]={5,10,15,20}; int same_age[4]; same_age[0]=ages[0]; same_age[1]=ages[1]; same_age[2]=ages[2]; same_age[3]=ages[3]; for(int k=0;k<4;k++) cout<<"Output of new array: "<<same_age[k]<<endl; //or for(int i=0;i<4;i++) { same_age[i]=ages[i]; cout<<"Output of new array: \t"<<same_age[i]<<endl; } 18 Write a program which copy values from one array to another array.


Download ppt "1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh."

Similar presentations


Ads by Google