100% found this document useful (1 vote)
651 views

Chapter 4 Array

An array is a collection of elements of the same type stored in contiguous memory locations that can be individually accessed using an index. There are one-dimensional and two-dimensional arrays. One-dimensional arrays use a single subscript while two-dimensional arrays use two subscripts to access elements. Arrays must be declared with a type and size and can be initialized. Elements can be accessed and modified using indexing with loops for processing sequential data. Strings are arrays of characters terminated by a null character.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
651 views

Chapter 4 Array

An array is a collection of elements of the same type stored in contiguous memory locations that can be individually accessed using an index. There are one-dimensional and two-dimensional arrays. One-dimensional arrays use a single subscript while two-dimensional arrays use two subscripts to access elements. Arrays must be declared with a type and size and can be initialized. Elements can be accessed and modified using indexing with loops for processing sequential data. Strings are arrays of characters terminated by a null character.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

COMPUTER

PROGRAMMING
DAE 20103
CHAPTER 4
ARRAYS

INTRODUCTION
An array is a collection of data elements that are of
the same type (e.g., a collection of integers,
collection of characters, collection of doubles).

INTRODUCTION

1-dimensional array

2-dimensional array
Oct 14
Oct 15
Oct 16

ONE DIMENSIONAL ARRAY

Array Declaration
Must declare both the name of the array
and the number of cells associated with it
The declaration:
Data_type array_name [expression]
Expression : size of array
Array name : name of array that are an identifier

The items in an array start with index 0

Array Declaration

Syntax: <type> <arrayName>[<dimension>]

Always start with index 0

Array Initialization
Three types of array initialization:
i. Array of 10 un-initialized integers
int A[10];
ii. Initialize array
int months[12]={31,28,31,30,31,30,31,31,30,31,30,31}
iii. If the size of declared array is not given and not
initialized to any value, the size of array will be
automatically set to the length of the list.
Exp:
float a[] = {1.1,2.1,3.1,4.1,5.1};

Array Initialization
Example
int A[10];

--

A[3] = 1;
int x = A[3];

--

--

0
A

--

--


1

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

Array Initialization

Array Initialization
Arrays can be initialized, but they cannot
be assigned!
Exp:
char A[3] = {a,b,c,d};
char B[3] = {f,g,h,i};
A = B ERROR
char C[3] = A; ERROR
10

Using Loops for Sequential Access


Looping statement can
be used in array for
sequential process
Using the loop counter
as an array index,
gives access to each
array element in turn
Example:
int square[SIZE], I;
The for loop
int square[SIZE], i;
for (i=0;i<SIZE; ++i)
square [i] = i * i;
11

Example: Average Value


/*This program is to read 10 numbers and get an arrays*/
#include<stdio.h>
main()
{
int x[10],total=0,i;
for (i=0;i<10;i++){
printf("Insert number %d >>>",i+1);
scanf("%d",&x[i]);
total+=x[i];
}
printf("TOTAL=%d\n",total);
printf("AVERAGE=%d",total/10);
return (0);

Average

12

TWO DIMENSIONAL ARRAY

13

2-D Array Example


int table[5][4];
1st Dimension
2nd Dimension

Allocate
Row:0-4
Allocate
Column:0-3

14

Two Dimensional Array Initialization


The right index of an array is increased first
before the left index
Example:

X[0][0]

X[0][1]

int x[10][5]

X[0][2]

/*allocate 50 spaces (row:0-9, and


column:0-4*/

X[0][3]

X[1][0]

X[1][1]

X[1][2]

X[1][3]

X[2][0]

X[2][1]

10

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

11

X[2][3]

12

float a[3][20]
/*allocate 60 spaces (row:0-2, and
column:0-19*/

Example:

15

Coding Example
/*This program will read each element in array b*/
#include<stdio.h>
main()
{
float b[3]
[4]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10,11.1,12.12};
int i,j;

for (i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("[%d][%d]=%.1f\n",i,j,b[i][j]);
}
}
Output
return (0);
[0][0]=1.1
[0][1]=2.2
[0][2]=3.3 and so on.
16

STRING ARRAY
String is a grouping of characters
C implements the string data structure
using arrays of char type
Example:
Char college[11]=KUITTHO BP;

String Initialization
A string

K U

T H O

char college[11]={K,U,I,T,T,H,O,,B,P,\0};

- A string with a size of array


char college[11]=KUITTHO BP;

- A string without a size of array


char college[]=KUITTHO BP;
17

B P \0

Coding Example
#include<stdio.h>
void main()
{
int i=0;
char time[7]="Work";
while (time[i]!='\0')
{
printf(%c, time[i]);
i++;
}
getchar();
}

\0 means the end of


an array

Output
Work

18

EXERCISE

19

Exercises 1
1.

What is the difference in meaning between x3 and


x[3]? x3 variable declaration which can hold only one value
x[3] array declaration which can hold 4 values

2.

For the declaration


char grades[5];
a) How many6memory cells are allocated for data
storage?
char
b) What type of data can be stored there?
c) How does one refer to the first array element?
grades[0]
d) To the final array element?
grades[5]

20

Exercise 2
int A[10], i = 7, j = 2, k = 4;
A[0] = 1;
A[i] = 5;
A[j] = A[i] + 3;
A[j+1] = A[i] + A[0];
A[A[j]] = 12;

What is the content of array A?


1

12

21

Whats wrong?

Exercise 3

Q(1)
int A[2][3] = {{2,8,1}, {4,3,9}};
int B[4] = {3, 6, 4, 2, 10, 9};
Q(2)
int A[6];
for (int i=1; i<=5; i++)
{
printf("Enter number: );
scanf(%d,&A);
22

Exercise 4
What will the output after executing the following
code?
int data[5] = {2, 8, 3, 1, 7};
int temp;
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(data[j] > data[i]){
temp = data[j];
data[j] = data[i];
data[i] = temp;
printf(%d\n, data[i]);
}
}
}
23

Exercise 5
What is the content of the array A upon
completion of the for loop?
int A[9] = {5, 3, 21, 33, 21, 9, 10, 80, 12};
int count, temp;
for (count = 1; count <= 8; count++) {
if (A[count-1] > A[count]) {
temp = A[count-1];
A[count-1] = A[count];
A[count] = temp;
}
}
24

You might also like