0% found this document useful (0 votes)
4 views

Array 2004 Notes

This is a presentation about arrays in computer programming (C)

Uploaded by

iufanboy8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array 2004 Notes

This is a presentation about arrays in computer programming (C)

Uploaded by

iufanboy8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Array

char a, b, c, d, e; // there are 5 variables of type character that can be grouped into an array.
and that character array is also known as String

What is array?
array is a fixed-size, sequenced collection of
elements of the same data type.
Elements refer to list of variables of the same data type that can be grouped into
an array

Ex.
int a, b, c, d, e, f, g, h, i, j; // there are 10 variables of type integer that can be grouped into an array.
float a, b, c, d, e; // there are 5 variables of type float that can be grouped into an array.
array is a sequence of data items that
are of the same type, that are indexible,
and that are stored contiguously.
Arrays are data type that is used to
represent a large number of homogenous
values.
array is a sequenced collection, we can
refer to the elements in the array as the
first element, the second element, and so
forth until we get to the last element.
Arrays are data type that is used to
represent a large number of homogenous
values.
use loops to read and write the elements
in an array.

use loops to add, subtract, multiply, and


divide the elements.
use loops for more complex processing
such as calculating averages.
The General Form – One-dimensional array

• storage class data-type arrayname[expression];


• Example:
int ccmit[5];
char bscs[30];
float bsit[20];
double ITCS[12];
int a, b, c, d, e;

int X[5]; a b c d e

X[0] X[1] X[2] X[3] X[4]

Each data element is accessed using an index.


Array with Initialization

Consider the following array definition:


int x[4] = {1, 2, 3};
float y[5] = {1.0, 1.25, 1.5};
The results on an element by element basis are:
x[0] = 1 y[0] = 1.0
x[1] = 2 y[1] = 1.25
y[2] = 1.5
x[2] = 3 y[3] = 0
x[3] = 0 y[4] = 0
char college[6] = “CCMIT”;
size

college[0] = ‘C’;
college[1] = ‘C’;
college[2] = ‘M’;
college[3] = ‘I’;
college[4] = ‘T’;
college[5] = ‘\0’;

index or subscript
Referencing Array Elements
27
• int X[10] = { 3, 1,7,2,6,9,4,8,5,10}; declaration with initialization
9 will be displayed
• printf(”%d”, X[5]);
• scanf(”%d”, &X[9]); 27 will replace 10

• if( X[0] > X[3]) the evaluation is TRUE

• X[2+4] = X[8] * 5; 25 is assigned to 7th element


Accessing Elements in Arrays

for(i=0; i<10; i++)


scores[i]…..;
Inputting Values

int scores[10];
int i; …
for(i=0; i<10; i++)
scanf(“%d”, &scores[i]);
Inputting Values

for(i=0 i<10; i++)


scores[i] = i * 2;
Sample program 1:
This is a program that sorts the values of the array num.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num[3] = {5, 3, 7};
int h, i, temp;

for(h=0; h<3; ++h)


for(i=0; i<h; ++i)
{
if(num[i] > num[I + 1])
{
temp = num[i];
num[i] = num[I + 1];
num[i+ 1] = temp;
}
}
for(i=0; i<3; i++)
printf(“%d\n”, num[i]);
getch();
}

sample run:

Output:
3
5
7
Multidimensional
Arrays
• Multidimensional arrays are defined in much the same manner as
one-dimensional
• A two-dimensional array requires two pairs of square brackets.
• Two-dimensional arrays are also called array of arrays.
float IT[5][3];
int CS[3][3];
int ccmit[3][3] ={1,2,3,4,5,6,7,8,9};

ccmit[0][0] = 1 ccmit[0][1] = 2 ccmit[0][2] = 3


ccmit[1][0] = 4 ccmit[1][1] = 5 ccmit[1][2] = 6
ccmit[2][0] = 7 ccmit[2][1] = 8 ccmit[2][2] = 9
column
ccmit [3] [3]
0 1 2
row 0 1 2 3
1 4 5 6
2 7 8 9
a [3] [3]

0 1 2
0 a [0] [0] a [0] [1] a [0] [2]
1 a [1] [0] a [1] [1] a [1] [2]
2 a [2] [0] a [2] [1] a [2] [2]
example: declarations
int x[3][4]={{2,4,6,8},{10,12,1,3},{5,7,9,11}};
int x[3][4]={{2,4,6,},{10,12,1,3},{5,7}};
error
int x[3][4]={{2,4,6,8},{10,12,1,3,14},{5,7,9,11}};
int x[3][4]={{2,4,6,8},{10,12,1,3},{8,1,3,4},{5,7,9,11}};
int x[3][4]={2,4,6,8,10,12,1,3,5,7,9,11};
int a;
int b;
for (a=0;a<3;a++)
for(b=0;b<4;b++)
printf(“%d\t”,x[a][b]);
referencing:
int x[3][4]={{2,4,6,8},{10,12,1,3},{5,7,9,11}};
printf(“%d”, x[1][2]); 0 1 2 3

scanf(“%f”, &x[0]0]); 0
1
2
10
4
12
6
1
8
3
x[2][0] = x[3][2] * x[1][3]; 2 5 7 9 11

if(x[1][4]>=7)
while( 9 == x[a][b])
1 2 3 4
1 123 3 321
12 12 32 32
123 1 321 3

5 6
* ***
** **
*** *

You might also like