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

Lecture07_Arrays

The document provides an overview of arrays in programming, explaining their definition, declaration, initialization, and manipulation. It includes examples of reading and storing numbers in arrays, as well as the concept of passing arrays as function parameters. Additionally, it covers multidimensional arrays and their usage in representing data structures like matrices.

Uploaded by

EG JP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views18 pages

Lecture07_Arrays

The document provides an overview of arrays in programming, explaining their definition, declaration, initialization, and manipulation. It includes examples of reading and storing numbers in arrays, as well as the concept of passing arrays as function parameters. Additionally, it covers multidimensional arrays and their usage in representing data structures like matrices.

Uploaded by

EG JP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Arrays

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

2
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);
3
Array
‣ is a collection of data items of the same element type

‣ to solve many programming problems, it is more


efficient 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

4
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.

5
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

6
Array
Declaration and Initialization

Declare and initialize an array of floats:

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;

7
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’};

8
Array
Sample Manipulation of the elements of array x

9
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];
}

10
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]);
}
11
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;
}

12
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

13
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;

14
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;

}
15
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.

16
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 first dimension is the only size that can be omitted

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

17
Multidimensional Arrays
Example

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


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

18

You might also like