0% found this document useful (0 votes)
22 views27 pages

Array

Here is a C program that multiplies two 2x2 matrices: #include <stdio.h> int main() { int a[2][2], b[2][2], mul[2][2], i, j, k; printf("Enter elements of first matrix:\n"); for(i=0; i<2; i++) for(j=0; j<2; j++) scanf("%d", &a[i][j]); printf("Enter elements of second matrix:\n"); for(i=0; i<2; i++) for(j=0; j<2; j++) scanf("%d", &b

Uploaded by

apexg786
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
22 views27 pages

Array

Here is a C program that multiplies two 2x2 matrices: #include <stdio.h> int main() { int a[2][2], b[2][2], mul[2][2], i, j, k; printf("Enter elements of first matrix:\n"); for(i=0; i<2; i++) for(j=0; j<2; j++) scanf("%d", &a[i][j]); printf("Enter elements of second matrix:\n"); for(i=0; i<2; i++) for(j=0; j<2; j++) scanf("%d", &b

Uploaded by

apexg786
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 27

Array

INTRODUCTION
ONE-DIMENSIONAL ARRAY
MULTIDIMENSIONAL ARRAY
Introduction

An array is a sequence of homogenous elements


It holds multiple values of same type.
Each block of array is stored consecutively in
memory.
SYNTAX:
data-type name[size];
Example:
int a[6];
Arrays always start with 0 and end with [size-1]
One dimensional Array

An array is a data structure consisting of a collection of elements (values


or variables), each identified by at least one array index
SYNTAX:
data-type name[index];
EXAMPLE:
int num[10];
Initialization

int num[6]={2,4,6,7,8,12};

Individual elements can also be initialize as:


 num[0]=2;
 num[1]=4;
 num[2]=6;
 num[3]=7;
 num[4]=8;
 num[5]=12;

A specific element in an array is accessed by an index.


Arrays: Example
#include<stdio.h>
#include<conio.h>
int main()
25 age[0]
{

int age[3];
30
age[1]
age[0] = 25;
age[1] = 30;
age[2] = 35; 35
age[2]

for (int j=0; j<3; j++)


printf("%d\n",age[j]);
getch(); }
Reading Data from User

for loop is used to read data from the user.


Arrays: Example
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int main() int main()
{
{
int age[3];
int age[3];

age[0] = 25; for (int i = 0; i<3; i++) {


age[1] = 30; printf("Enter ages \n");
age[2] = 35; scanf("%d",&age[i]);}
printf("Ages are ");
printf("Ages are "); for (int j=0; j<3; j++)
for (int j=0; j<3; j++)
printf("%d \n",age[j]);
printf("%d\n",age[j]);
getch();
}
getch();
}
Initializing Arrays in Declarations

• Possible to declare the size & initialize


int results [5] = {14, 6, 23, 8, 12 }

• Possible to omit size at declaration


– Compiler figures out size of array

float prices [ ] = { 2.41, 85.06, 19.95, 3.91 }


Arrays Initialization: Example
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>

Empty brackets
int main() int main() can take any
{ size
{

int age[3] = {25, 30, int age[ ] = {25, 30, 35};


35};
for (int j=0; j<3; j++)
for (int j=0; j<3; j++) printf("%d\n",age[j]);
printf("%d\n",age[j]); getch();
getch(); }
}
Arrays: Class
Exercise
#include<stdio.h>
Write a C program #include<conio.h>
using arrays that int main()
accepts five (05) {
integers and then int order[5];
printf("Enter numbers \n");
prints them in
for(int i=0; i<=4; i++)
reverse order. scanf("%d ", &order[i]);
for (int j=4; j>=0; j--)
printf("%d\n", order[j]);
getch();
}
Why we need Array in Programming?

 Consider a scenario where you need to find out the average


of 100 integer numbers entered by user. In C, you have two
ways to do this:
1) Define 100 variables with int data type and then perform
100 scanf() operations to store the entered values in the
variables and then at last calculate the average of them.
2) Have a single integer array to store all the values, loop the
array to store all the entered values in array and later calculate
the average.
 Which solution is better according to you? Obviously the
