Download presentation
Presentation is loading. Please wait.
Published byRoger Mosley Modified over 9 years ago
1
UNIT II
2
-set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and size. -share a common name. Eg: -Differentiated from one another by their positions. Eg: We refer to each item in an array as an element. The position of each element is known as its index. -Each array element is referred by specifying the array name, followed by a number(with square braces)as an index or subscript. Eg:
4
Array declarations similar to variables, but use square brackets: – datatype[] name; For example: – int[] prices; Can alternatively use the form: – datatype name[]; Eg: int prices[];
5
------Riddles-----------------------
6
------Riddles------------------------------ Each morning I appear To lie at your feet, All day I will follow No matter how fast you run, Yet I nearly perish In the midday sun.
7
Unlike variables, we need to allocate memory to store arrays. (malloc() in C.) Use the new keyword to allocate memory: name = new type[size]; Eg: prices = new int[3];
8
Every element in an array is referenced by its index. If the array prices has size 3, its valid indices are 0, 1, and 2. Beware “Array out of Bounds” errors. Assigning variables: Example assigning values to each element of prices: prices[0] = 6; prices[1] = 80; prices[2] = 10;
9
We assign values to elements of String arrays in a similar fashion: string[] name; Eg: String[] people; people = new String[4]; people[0] = ”Alice”; people[1] = ”Bilha”; people[2] = ”Chris”; people[3] = ”David”;
10
You can also specify all of the items in an array at its creation. Eg: String[] people = {“Alice”, “Bilha”, “Chris”, “David”}; int[] prices = {6, 80, 10}; All the items must be of the same type. Array names used to identify the array.
11
Allocate - Create empty space that will contain the array. Initialize - Fill in a newly allocated array with initial values. Element - An item in the array. Index - Element’s position in the array. Size or Length - Number of elements.
12
My life can be measured in hours, I serve by being devoured. Thin, I am quick Fat, I am slow Wind is my foe. ?
13
I am seen in the water If seen in the sky, I am in the rainbow. ?
14
Two types of Array 1.Singly Dimensional Arrays. 2.Doubly Dimensional Arrays.
15
Initializing an array : ->during declaration. ->using loops. Initializing an array during declaration: -General form. Eg: int mark[7]={100,45,60,80,75}; char name[8]={`s`,`u`,`d`,`h`,`a`,`0`}; Syntax:? How mark array will get allocate? ->Implementation of Singly linked list.
16
Initializing an array : ->during declaration. ->using loops. Initializing an array using loops: -can be initialized by using for loop, a while loop or a do-while loop. -size should be a +ve integer constant, indicate the maximum no: can be stored. ->Implementation of Singly linked list.
17
Initializing an array using loops: -can be initialized by using for loop, a while loop or a do-while loop. -size should be a +ve integer constant, indicate the maximum no: can be stored. -In an n-element array, elements are stored in x[0],x[1],…………,x[n-2],x[n-1]. Eg: Fig:M/y allocation of a singly dimensional array. Eg: ->Implementation of Singly linked list.
18
-subscript specifies the elements ?? -array starts from?? Eg: Eg:for initializing an array using a for loop: 1) int i, mark[50]; 2)int i,mark[50],x=5; for(i=0;i<50;i++) for(i=0;i<50;i++) { mark[i]=0; mark[i]=x; } x+=5; o/p: ? } o/p:? ->Accessing array elements.
19
3)int i,mark[50]; for(i=0;i<50;i++) { scanf(“%d”,&mark[i]); } o/p: ? Similar for while and do while. ->Accessing array elements.
20
----------Riddles----------------------- You heard me before, Yet you hear me again, Then I die, 'Till you call me again. ?
21
Two in a corner, 1 in a room, 0 in a house, but 1 in a shelter. What am I?
22
->Eg: int values[5]={21,5,79} -other space set to 0. ->Eg:int value[2]={4,3,5,6,7} -Error. ->not necessary to specify the length of an array. Eg: int value[]={5,4}; Eg: ->Rules for initializing an array
23
Eg: void main() { int a[5],sum=0,i; printf(“enter 5 integer number\n”); for(i=1;i<=5;i++) { scanf(“%d”,&a[i]); sum=sum+a[i]; } printf(“the sum of given no: %d\n”,sum); }
25
Elements are specified by 2 subscripts. i.e it requires two square brackets. ->used for matrix. Eg: Syntax: datatype arrayname[rowsize][columnsize]; Eg: To find total number of elements of a double dimensional array can be calculated from m*n n=column m=row Introduction of two dimensional array.
26
->Initializing an array during declaration. ->Initializing an array during using loops. Initializing an array during declaration: Eg: int mark [4][2]= { {85,56},{95,66}, {76,80},{50,90} }; 4-indicates? 2-indicates? or int mark[4][2]={85,56,95,66,76,80,50,90}; How it will allocate in m\y? Implementing double dimensional arrays.
27
Initializing an array during using loops. Eg: 1)int i,j,x=5,mark[10][5]; for(i=0;i<10;i++) { for(j=0;j<5;j++) { mark[i][j]=x; x+=5; } Implementing double dimensional array
28
Initializing an array during using loops. Eg: 2)int i,j,mark[10][5]; for(i=0;i<10;i++) { for(j=0;j<5;j++) { scanf(“%d”, &mark[i][j]; } Implementing double dimensional array
29
Arrays whose elements are specified by 3,4 subscripts or more said to be multidimensional arrays. Eg:?? Syntax of multidimensional array: ??? Eg:double dimensional matrix Introducing
30
#include void main() { int a[10][10],i,j,m,n,sum=0; printf(“enter the order of matrix:”); scanf(“%d %d”,&m,&n); printf(“enter the elements of matrix:”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } for(i=0;i<m;i++) { for (j=0;j<n;j++) { sum=sum+a[i][j]; } printf(“sum of elements of the matrix is %d”, sum); } Addition matrix Example
31
A structure is a collection of variables of same or different data types grouped together under a single name. Data items ->members of the structure. May contain one or more char,float,arrays,pointer… Uses: ->to organize data,particularly in large programs because they allow a group of related variables to be treated as a single unit.
32
Array ? Elements are referred by ? Members in a structure are referred by their unique name. Structure
33
Eg: struct emp { int empno; char empname[15]; float salary; }; Members? Structure name? So Syntax? Declaring a structure
34
If u break me, I do not stop working, If u touch me, I may be snared,If u lost me nothing will matter.
35
U saw me where I never was and where I could not be, And yet within that very place, my face u often see?
36
syntax struct { datatype member1; datatype member2; “ datatype membern; }structure _variable(s); struct emp { int empno; char empname[25]; float salary; }emp1; Eg:
37
From above example: Structure emp declares a variable ?. The structure_variables used to access the members of the structure. -more than one variable can be declared. Eg:?
38
A member of structure is referred and accessed by the structure variaable. Eg: ->The member access operator also referred as the dot operator “-” to access the member independently. Eg:emp1.empname; emp1.empno; Syntax: ? So emp1 is ?,used to access the members empname,empno using member access operator.
39
Eg emp1.empname=“Eve” To display the Value: Eg: puts(emp1.empname);
40
#include struct student { char name[10]; Int age; Int roll_no; }stud={“deepti”, 23, 938}; Void main() { printf(“\nName of student is %s”,stud.name); printf(“\nAge of student is %s”,stud.age); printf(“\nRoll no of student is %s”,stud.roll_no); } Output: Name of student is deepti Age of student is 23 Rollno of student is 938
41
I am always hungry, I must always be feed, the finger I touch will soon turn red.
42
I am a box that holds keys without locks,yet they can unlock ur souls.?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.