unit ii. -set of homogeneous data items. eg: int arrays can hold only integers and char arrays can...
TRANSCRIPT
-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:
What is Array?
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[];
Declaring an Array
------Riddles-----------------------
I have four legs but no tail. Usually I am heard only at night. What am I?
------Riddles------------------------------Each morning I appear
To lie at your feet,All day I will followNo matter how fast you run,Yet I nearly perishIn the midday sun.
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];
Allocating an Array
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;
Array Indices
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”;
Using an Array:
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.
Initializing Arrays
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.
Vocabulary Review
My life can be measured in hours,I serve by being devoured.Thin, I am quickFat, I am slowWind is my foe.
?
-----------Riddles--------------------
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.
Singly linked list
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.
Singly linked list
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.
Singly linked list
-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.
Singly linked list
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.
Singly linked list
----------Riddles-----------------------
You heard me before,Yet you hear me again,Then I die,'Till you call me again.
?
Two in a corner,1 in a room,0 in a house, but 1 in a shelter. What am I?
------------Riddles---------------
->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
Singly linked list
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);}
Program to read 5 data items from i/p and store them in an array and display their sum
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.
Tw o dimensional array or double dimesional array.
->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? orint mark[4][2]={85,56,95,66,76,80,50,90};
How it will allocate in m\y?
Implementing
double
dimensional
arrays.
Tw o dimensional array or double dimesional array.
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
Tw o dimensional array or double dimesional array.
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
Tw o dimensional array or double dimesional array.
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
Multi dimensional array
#include<stdio.h>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
Multi dimensional matrix
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.
Structures
Array?Elements are
referred by?
?Members in a
structure are referred by their unique name.
Fundamental difference b/w An array & Structure:
Structure
Eg: struct emp { int empno; char empname[15]; float salary; };Members?Structure name?So Syntax?
Declaring a
structure
Structure
If u break me ,I do not stop working, If u touch me, I may be
snared ,If u lost me nothing will matter.
------------Riddles-------------------
U saw me where I never was and where I could not be, And yet within that very place, my face u often see?
----------Riddles----------------------------
syntaxstruct
<structure_name>{datatype member1;datatype member2; “ “datatype membern;}structure _variable(s);
struct emp{int empno;char empname[25];float salary;}emp1;
Defining a Structure:It means creating variables to access the members in the structure.Creating a structure variable allocates sufficient m/y space to hold all the members of the structure.Syntax for defining the structure during structure its declaration is ..
Eg:
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:?
Structure variable:
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.
Accessing structure members:
#include<stdio.h> #include<conio.h> 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
Eg:
I am always hungry, I must always be feed, the finger I touch will soon turn red.
--------Riddes------------------------