0% found this document useful (0 votes)
13 views9 pages

arrays ppt

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

arrays ppt

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

ARRAYS IN C

BY,
C.SARAVANAN,
ASSISTANT PROFESSOR/CSE
MANAKULA VINAYAGAR INSTITUTE OF
TECHNOLOGY
ARRAYS
⦿ Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.
⦿ To create an array, define the data type
(like int) and specify the name of the array
followed by square brackets [].

int myNumbers[] = {25, 50, 75, 100};


ACCESS THE ELEMENTS OF AN ARRAY

⦿ To access an array #include <stdio.h>


int main() {
element, refer to
int myNumbers[] = {25, 50,
its index number. 75, 100};
⦿ Array indexes start printf("%d",
myNumbers[0]);
with 0: [0] is the
first element. [1]
return 0;
is the second }
element, etc.
CHANGE AN ARRAY ELEMENT

#include <stdio.h>

int main() {
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

return 0;
}
LOOP THROUGH AN ARRAY

#include <stdio.h>

int main() {
int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

return 0;
}
MULTIDIMENSIONAL ARRAYS

⦿ If you want to store data as a tabular form,


like a table with rows and columns, you need
multidimensional arrays.
REPRESENTATION
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
ACCESS THE ELEMENTS OF A 2D ARRAY

#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
printf("%d", matrix[0][2]);
return 0;
}

Output:
2
LOOP THROUGH A 2D ARRAY

#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}

return 0;
}

You might also like