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

Lecture 8

This document is a lecture on arrays in computer programming, covering their definition, declaration, usage, and passing to functions. It includes examples of single and multiple-subscripted arrays, as well as code snippets demonstrating how to read from and manipulate arrays. Key concepts such as initialization, bounds checking, and passing arrays by reference are also discussed.

Uploaded by

madin20232022
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 views

Lecture 8

This document is a lecture on arrays in computer programming, covering their definition, declaration, usage, and passing to functions. It includes examples of single and multiple-subscripted arrays, as well as code snippets demonstrating how to read from and manipulate arrays. Key concepts such as initialization, bounds checking, and passing arrays by reference are also discussed.

Uploaded by

madin20232022
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/ 17

University of Hafr Al-Batin

Hafr Al-Batin Community College ( HBCC )


Computer Science & Engineering Technology Unit (CSET)

CST121: Introduction to Computer


Programming

Arrays
Lecture-8
Outline

• Introduction

• Arrays

• Declaring Arrays

• Using Arrays

• Passing Arrays to Functions

• Multiple-Subscripted Arrays

2
Introduction

• Arrays
– An arrays is a structure of related data items
which are placed next to each other in the
memory.

– Arrays are static entity – same size throughout


program. We need to tell the size at the time of
their declaration.

– Syntax: dataType arrayName [ arraySize ];

3
Name of array
Arrays (Note that all
elements of this
• Array array have the
same name, c)
– Group of consecutive memory locations
– Same name and type c[0] -45
c[1] 6
c[2] 0

• To refer to an element, specify c[3]


c[4]
72
1543
– Array name c[5] -89
c[6] 0
– Position number c[7] 62
c[8] -3
c[9] 1
• Format: c[10] 6453

arrayName[ position number ] c[11] 78

– First element at position 0


– n element array named c: Position number
of the element
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
within array c 4
Arrays
• Array elements are like normal variables
c[ 0 ] = 3;
scanf( "%d", &c[ 0 ] );
printf( "%d", c[ 0 ] );

– Perform operations in subscript. If x = 3 then


c[ 5 - 2 ] == c[ 3 ] == c[ x ]

5
Declaring Arrays
• When declaring arrays, specify
– Name
– Type of array
– Number of elements
arrayType arrayName[ numberOfElements ];
– Examples:
int c[ 10 ];
float myArray[ 3284 ];

• Declaring multiple arrays of same type


– Format similar to regular variables
– Example:
int b[ 100 ], x[ 27 ];
6
Using Arrays
• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 } ;
» All elements initialized to 0
– If too many initializers, a syntax error is produced
– C arrays have no bounds checking

• If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array

7
Using Arrays
• Character arrays
– String “first” is really a static array of characters

– Character arrays can be initialized using string literals


char string1[] = "first";
• Null character '\0' terminates strings
• string1 actually has 6 elements and is equivalent to :
char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

– Can access individual characters


string1[ 3 ] is character ‘s’

– Array name is address of array, so & not needed for strings


scanf( "%s", string1 );
• Reads characters until whitespace encountered
• Can write beyond the end of array… So be careful !
8
Example 1
/* Input 10 numbers in an ARRAY and display their SUM */
#include <stdio.h>
#define SIZE 10
main()
{
int i, a[SIZE];
int sum = 0;
printf("Enter %d numbers : \n", SIZE);

// loop for input 10 numbers and save in an array

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


scanf("%d", &a[i]);

// loop for adding 10 numbers stored in an array

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


sum = sum + a[i];

printf("sum: %d\n", sum);


} 9
Example 2
/* Reads five grades and prints them */
#include <stdio.h>
#define SIZE 5

int main(void) {
double grades[SIZE] ; // array declaration
int i ;

printf("Enter five grades to store in array : \n");


printf("*********************************\n\n");

for (i = 0; i < SIZE; ++i) // loop to read the five grades into the array
{
printf ("Enter the %d element of array : ", i ) ;
scanf ( "%lf", &grades[i] ) ;
}

printf("\n");
for (i = 0; i < SIZE; ++i) // loop to display five grades stored in the array
printf ("The %d th element of array is %f\n", i, grades[i]) ;

system("pause");
return 0;
} 10
Example 3
/* Reads data into two arrays and subtracts their corresponding elements, storing the result in
another array. */
#include<stdio.h>
#define SIZE 5
int main(void) {
int first[SIZE], second[SIZE], diff[SIZE], i;

printf("Enter %d data items for first array : ", SIZE);


for(i=0;i<SIZE; i++) // input first array
scanf("%d",&first[i]);

printf("Enter %d data items for second array : ", SIZE);


for(i=0;i<SIZE; i++) // input second array
scanf("%d",&second[i]);

for(i=0;i<SIZE; i++) // compute the differences


diff[i]= second[i] - first[i];

printf("\n\nOutput of the arrays : \n");


for(i=0;i<SIZE; i++) // output the arrays
printf("%5d %5d %5d\n", first[i], second[i], diff[i]);
system("pause");
return 0;
}
11
Passing Arrays to Functions
• Passing arrays
– To pass an array argument to a function, specify the name of
the array without any brackets; array size is also usually
passed to function
int myArray[ 24 ];
myFunction( myArray, 24 );
– Arrays passed are call-by-reference
– Name of array is the address of first element
– Function knows where the array is stored. Therefore, it
modifies original memory locations

• Passing array elements


– Individual elements passed are call-by-value
– Pass subscripted name (i.e., myArray[ 3 ]) to function
12
Using arrays as function arguments
• In addition to passing individual elements of an array to
functions, we can also write functions that take an entire
array as a single argument.

• Such functions can manipulate some or all of the array


elements.

• However, unlike scalar variables where we have the


option of either passing value or reference (address) of
variables to a function, C allows only passing by reference
for arrays.

• In this section, we learn how to write functions that take


array as argument and how to call such functions.
13
Array as formal parameter to functions
• To specify an array as a formal parameter to a function, we put it
as if we are declaring the array, but without specifying the size.
void print_array (int a[], …){ // . . .}

• Not specifying the size will allow the function to be called with
any size of array.

• However, the function needs to know the size of the actual array
in order to process it correctly. The solution is to add an extra
parameter indicating the size or actual number of elements in the
array.
void print_array(double a[], int size){//. . . }

14
Array as actual argument in function call
• To pass an array as actual argument to functions, we just give the array name
without the brackets.

print_array (a, …);

• Since functions that take array usually also need to know the size of the array
or at least the number of elements in the array, the complete call to the
print_array function might be:
print_array(a, SIZE);

• Note that passing array as argument to functions is pass by reference not pass
by value. Thus, no copy of the array is made in the memory area of the
function. Instead, the function receives the address of the array and
manipulates it indirectly.

• How does the function receive the address when we did not use & operator
and the function did not declare a pointer variable?

• The truth is, array variables are in fact pointer variables, but which are 15
declared differently.
Multiple-Subscripted Arrays
• Multiple subscripted arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column

– Example:
int a[3][4];

Column 0 Column 1 Column 2 Column 3


Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]

Column subscript
Array name
Row subscript
16
Multiple-Subscripted Arrays
• Initialization
1 2
– int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
3 4
– Initializers are grouped by row in braces

– If not enough, unspecified elements are set to zero 1 0


int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 3 4

• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );

17

You might also like