0% found this document useful (0 votes)
4 views13 pages

Understanding Arrays in Programming

Cs lesson

Uploaded by

y4kzv998rm
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)
4 views13 pages

Understanding Arrays in Programming

Cs lesson

Uploaded by

y4kzv998rm
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

Module 4 Arrays (Storing data values that has same data type with one name)

Array definition ................................................................................................................................................................................................................................ 2


Array types and their declaration statements syntaxes .................................................................................................................................................................... 2
[Link] (One) dimentional Array ................................................................................................................................................................................................ 2
[Link] dimentional Array ..................................................................................................................................................................................................... 2
Array components ........................................................................................................................................................................................................................... 3
[Link] data type. ......................................................................................................................................................................................................................... 3
[Link] name. ............................................................................................................................................................................................................................... 3
[Link] Size: ................................................................................................................................................................................................................................ 3
[Link] Elements: ........................................................................................................................................................................................................................ 3
[Link] Index(s) or subscripts or locations: ................................................................................................................................................................................. 3
Input data to arrays (accessing array elements) ............................................................................................................................................................................... 4
Common arrays elements operations ............................................................................................................................................................................................... 5
Input/read data from user and store in array elements using for loop ......................................................................................................................................... 5
Output/print/display array elements data using for loop .............................................................................................................................................................. 5
Find sum and average of array elements data using for loop....................................................................................................................................................... 5
Find the minimum (smallest value) of array elements using for loop ......................................................................................................................................... 5
Find the maximum (largest value) of array elements using for loop ........................................................................................................................................... 5
Review Questions ............................................................................................................................................................................................................................ 7
Write an appropriate array definition ........................................................................................................................................................................................... 7
Write an appropriate array declaration......................................................................................................................................................................................... 7
What is the value of the expressions on the arrays given below: ................................................................................................................................................ 8
Perform the following operations on the arrays given below: ..................................................................................................................................................... 8
What is the output for the following (What is printed): ............................................................................................................................................................... 9
Write a c program. ..................................................................................................................................................................................................................... 11

-1-
Array definition
An array is a data structure which contains multiple data items of same type.

Array types and their declaration statements syntaxes


There are two types of array types :
[Link] (One) dimentional Array

[Link] dimentional Array

(1) (2)
Single (One) dimentional Array Mutlitple dimentional Array
Syntax Syntax
data-type array-name [size1] [size2] [size3]. . . ;
data-type array-name [size] ; Or

Or data-type array-name [size1][size2] = {{Value1,…, ValueSize1} ,.. {Value1, …, ValueSize1}};

data-type array-name [size] = {Value1, Value2, …, ValueSize} ;


Example Example
int b[5]; int a[2][3];
b[0] = 10; a[0][0] = 10 ;
b[1] = 20; a[0][1] = 20 ;
b[2] = 30; a[0][2] = 30 ;
b[3] = 40; a[1][0] = 40 ;
b[4] = 50; a[1][1] = 50 ;
Or a[1][2] = 60 ;
int b[5] = {10,20,30,40,50};
Or
int a[2][3] = { {10,20,30},{40,50,60} };

Memory Representation Memory Representation


Locations: a
b[0] b[1] b[2] b[3] b[4]
[0] [1] [2]
10 20 30 40 50
[0] a[0][0] = 10 a[0][1] = 20 a[0][2] = 30

[1] a[1][0] = 40 a[1][1] = 50 a[1][2] = 60

-2-
Array components
Array components are :
[Link] data type.
Arrays data types are : int , char, float, double. Refer to Module 2 for more data types.
[Link] name.
The array name is an identifier, please refer to module 2 for more information on identifier rules.
[Link] Size:
Number of elements.
[Link] Elements:
A memory location that store data, specified by the array name followed by one or more indexes/subscripts, with each index/subscript enclosed in square
brackets. Example: Arr[5][3] store the value of 100.
[Link] Index(s) or subscripts or locations:
A non-negative integer determine the dimension of the array.
The last index of the array is always one less than the size of the array declared.
Example: x[i] refers to an element in the one dimensional array x. Similarly y[i][j] refers to an element in two dimensional array.
Example The statement int a[5]; define a one dimensional array called/named a.
int a[5];
The statement char b[2][3]; define a two dimensional array called/named b.
a[0] = 10; Array a declaration: int a[5];
a[1] = 20; Array a defined as : int a[5];
Array b declaration: char b[2][3];
char b[2][3];
b[0][0] = ‘a’ ;
Array b defined as : char b[2][3];
b[0][1] = ‘v’ ; Size of array a = 5 elements
Size of array b = 2 X 3 = 6 elements
Array a data type: integer
Array b data type: character
a[0] is an element from array a, its value is 10;
b[0][1] is an element from array b, its value is ‘v’;
0 is the index or subscript of the element a[0]
0 and 1 are the indexes or subscripts of the element b[0][1]