second solution, it is convenient to store same data types in
one single variable and later access them using array index
Advantage of Array

Huge amount of data can be stored under single


variable name.
Searching of data item is faster.
2 dimension arrays are used to represent the
matrices.
It is helpful in implementing other data structure
like linked list, queue,stack.
Class work

WAP to read 10 numbers from the user and display


them.
WAP to read 20 numbers from the user and find out
the highest number.
2-Dimensional Arrays

• A collection of a fixed number of components


arranged in two dimensions
– All components are of the same type
• The syntax for declaring a two-dimensional
array is:
dataType arrayName[intexp1][intexp2];
where intexp1 and intexp2 are expressions
yielding positive integer values; e.g., double
sales[10][5]
2-Dimensional Arrays

• The two expressions intexp1 and intexp2 specify


the number of rows and the number of columns,
respectively, in the array

• Two-dimensional arrays are sometimes called


matrices or tables
2-Dimensional Arrays

double sales[10][5];
2-Dimensional Arrays

The syntax to access a component of a two-


dimensional array is:
arrayName[indexexp1][indexexp2]
where indexexp1 and indexexp2 are expressions
yielding nonnegative integer values
indexexp1 specifies the row position and
indexexp2 specifies the column position
2-Dimensional Arrays

sales[2][3] = 35.60;

35.60
2-Dimensional Arrays Accessing

 Accessing all of the elements of a two-dimensional array


requires two loops: one for the row, and one for the column.
 Since two-dimensional arrays are typically accessed row by
row, generally the row index is used as the outer loop.

for (int nRow = 0; nRow < nNumRows; nRow++)


for (int nCol = 0; nCol < nNumCols; nCol++)
printf(“%d”,anArray[nRow][nCol]);
2 DIM. Arrays: Example
#include<stdio.h>
#include<conio.h>
//complete program
int main() by //printing the
{ values which look like
double sales[2][3]; this:
sales[0][0] = 2.3;
sales[0][1] = 3.5;
sales[0][2] = 4.2;

sales[1][0] = 5.6;
sales[1][1] = 6.7;
sales[1][2] = 7.8;
2-Dimensional Arrays Initialization

 Like one-dimensional arrays


 Two-dimensional arrays can be initialized when
they are declared
 To initialize a two-dimensional array when it is
declared
1) Elements of each row are enclosed within braces and
separated by commas
2) All rows are enclosed within braces
3) For number arrays, if all components of a row are not
specified, the unspecified components are initialized
to zero
2-Dimensional Arrays Initialization

 Example:

int anArray[3][5] =
{
{ 1, 2, 3, 4, 5, }, // row 0
{ 6, 7, 8, 9, 10, }, // row 1
{ 11, 12, 13, 14, 15 } // row 2
};
2 DIM. Arrays: Example
#include<stdio.h>
#include<conio.h> for(int i = 0; i < 2; i++)
int main() {
{ for(int j = 0; j < 2; j++)
int matrix[2][2] = { {
{2,3,}, //row0
printf(" %d", matrix[i][j]);
{5,7} //row1 }
};
printf("\n"); }
printf("\n Resultant \n");
getch();
}
2 DIM. Arrays: Class Exercise
Write a C program using 2 DIM. arrays that gets 2x2
matrix input from the user and then prints the
resultant matrix. The output should look like this:
2 DIM. Arrays: Exercise Solution

#include<stdio.h> printf("\n");
#include<conio.h>
int main() for(int i = 0; i < 2; i++)
{ {
int matrix[2][2]; for(int j = 0; j < 2; j++)
{
for(int i = 0; i < 2; i++) printf(" %d",matrix[i][j]);
{ }
for(int j = 0; j < 2; j++) printf("\n");
{ }
printf("Enter values for [%d %d] getch();
",i,j); }
scanf("%d",&matrix[i][j]);
}}
2 DIM. Arrays: Class Exercise

Write a C program
using 2 DIM. arrays
that gets two 2x2
matrices as an input
from the user and
then prints the sum
of entered matrices.
The output should
look like this:
2 DIM. Arrays: Assignment

1) Write a C program using arrays that


produces the multiplication of two
matrices.

You might also like