Uploaded by Omkar

C arrays PPT

advertisement
ARRAYS
By - Tanisha Mittapelli, Dhananjay Kulkarni,
Mayur Harad, Sarth Lakde, Shruti Kadgi, Uday
Bhoyar, Shirin Kulkarni, Omkar Kumbhar,
Sanket Naik, Pushkaraj Mane
What are arrays?
arrays allow you to group values of same data-types
under a single name
you do not need separate variable for each item of
data.
An array has fixed number f data items, called
elements.
For eg,
1. if you want to write a program about storing runs of
individual players in a cricket tournament.
2. you would have to write a using different variable
for each player.
3. Tis is very hectic so using arrays will solve this
problem.
Declaring an array
Data items in an array are reffered to as elements
The elements in an array have to be of same
datatype
While declaring an array, datatype is followed by
name of the array, followed by square brackets []. Eg:
int[10].
. The number inside the square brackets define how
many elements the array contain.Which is also called
size/dimension of the array.
Accessing array elements
Each of the data items stored in an array is
accessed by the same name.
you select a particular element using an
index value between square brackets
following the array name.
index values start from zero.
For eg : index values for the elements in an
array size of 10 would be from 0-9.
zero is the index value for the first array
element.
For eg: for accessing array element 6 you use
the expression arrayName[5], as the index
values start from zero.
Assigning values to an array
A value can be stored in an element of an
array by specifying array element to the left
side of equal sign.
Eg : giga[20]=35;
this means the value 35 is stored in the 20th
element of giga array.
SINGLE / ONE DIMENSIONAL
ARRAY
1D ARRAY:
1. Single or One Dimensional array is used to
represent and store data in a linear form. graph
text.
2. Array having only one subscript variable is
called One-Dimensional array.
3. It is also called as Single Dimensional Array or
Linear Array
DECLARATION OF ONE DIMENTIONAL
ARRAY:Syntax for declaration:
<data type> <array name> [size];
Examples for declaration:
int iarr[3]; char carr[20]; float farr[3];
INITIALIZATION OF ONE DIMENTIONAL ARRAY:Syntax for initialization: [size] = {val1, val2, …,
valn}
Examples for initialization: int iarr[3] = {2, 3, 4};
char carr[20] = “program”;
float farr[3] = {12.5, 13.5, 14.5};
Accessing Array :
INITIALIZATION OF ONE DIMENTIONAL ARRAY:Syntax for initialization: [size] = {val1, val2, …,
1. We all know that array elements are randomly
valn}
accessed using the subscript variable.
Examples for initialization: int iarr[3] = {2, 3, 4};
char
carr[20]
“program”;
2. Array
can= be
accessed using array-name and
float farr[3] = {12.5, 13.5, 14.5};
subscript variable written inside pair of
square brackets [ ].
INITIALIZATION OF ONE DIMENTIONAL ARRAY:-
To access an array element, just write :
array_name[index]
Syntax for initialization: [size] = {val1, val2, …,
valn}
To
access
first
element
of
an
array
:
arr[0]
Examples for initialization: int iarr[3] = {2, 3, 4};
char carr[20] = “program”;
float farr[3] = {12.5, 13.5, 14.5};
To access second element of an array :
arr[1]
And so on ....
In this example we will be accessing array like this
arr[3] = Forth Element of Array
arr[5] = Sixth Element of Array
Whereas elements are assigned to an array using
below way
arr[0] = 51; arr[1] = 32; arr[2] = 43; arr[3] = 24; arr[4] = 5; arr[5] =26;
C program to print the elements of an array
ALGORITHM:
STEP 1: START
STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}.
STEP 3: length= sizeof(arr)/sizeof(arr[0])
STEP 4: PRINT "Elements of given array:"
STEP 5: i=0. REPEAT STEP 6 and STEP 7 UNTIL i<length
STEP 6: PRINT arr[i]
STEP 7: i=i+1.
STEP 8: RETURN 0.
STEP 9: END
PROGRAM:
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array
int length = sizeof(arr)/sizeof(arr[0]);
printf("Elements of given array: \n");
//Loop through the array by incrementing value of i
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
INTRODUCTION TO
2-D ARRAY
DEFINITION ,
DECLARATION ,
AND
INITIALIZATION
2D ARRAY DEFINITION:THE TWO DIMENSIONAL ARRAY CAN BE DEFINED AS
AN ARRAY OF ARRAYS. IT IS ALSO CALLED AS
MATRIX. THE 2D ARRAY IS ORGANIZED AS MATRICES
WHICH CAN BE REPRESENTED AS THE COLLECTION
OF ROWS AND COLUMNS. HOWEVER 2D ARRAYS
ARE CREATED TO IMPLEMENT A RELATIONAL
DATABASE LOOKALIKE DATA STRUCTURE. IT
PROVIDES EASE OF HOLDING THE BULK OF DATA AT
ONCE WHICH CAN BE PASSED TO ANY NUMBER OF
FUNCTIONS WHEREVER REQUIRED.
DECLARATION OF TWO
DIMENTIONAL ARRAY:THE SYNTAX TO DECLARE THE 2D ARRAY IS GIVEN
BELOW:
data_type array_name[rows][columns];
Consider the following example.
int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of
columns.
INITIALIZATION OF
TWO DIMENTIONAL
ARRAY:-
There are two ways to initialize a two Dimensional
arrays during declaration.
int disp[2][4] = {
{10, 11, 12, 13},{14, 15, 16, 17}};
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
Although both the above declarations are valid, I
recommend you to use the first method as it is more
readable, because you can visualize the rows and
columns of 2d array in this method.
EXAMPLE OF TWO
DIMENTIONAL ARRAY:-
1.#include<stdio.h>
2.int main(){
3.int i=0,j=0;
4.int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
5.//traversing 2D array
6.for(i=0;i<4;i++){
7. for(j=0;j<3;j++){
8. printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
9. }//end of j
10.}//end of i
11.return 0;
}
MULTIDIMENSIONAL
ARRAYS
What are multidimensional arrays?
Arrays with two or more dimensions are called as multi
dimensional arrays.
2 dimensional arrays can be generalised to 3 dimensional
arrays and further
3d arrays can be intialised in the following way:
int chad[10][20][30];
you an visualise 1d array as a row of data.
you an visualise 2d array as table of data.
you an visualise 3d array as a stack of tables.
Download