-3-
Input data to arrays (accessing array elements)

(1) (2) (3)


Initialize the array elements Assign the array elements Input data to array elements

int array1[3] = { 10, 30, 15 } ; int array1[3]; int array1[3];


array1[0] = 10; scanf(“%d” , & array[0] );
array1[1] = 30; scanf(“%d” , & array[1] );
array1[2] = 15; scanf(“%d” , & array[2] );

An example user input would look like the


following:

10 30 15 
All the above example will create an array in memory represented as follows:

Memory representation :

Array1[0] Array1[1] Array1[2] Array1[3] Array1[4]


Array1 10 20 30 40 50

-4-
Common arrays elements operations
Input/read data from user and store in array elements using for loop
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i)
scanf("%d", &values[i]);

Output/print/display array elements data using for loop


int values[5];
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i)
printf("%d\n", values[i]);

Find sum and average of array elements data using for loop
float sum = 0.0, average;
for(i=0; i < 5; ++i)
sum += values[i];

average = sum / 5;
printf("Sum = %.2f", sum);
printf("Average = %.2f", average);

Find the minimum (smallest value) of array elements using for loop
int min = 99999;
for(i=0; i < 5; ++i)
if ( values[i] < min) min = values[i] ;

printf("Min = %d", min);

Find the maximum (largest value) of array elements using for loop
int max = -99999;
for(i=0; i < 5; ++i)
if ( values[i] > max) max = values[i] ;

printf("Max = %d", max);

-5-
Example
a and b are two one-dimentional arrays represented below, perform the operations below:

a 12 10 16 15 13
and
a[0] a[1] a[2] a[3] a[4]

b -5 3 0 4 30
are two
b[0] b[1] b[2] b[3] b[4]

Operations / Expressions Solution values :

a[1] + b[1] = 10 + 3 = 13

a[0] – b[0] = 12 – (-5) = 17

a[1] * b[1] = 10 * 3 = 30

a[0] / b[3] = 12/4 = 3

a[1] < b[4] = 10< 30 = 1 (True)

a[0] > b[4] = 12 > 30 = 0 (False)

-6-
Review Questions

Write an appropriate array definition


Write an appropriate array declaration
Q1 : Declare a two-dimensional, 5x6- element integer array called A.
Sol: int A[5][6] ;
Q2 : What is the size of the array A? What is the range of array A?
Sol: 5 X 6 = 30 elements Range is 0 to 4 and 0 to 5
Q3 : Define a one-dimensional array called Letters with charater data type and size =10.
Sol: char Letters[10];
Q4 : What is the size of the array Letters? What is the range of array Letters?
Sol: 10 elements Range is 0 to 9
Q5 : Write an appropriate array definition for a 20X48 real numbers named Matrix.
Sol: float Matrix [20][48] ;
Q6 : What is the size of the array Matrix? What is the range of array Matrix ?
Sol: 20 X 48 = 816 elements Range is 0 to 19 and 0 to 47
Q7 : Write an appropriate array declaration for a 800 floating point numbers named Students
Sol: float Students[800];
Q8 : What is the size of the array Students? What is the range of array Students?
Sol: size = 800 elements Range is 0 to 799
Q9 : Write an appropriate array declaration called MyArray that has the following initial values: 19.1, 90.01, 15.5
Sol: float MyArray[3] = { 19.1, 90.01, 15.5 } ;

-7-
What is the value of the expressions on the arrays given below:
Perform the following operations on the arrays given below:
Q1
x 10 20 30 40 10 Operations/Expressions Solution
x[ 1 ] + y[ 3 ] = 20 + 3 = 23
x[ 3 ] % x[ 2 ] = 40 % 30 = 10
y 1 2 3 3 2 x[ 2 ] - y[ 1 ] = 30 – 2 = 28
x[ 1 ] + y[ 3 ] = 20 + 3 = 23

Q2
x 10 20 30 40 10
x[1][2] + x[0][1] = 3 + 20 = 23
1 2 3 3 2
x[0][3] += x[1][4] ➔ x[0][3] = x[0][3]+ x[1][4] ➔ x[0][3] = 3 + 2 = 5

x[ x[1][0] ] [ x[1][2] ] - 12 ➔ x[ 1 ] [ 2 ] – 12 ➔ 3 – 12 = -8

Q3 What will be the output after performing the array operation : Q4 What will be the value after performing the array operation :
a 2 4 3 5
a[0] a[1] a[2] a[3] int sample[4] = { 2, 5 , 7 , 8 } ;

