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

Array-1

2

Uploaded by

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

Array-1

2

Uploaded by

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

Chapter 5

Exploring Array
How to store math marks for all Students?
• Suppose we have 10 students.

• So, we have to keep in mind two things-


1. Roll of a student
2. Mark of that student

• So, we need 10 variable to store marks –


roll_1, roll_2,........., roll_10

• What will happen if we have 100 students? [example]


Types of Array
• One dimensional Array

• Multi dimensional Array


One Dimensional Array
• In C, a one dimensional array is a list of variables that are
all of the same type and are accessed through a common
name.

• An individual variable in the array is called an array


element.
• It is useful to handle groups of related data.
One Dimensional Array….
• General form-
type var_name[size]

▪ type is a valid C data type.


▪ var_name is the name of the array
▪ size specifies the number of elements in the array

Type Array name Size – can hold


100 elements
One Dimensional Array….
• An array element is accessed by indexing the array using the
number of the element.
• In C, all arrays begin at zero.
One Dimensional Array….
Array - 1 (assign value)
Array…. Input from KB

• Array-2 (Input from KB)

• Single element access


Initialize Arrays
Variable Initialization Array Initialization
Be aware!
• Bounds checking
• Assign one entire to another
• Single element access
Assignment 1
Write a C program that will find the largest number among 10 user given
numbers of any data types.

Assignment 2
Find out the output of the following program.

#include<stdio.h>
int main(){
int a[8]={5,2,3,1,4,5,2},i;
for(i=0;i<8;i++){
if(a[i]%2==1)
printf(“%d ”,i);
}
return 0;
}
Array Dimension

One dimensional array consists of only one dimension(row). E.g. int a[10];

Multidimensional array consists of more than one dimension.


E.g: int a[5][2];
int a[5][3][2];
Two – Dimensional Array
• A two dimensional array is an array of
one dimensional arrays and most easily
thought of in row, column format.

• Syntax: data_type name[row][column];


int x[3][3];
2-D Array Initialization
1. Like 1-D array
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}

2. Another approach (Row wise)

int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};


Examples
• Array-3 (Find biggest number)

• Array-4 (Sorting)

You might also like