Array
Array
INTRODUCTION
ONE-DIMENSIONAL ARRAY
MULTIDIMENSIONAL ARRAY
Introduction
int num[6]={2,4,6,7,8,12};
int age[3];
30
age[1]
age[0] = 25;
age[1] = 30;
age[2] = 35; 35
age[2]
Empty brackets
int main() int main() can take any
{ size
{
double sales[10][5];
2-Dimensional Arrays
sales[2][3] = 35.60;
35.60
2-Dimensional Arrays Accessing
sales[1][0] = 5.6;
sales[1][1] = 6.7;
sales[1][2] = 7.8;
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