0% found this document useful (0 votes)
45 views

Std-10-Computer-Chapter 15 Arrays

Ch 10 computer

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Std-10-Computer-Chapter 15 Arrays

Ch 10 computer

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 15 Computer

Arrays Class 10
 In C programming, Basic data types, namely int, float, char, double etc are types of
variables that can store only one value at a time.
 If we want to process five different values, we need five different variables.
 To handle very large amount of data, C supports a special kind of data type called an
array.

Need of Arrays
 Maintain marks of all students of class
 Maintain cost of all the products of super market
 List of employees and their contact numbers
 Maintain height and weight of each student of class

To handle marks of each student, instead of declaring different variables such as


marks0, marks1, marks2, marks3, marks4,…… and marks59.
We declare one variable such as marks[60]
Memory array of marks[59] will look like given below:

First element Second element Third element Last element


marks[0] marks[1] marks[2] marks[3] …. marks[59]

Important points related to an arrays:


 It is a collection of elements having same data type.
 It is a fixed-size sequential collection of elements. An array element occupies
contiguous memory locations.
 The element of an array is accessed by an index number. The index number
contained within square brackets is also known as subscript.
 A subscript number must be an integer or an integer expression.
 The subscript number starts with zero.

Types of Arrays
The arrays in C language can be classified in to following two categories :
(1) Single or One dimensional array
(2) Multidimensional array

The single dimensional array may have one row or one column while multidimensional
array has one or more rows and one or more columns.
The number of rows and columns are specified by user as per the program
requirement.

Presented by Nuzhat Ibrahim Memon 1


Declaration of Single Dimensional Array
To declare an array in C language, we specify the type of data which we want to store in
the elements and the number of elements to be stored in an array as follows:
datatype arrayname [size];
 datatype: It specify the type of elements that an array stores. If we want to store
characters in an array then type of an array is 'char'. In the same way to store
integers, type of an array is 'int'. The datatype can be any valid C language data type.
 arrayname: This is the name given to an array variable by the programmer. It can
be any string but it is usually suggested that some meaningful name should be given
to an array. The array name should be in context with what is being stored in that
array in a program.
 size: The value of size indicates the number of elements the array can stores. The
size must be an integer constant greater than zero.

Example: int marks [60]; Other Example:


datatype arrayname size char strings[20];
float percentages[20];
double numbers[20];
Assigning values to Single Dimensional Arrays
C language allows array initialization in two different ways:
(1) Compile time array initialization
(2) Runtime array initialization

Compile Time Array Initialization


An array can be assigned values similar to a normal variable at the time of declaration.
General syntax to initialize the array elements:
datatype arrayname[size] = { value1, value2, ….,valueN };
value1, value2, ….. ,valueN provide the initial values for successive elements of array.
Example1: int marks[5]={60,65,70,75,80};
 Here marks[0]=60, marks[1]=65, marks[2]=70, marks[3]=75 and marks[4]=80.
Example 2: int marks[5]={60,65,70};
 During such declaration, if list of values are less than the size of an array, then
only specified number of elements will be initialized and remaining elements will
be initialized to zero.
 Initialize first three elements as marks[0]=60, marks[1]=65 & marks[2]=70
whereas remaining two elements ie marks[3] and marks[4] will be initialize to 0
Example 3: int marks[]={60,65,70,75,80};
 If the array elements are assigned value at the time of declaration itself then
specifying size is optional. If we don’t specify the size, compiler will automatically
calculate the size by counting total number of values written inside the curly
bracket { }.
 5 elements are written inside the curly bracket so size of marks array is 5.
Presented by Nuzhat Ibrahim Memon 2
Runtime Array Initialization
When we need to read data from user while executing our program, we use runtime
array initialization.
EXAMPLE:
int marks[5];
printf(“Enter marks of 5 subjects one by one:”);
for(int i=0;i<5;i++){
scanf(“%d”,&marks[i]);
}

String as a Character Arrays


A string is a series of characters that is normally treated as a single block.
C language does not provide the string as inbuilt data type. However it allows us to
represent string as a character array.
char stringname[size];
size indicates the number of characters in variable called stringname.
Note that strings in a C language are terminated by the special character called null
character (‘\0’);This helps the program to identify the end of the string. . Since strings
are terminated by the null character ‘\0’, we require one extra storage location in the
character array to accommodate all the characters of strings.

COMPILE TIME CHARACTER ARRAY INITIALIZATION


EXAMPLE 1: char stud_name[6]={‘P’, ’U’, ‘R’, ‘V’, ‘A’, ‘\0’};
The stud_name is a character array that can store 6 elements as PURVA string contains
5 characters and one element is used for null character ‘\0’
EXAMPLE 2: char state[]={‘G’, ’U’, ‘J’, ‘A’, ‘R’, ‘A’, ‘T’, ‘\0’};
Here without specify the size of array in square bracket, we can directly initialize the
character array by listing elements and specifying the null character at the end. Here
size of state array is 8 (including null character at the end of string)
EXAMPLE 3: char stud_name[6]=”PURVA”;
character array can also be initialized by enclosing the values within double quotes. In
this case, there is no need to end the array with null character (‘\0’)
RUNTIME CHARACTER ARRAY INITIALIZATION
char stud_name[10];
printf(“Enter your name”);
scanf(“%s”,name);

Multidimensional Array
The data associated with certain real life systems (such as digital image, a chess game
board, matrix, etc.) are available in two dimensions.
The simplest form of the multidimensional array is the two-dimensional array (More
than one rows and columns). The two dimensional array have row index as well as
column index.
Presented by Nuzhat Ibrahim Memon 3
Declaration of Two Dimensional Arrays
datatype arrayname [row_size][column_size];
Rowsize and column size must be integer and greater than 0. Example: int sales[3][5];

Compile-time Initialization of Two Dimensional Arrays


datatype arrayname[row_size][col_size]={{val1, val2,val3},{val4, val5, val6} };
Example
int sales[3][5]={{100,150,200,250,200},{200,250,300,350,300},{150,200,250,300,250}};
Memory layout of two dimensional (2D) array with 3 rows and 5 columns
MON TUES WED THURS FRI
Salesman/Day
0 1 2 3 4
SALESMAN1
100 150 200 250 200
0
SALESMAN2
200 250 300 350 300
1
SALESMAN3
150 200 250 300 250
2

Each element is accessed by Index position: arrayname[row_index][col_index]


Salesman/Day 0 1 2 3 4
100 150 200 250 200
0
sales[0][0] sales[0][1] sales[0][2] sales[0][3] sales[0][4]
200 250 300 350 300
1
sales[1][0] sales[1][1] sales[1][2] sales[1][3] sales[1][4]
150 200 250 300 250
2
sales[2][0] sales[2][1] sales[2][2] sales[2][3] sales[2][4]

Run-time Initialization of Two Dimensional Arrays


int sales[3][5];
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
scanf(“%d”,&sales[i][j]);
}
}
Total 15 different values will be read in an array sales[3][5]

No Array Bound Check


Arrays are one of the strength of C language but it is important to note that there is no
array bound check in C language. Array bound check means checking the boundaries of
array declared in a program
EXAMPLE: int number[5];
We can access individual array elements by using number[0], number[1]….number[4]
But accessing number[5] or number[6] refer to illegal index location.
As there is no bound check of array index, we will not get any error but the program
will give us the garbage data or it will crash.
Presented by Nuzhat Ibrahim Memon 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy