KEMBAR78
Array 2d | PDF | Integer (Computer Science) | Data Type
0% found this document useful (0 votes)
19 views51 pages

Array 2d

Uploaded by

ayushabc247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views51 pages

Array 2d

Uploaded by

ayushabc247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Arrays in C:

Data types

Prepared by Mrs G.S.Patil


Examples of variable declaration

Prepared by Mrs G.S.Patil


Consider the following issue:

• "We have a list of 1000


students marks of an integer
type. If using the basic data
type (int), we will declare
something like the following…"

• int studMark0, studMark1,


studMark2, ..., studMark999;

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Program to Display Five intergers number using Array datatypes.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf("%d ",&a[i]);
}
printf("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%d ",a[i]);
}
getch();
}
 Can you imagine how long we have to write
the declaration part by using normal
variable declaration?
– int main(void)
– {
• int studMark1, studMark2, studMark3, studMark4,
…, …, studMark998, stuMark999, studMark1000;
• …
• …
• getch();
– }
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Some examples:
• List of employees in an organization.
• List of products and cost sold by the
store.
• Test scores of class of students.
• List of customers and their telephone
numbers .
• And so on...........
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
ARRAYS:
• An array is a collection of data that holds fixed
number of values of same type.
• It is a Derived data type.
• It is a simple and fast way of storing multiple
values under a single name.
• An array in C is a fixed-size collection of similar
data items stored in continuous memory
locations.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
ARRAYS:
• The size and type of arrays cannot be changed
after its declaration. .
• An array in C is a fixed-size collection of similar
data items stored in continuous memory
locations.
• An array is a collection of data that holds fixed
number of values of same type. .

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
• If you want to store marks of 100 students you can create an array for it.
• float Marks[100];

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Memory representation in an array

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Types of Arrays:
• One Dimensional Arrays
• Two Dimensional Arrays
• Multidimensional Arrays

Note: Dimension refers to the array's size,


which is how big the array is.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
One dimensional array in C
• One-dimensional arrays, also known
as single arrays, are arrays with only
one dimension or a single row.

• A list of items can be given one


variable name using one subscript
(index) and such a variable is called a
single-subscripted variable or a one-
dimensional array
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
For Example –
 If we want to represent a set of five numbers say (35, 40, 20, 57,
19), by an array variable number, then number is declared as → int
number [5];
 Computer store (35, 40, 20, 57, 19) as shown below:
The values can be assigned to the array as:
• number [0] = 35;
• number [1] = 40;
• number [2] = 20;
• number [3] = 57;
• number [4] = 19;

 Declaration of One-dimensional array

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Declaration of one-dimensional
array
Syntax
type variable-name[size];
Here the type specifies the data type of
elements contained in the array, such as int,
float, or char.
variable-name specifies the name of the array.
[size] represents the maximum number of data
elements an array can store. It can be a numeric
constant or a symbolic constant.
Prepared by:Mrs.G.S.Patil(Assistant
ProfessorETC)
• Declare an array to contain the numbers of 5
integer constants.
int number[5];
• The compiler reserves five storage locations.
The general form of array declaration is:
type array-name[size];
 Here the type specifies the data type of elements contained in the
array, such as int, float, or char.
 The size indicates the maximum numbers of elements that can be
stored inside the array.
For example: int rollno [10] ;
 Here int is datatype, rollno is a variable name, 10 is a size of array
and the subscripts (index) is start from 0 to 9.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Initialization of one-dimensional array

• After declaration an array needs to


be initialized i.e. assign values to data
elements.
• Initialization can be done :
1. At Compile time
2. At run time
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
At Compile time

• Values of data elements can be initialized at the time of


declaration.
Syntax
type array-name[size]={list of values};
The values in the list are separated by commas.
int number[3]={4,7,3};
float total[5]={0.0,15.57,-10};
int c[]={1,2,3,4};//size is omitted.
int number[3]={13,46,365,346};//This is not allowed

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
• Initialization of One-dimensional array
After an array is declared, its elements must be initialized.
In C programming an array can be initialized at either of the following stages:
 At compile time
 At run time

 Compile time initialization


▪ In compile time initialization, the array is initialized when they are
declared.
▪ The general form of initialization of array is:
type array-name[size] = { list of values };
The list of values is separated by commas.
Example: int number[3] = {4, 5, 9};
Here declare the variable ‘number’ as an array of size 3 and will assign
the values to each elements.
4 is assign to first element(number[0]),
5 is assign with second element(number[1]),
9 is assign with third element(number[2]).
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
 If the number of values in the list is less than the length (size),
then only that many elements will be initialized.
 The remaining elements will be set to zero automatically.
 If we have more initializers than the declared size, the compiler
will produce an error.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Example of 1D array
• Suppose we want to store roll no of 100
students , then we declare variable roll_no as:
int roll_no[100];

i.e.roll_no[0], roll_no[1], roll_no[2], roll_no[3],


………………. roll_no[99]

The subscript begin with number 0.


Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Program based on One Dimensional Array (Compile Time)

#include<stdio.h>
#include<conio.h>
void main()
{
int roll_no[5]={10,20,30,40,50};
printf("%d",roll_no[0]);
getch();
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Example for Compile Time Array (Character Data Type)

#include<stdio.h>
#include<conio.h>
void main()
{
char alpha[5]={'X','N','T','U','Z'};
clrscr();
printf("%c",alpha[2]);
getch();
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Example for Compile Time Array (Float Data Type)

#include<stdio.h>
#include<conio.h>
void main()
{
float perc[5]={37.85,45.67,31.29,51.67,78.23};
clrscr();
printf("%f\t""%f",perc[2],perc[3]);
getch();
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
At Run time :

 An array can also be initialized at runtime using


scanf() function.
 This approach is usually used for initializing large
arrays, or to initialize arrays with user specified
values.
 To input elements in an array, we can use a for loop
or insert elements at a specific index.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
At Run time :

• An array can be initialized at run time i.e.after


execution of program.
• Usually applied for initializing large arrays.
for (i=0;i<50;i++)
{
arr[i]=i+1;
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Program to Display Five Characters using Array datatypes (run time).

#include<stdio.h>
#include<conio.h>
void main()
{
char alpha[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf ("%c",&alpha[i]);
}
printf ("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%c\t",alpha[i]);
}
getch();
}
Q. Program to Display Five Decimal values using Array datatypes (run time).

#include<stdio.h>
#include<conio.h>
void main()
{
float alpha[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf ("%f",&alpha[i]);
}
printf ("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%f\t",alpha[i]);
}
getch();
}
 A variable which represents the list of items using two index (subscript) is
called two-dimensional array.
 Two-dimensional array is known as matrix.
 In Two dimensional arrays, the data is stored in rows and columns format.
 2-d array is a collection of 1-D array placed one below the other.
Its syntax is:
Data-type array name[row][column];

Total no. of elements in 2-D array is calculated as row*column


• Example:
int a[2][3];
Total no of elements= row*column = 2*3 = 6
It means the matrix consist of 2 rows and 3 columns

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
 A two-dimensional array a, which contains 2 rows and 3 columns can be shown as
follows:

For example: int a[2][3] = {20, 2, 7, 8, 3, 15};

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Declaration of Two-dimensional arrays:
 The general form of two-dimensional array declaration is:

type array-name[row_size][column_size];

Here the type specifies the data type of elements contained in the array, such as int, float, or
char.
The size indicates the size of number of rows and number of columns.

Initialization of Two-dimensional arrays:


 The general form of initializing two-dimensional array is :

type array-name[row_size][column_size] = {list of values};

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Example: int table[2][3] = {0, 0, 0, 1, 1, 1};
 Here the elements of first row initializes to zero and the elements of second row initializes to
one.
 This above statement can be written as :
int table[2][3] = {
{0,0,0},
{1,1,1}
};
 In two-dimensional array the row size can be omitted.
Example:
int table[ ][3] = {{0,0,0}, {1,1,1}};

 If the values are missing in an initializer, they are automatically set to zero.
Example:
int table[2][3] = {1,1,2};

 Here first row initializes to 1,1 and 2 and second row initialize to 0,0 and 0 automatically.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Memory layout of Two-dimensional arrays:

In Two dimensional arrays, the data is stored in rows and columns format.
For example:
int table[2][3] = {1,2,3,4,5,6};
The memory layout of above example:
table[0][0] = 1;
table[0][1] = 2;
table[0][2] = 3;
table[1][0] = 4;
table[1][1] = 5;
table[1][2] = 6;

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
TWO-DIMENSIONAL ARRAYS
• To store data in table form 2D Arrays are used.
• For example:
Roll No subject1 subject2 subject3 subject4 subject5
209 100 47 58 39 57
89 75 59 49 59 58
67 89 29 67 9 39
489 67 59 67 58 48
46 4 59 58 79 74
78 98 35 75 57 46

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
• Two-dimensional array has
-Rows
-Columns

• A list of items can be given one variable name


using two subscripts and such a variable is a
two-dimensional array.

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Declaration of 2D Array:
• type array-name[row-size][colum-size];
• Example:
int value[4][3];
• first index:-No. of rows
• Second index:-No.of columns

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Representation of 2D array in memory:

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Initialization of 2D arrays:
• int table[2][3]={0,0,0,1,1,1};
• int table[2][3]={{0,0,0},{1,1,1}};
• int table[2][3]={
{0,0,0},
{1,1,1}
};

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
• int table[ ][3]={
{0,0,0},
{1,1,1}
};

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
• int table[2][3]={
{0,0},
{1,}
};

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
• When all the elements need to be initialized
to 0 :
• int table[2][3]={{0},{0}}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. 2 Dimensional array program with compile time initialization.(integer type)

#include<stdio.h>
#include<conio.h>
void main()
{
int s[2][3]={
{7,8,9},
{6,5,4}
};
int i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%d\t",s[1][2]);
}
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. 2 Dimensional array program with compile time initialization.(character type)

#include<stdio.h>
#include<conio.h>
void main()
{
char s[2][3]={
{'A','W','T'},
{'R','V','L'}
};
char i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%c\t",s[1][1]);
}
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. 2 Dimensional array program with compile time initialization.(float type)

#include<stdio.h>
#include<conio.h>
void main()
{
float s[2][3]={
{1.1,2.5,6.7},
{2.1,6.3,4.1}
};
float i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%f\t",s[0][1]);
}
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Program for Declaration of two dimensional array (Run-time)

#include<stdio.h>
#include<conio.h> printf("Two D array elements are\n");
int main() for (i=0;i<2;i++)
{ {
int a[2][3],i,j; for (j=0;j<3;j++)
printf("Enter array element"); {
for(i=0;i<2;i++) printf("%d",a[i][j]);
{ }
for (j=0;j<3;j++) printf("\n");
{ }
printf("\nelement a[%d][%d]= ",i,j); return 0;
scanf("%d",&a[i][j]); }
}
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Program for Declaration of two dimensional array(Float Data type)

#include<stdio.h>
#include<conio.h> printf("Two D array elements are\n");
void main() for (i=0;i<2;i++)
{ {
float a[2][3],i,j; for (j=0;j<3;j++)
printf("Enter array element"); {
for(i=0;i<2;i++) printf("%f",a[i][j]);
{ }
for (j=0;j<3;j++) printf("\n");
{ }
printf("\nelement a[%f][%f]= ",i,j); getch();
scanf("%f",&a[i][j]); }
}
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Q. Program for Declaration of two dimensional array(Char Data type)

#include<stdio.h> fflush(stdin);
#include<conio.h> scanf("%c",&a[i][j]);
int main() }
{ }
char a[2][3]; printf("Two D array elements are\n");
int i,j; for (i=0;i<2;i++)
clrscr(); {
printf("Enter array element"); for (j=0;j<3;j++)
for(i=0;i<2;i++) {
{ printf("%c\t",a[i][j]);
for (j=0;j<3;j++) }
{ printf("\n");
printf("\nelement a[%d][%d]= ",i,j); }
getch();
return 0;
}

Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)
Prepared by:Mrs.G.S.Patil(Assistant
Professor ETC)

You might also like