sample[ sample[3] / sample[0] ]


(a[0] - a[3] )>(a[1]%a[2]==1) Sol:
Sol:
(2 – 5 ) > ( 4 % 3 == 1) sample[ 8 / 2 ]
-3 > ( 1 == 1) sample[ 4 ]
-3 > ture Error, element does not exist in array
-3 > 1
false or 0

-8-
What is the output for the following (What is printed):
Q1
#include <stdio.h> Solution:
void main()
{ a b c
int a, b = 0; 0 0
int c[6] = {1, 2, 3, 4, 5, 0}; 1 1 c[0] c[1] c[2] c[3] c[4] c[5]
for (a = 0; a < 6; a++) 2 4 1 2 3 4 5 0
if ((a%2) == 0) b += c[a]; 3 9
printf ("%d", b); 4
} 5
6

Output :
9

Q2
#include <stdio.h> Solution:
void main()
{ a b c
int a, b = 1; 0 1
int c[6] = {1, 2, 3, 4, 5, 0}; 1 c[0] c[1] c[2] c[3] c[4] c[5]
for (a = 0; a < 6; a++) 2 1 2 3 4 5 0
if ((c[a] %2) ==1) c[a] += b; 3 2 4 6
printf ("%d", c[0]); 4
printf ("%d", c[2]); 5
printf ("%d", c[4]); 6
}
Output :
246

-9-
Q3 What is the output of the following program if the user input the following :

User input: 1 2 3 4
5 6 7 8
9 10 11 12

#include <stdio.h> Solution:


int z[3][4];
void main () c = 999 which is a large number, hence if the array element is holding a
{ smaller value it will be saved in c in every row.
int i, j, a, b, c;

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


for (j = 0; j <4; j++) Output :
scanf ("%d", &z[i] [j]); 1
5
for (a = 0; a < 3; a++)
{ 9
c = 999;
for (b = 0; b < 4; b++)
if (z[a][b] < c) c = z[a][b];
printf ("%d \n", c);
}
}

- 10 -
Write a c program.

Q1: Write a C program to read a 10 elements integer array and then display the elements.

Sample output
#include <stdio.h> Enter 5 integers: 12 9 10 4 15 
void main()
{ Displaying integers:
int values[5];
printf("Enter 5 integers: "); 12
for(int i = 0; i < 5; ++i)
9
scanf("%d", &values[i]);
10
printf("Displaying integers: "); 4
for(int i = 0; i < 5; ++i)
printf("\n%d", values[i]); 15

Q2: Write a c program that reads 5 integer numbers in a single dimensional array then display a particular element.

Sample output
#include <stdio.h> Enter 5 integer numbers:
void main ()
{ 11 12 13 42 54 
int digit [5], i;
printf ("\nEnter 5 integer numbers:\n"); Enter a subscript to print the element:4
for (i = 0; i <=4; ++i)
digit [4] = 54
scanf ("%d", & digit[i]);

printf ("\nEnter a subscript to print the element:");


scanf ("%d", &i);
printf ("\ndigit [%d] = %d", i, digit [i]);
}

- 11 -
Q3: Write a c program that reads 5 students marks in an array then computes and displays average of the marks.

Sample output
#include <stdio.h> Enter marks of 5 students: 70 80 77 99 92
void main()
{ The average of the marks is 83.60
int marks[5], i; float sum=0.0,avg;
printf( "Enter marks of 5 students: ");
for( i=0;i<=4;++i)
{
scanf( "%d", &marks[i] );
sum=sum + marks[i];
}
avg = sum/5;
printf( "\n The average of the marks is %.2f ", avg);
}

Q4: Write a c program that reads 6 integer values in array then find and displays the minimum and the maximum.

Sample output
#include <stdio.h> Enter 6 integers: 5 71 -1 5 4 45
void main()
{ Min = -1
int values[6];
int min = 99999; Max = 71
int max = -99999;

printf("Enter 6 integers: ");


for(int i = 0; i < 6; ++i)
{
scanf("%d", &values[i]);
if ( values[i] < min) min = values[i] ;
if ( values[i] > max) max = values[i] ;
}
printf("Min = %d\n", min);
printf("Max = %d\n", max);
}

- 12 -
Given the following declaration

Given the following declaration:


#include <stdio.h>
#include <string.h>
main()
{
int st[30];
int i;

[1] Write the appropriate for loop statement to process the Array.
[2] Write the appropriate statement to Input /Enter data into the Array using Keyboard.
[3] Write the appropriate statement to Display /Output the Array onto screen.

Solution:
[1] for(i=0;i<30;i++)
[2] &STD[i]);
[3] STD[i]);

- 13 -

You might also like