0% found this document useful (0 votes)
20 views19 pages

Lecture07 Arrays

The document discusses arrays in C programming. It defines arrays as collections of data items of the same type stored in adjacent memory locations. It describes how to declare, initialize, access, and manipulate array elements using subscripts. It also discusses passing arrays to functions, multidimensional arrays, and examples of using arrays to store and process data. The overall purpose is to teach the fundamentals and applications of using arrays in C.

Uploaded by

Ramski Gamboa
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)
20 views19 pages

Lecture07 Arrays

The document discusses arrays in C programming. It defines arrays as collections of data items of the same type stored in adjacent memory locations. It describes how to declare, initialize, access, and manipulate array elements using subscripts. It also discusses passing arrays to functions, multidimensional arrays, and examples of using arrays to store and process data. The overall purpose is to teach the fundamentals and applications of using arrays in C.

Uploaded by

Ramski Gamboa
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

Arrays

LECTURE 07
CCC101 - Introduction to Programming 1
Jennifer Joyce M. Montemayor

Department of Computer Science


College of Computer Studies
MSU - Iligan Institute of Technology
Objectives
‣ to learn how to declare and use arrays for storing
collections of values of the same type

‣ to understand how to use a subscript to reference


the individual values in an array

‣ to learn how to process the elements of an array in


sequential order using loops

‣ to understand how to pass individual array elements


and entire arrays through function arguments

2 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Your Task
Write a program that reads 10 numbers from the keyboard and then
stores them

3 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Your Task
Write a program that reads 10 numbers from the keyboard and then
stores them

Solution 1

int num0, num1, num2, num3, num4, num5, num6, num7,


num8, num9;

printf(“Enter a number 1: ”);


scanf(“ %d”, &num0);

printf(“Enter a number 2: ”);


scanf(“ %d”, &num1);

. . .

printf(“Enter a number 10: ”);


scanf(“ %d”, &num9);
4 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07
Array
‣ is a collection of data items of the same element type

‣ to solve many programming problems, it is more


ef cient to group data items together in main memory
than to allocate an individual memory cell for each
variable

‣ a collection of two or more adjacent memory cells


called array elements that are associated with a
particular name

‣ is an ordered list of values

5 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


fi
Array
To setup an array in memory, we must declare both the
name of the array and the number of cells associated with it:

double x[8];

The declaration above will instruct the compiler to associate


8 adjacent memory cells with the variable name x.

Each element of array x may contain a single type double


value, so a total of 8 such numbers may be stored and
referenced using the array name x.

6 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array
To process the data stored in an array, we reference each
individual element by specifying the array name and
identifying the element desired using a subscripted
variable.

x[2] is element 3 of array x

An array of size N is indexed from zero to N-1

7 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array
Declaration and Initialization

Declare and initialize an array of oats:

float prices[3] = {1.2, 2.3, 3.4};

or you can also initialize it later:

float prices[3];

prices[0] = 1.2;
prices[1] = 2.3;
prices[2] = 3.4;

8 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


fl
Array
Declaration and Initialization

Declaring an array of characters of size 3:

char letter[3] = {‘a’, ‘b’, ‘c’};

or we can skip the size and leave it to the compiler to


estimate the size of the array:

char letter[] = {‘a’, ‘b’, ‘c’};

9 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array
Sample Manipulation of the elements of array x

10 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Your Task
Write a program that reads 10 numbers from the
keyboard and then stores them

Solution 2

int i, numbers[10];

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


printf(“Enter number %d”,i);
scanf(“%d”, &numbers[i];
}

11 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array
Example

#define N 10

int i, numbers[N];

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


printf(“Enter number %d”,i);
scanf(“%d”, &numbers[i]);
}

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


printf(“ %d”,numbers[i]);
}
12 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07
Array
Example

What will be the output of the following code?

#include<stdio.h>

int main(){

int a[5] = {5, 1, 15, 20, 25};

int i, j, m;

i = ++a[1];

j = a[1]++;

m = a[i++];

printf("%d, %d, %d", i, j, m);

return 0;
}

13 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array as Formal Parameters
‣ when an array with no subscript appears in the argument
list of a function call, what is actually stored in the
function’s corresponding formal parameter is the address
of the initial array element or the base address of the array

‣ when an array is passed as an argument to a function, the


function will be able to manipulate the original array and
not a copy of the array

‣ in C, it is illegal for a function to have an array as return


type

14 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array as Formal Parameters
Example

void fill_array(int list[], int n, int value){

int i;

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

list[i] = value;

15 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Array as Formal Parameters
Example (continuation..)

int main(){

int i, size=5,
y[size], value=1;

fill_array(y, size, value);

for(i=0;i<size;i++)
printf("%d ",y[i]);

return 0;

}
16 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07
Array as Formal Parameters

When an array without


a subscript is passed
as an argument to a
function call, the base
address of the array or
the address of the
initial element of the
array is passed.

17 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


Multidimensional Arrays
‣ an array with two or more dimensions

Two-Dimensional Array

‣ we will use to represent tables of data, matrices and other two-dimensional objects

Syntax

data_type array_name [size1][size2] … [sizen];

Example

double table[ROW_SIZE][COLUMN_SIZE];
void process_matrix(int in[][4], int out[][4], int row_count);

‣ the size of the rst dimension is the only size that can be omitted

‣ multidimensional arrays are initiialized by listing values that are grouped by row

18 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07


fi
Multidimensional Arrays
Example

char tictac[3][3] = {{‘x’,’o’,’x’},


{‘o’,’x’,’o’}, {‘o’,’x’,‘x’}};

19 Jennifer Joyce M. Montemayor / CCC101 / Lecture 07

You might also like