Programming Applications for Engineers

profileAisha_
Week3_Labs_Materials.pdf

Course Title Programming Applications for Engineers

Course Code CS 159 Lab

Assignment Title Arrays – One Dimensional

P a g e | 2

Table of Contents

Scenario

Objective

Overview

Single Dimensional Arrays

Function Communication with Arrays

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Page3

Page3

Page3

Page4

Page5

Page6

Page6

Page6

Page6

P a g e | 3

Arrays – One Dimensional

Scenario:

In this lab you will be able to understand the concept and uses of arrays in C program.

Objectives:

• To understand the basic concepts of arrays (Single Dimensional) • To be able to define and initialize arrays • To be able to pass arrays and elements to functions • To write programs that search arrays

Overview:

This lab will make you capable of dealing with arrays in C program by creating single dimensional

array, inserting and accessing elements from array.

P a g e | 4

Concept of Arrays

Array is used to store sequential collection of data of the same data type.

Example: storing a string that contains series of characters.

A specific element in an array is accessed by an index.

Single Dimensional Arrays

All arrays consist of contiguous memory locations. The lowest address corresponds to the first

element and the highest address to the last element.

Array Declaration

To declare an array in C, you need to specify the type of the elements and the number of

elements required by an array as follows:

type arrayName [ arraySize ];

Example: int mark [5]; //array name is mark, type is int and the size is 5

Array Initialization

You can initialize array in C either one by one element, or using a single statement.

int a [5] = {34,67,54,33,66}; // array is declared and initialized at same time

Accessing/Inserting array elements:

C uses an index to access elements from array. You can use any numeric constant to access

element of array by using index.

Index expression should be as below,

int x = a [4]; // variable x has the value of index 4 of array a

To process all elements in an array, you have to use for loop as below,

//Reading or inserting elements

for (i=0;i<10;i++) //if the size of array is 10

{

scanf(“%d”,&a[i]); //inserting elements into array a

P a g e | 5

}

//Accessing for printing

for (i=0;i<10;i++) //if the size of array is 10

{

printf(“%d”,a[i]); //accessing all array elements

}

Function communication with arrays:

//Passing whole array to a function

void fun(int fmark[ ]); //function declaration

int mark[10]; // array declaration

fun(mark); // function calling, and passing the whole array mark

void fun (int fmark [ ]) //function definition

{

}

//Passing data from array

void fun(int fmark[ ]); //function declaration

int mark[10]; // array declaration

fun(mark[3]); //function calling, and passing a single value (4th element) in the array mark

void fun (int fmark ) //function definition

{

}

P a g e | 6

Exercise 1: Write a C program that prompts the user to enter 10 numbers into a single dimensional array, and find the largest number in the array.

Exercise 2: Write a function Equal in C to test if every element in array A is equal/not equal to its corresponding element in array B. Size of both arrays is 5. Write a main function that

prompts the user to enter 5 numbers into each of two arrays A and B. Then, it calls the function

Equal to test and print if a[0] = b[0], a[1] = b[1] and so forth.

Exercise 3: Write a function FindSum in C to calculate and return the sum of all even elements in an integer array of size 10. Write a main function that prompts the user to enter

10 numbers into the array. Then, it calls the function Findsum and prints the returned value.

Exercise 4: Write a function ChangeArray in C that takes a 1D integer array A of size 10, as an argument. The function should search for all negative array elements, and replace them

with zero. The function should print the updated array. Write a main function that prompts

the user to enter 10 numbers into an array A. Then, it calls the function ChangeArray and pass

the array A as argument.

//Sample Output

Enter 5 numbers into array A: 2 3 5 7 9

Enter 5 numbers into array B: 2 3 8 7 4

A[0] is equal to B[0]

A[1] is equal to B[1]

A[2] is not equal to B[2]

A[3] is equal to B[3]

A[4] is not equal to B[4]

//Sample Output

Enter 10 numbers into array:

2 -3 -6 7 -8 1 6 -4 8 5

The updated array has the following elements:

2 0 0 7 0 1 6 0 8 5

Course Title Programming Applications for Engineers

Course Code CS 159 Lab

Assignment Title Arrays – Two Dimensional

P a g e | 2

Table of Contents

Scenario

Objective

Overview

Two Dimensional Arrays

Function Communication with Arrays

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Page3

Page3

Page3

Page4

Page5

Page6

Page6

Page6

Page7

P a g e | 3

Arrays – Two Dimensional

Scenario:

In this lab you will be able to understand the concept and uses of arrays in C program.

Objectives:

• To understand the basic concepts of arrays (two dimensional) • To be able to define arrays • To be able to pass arrays and elements to functions • To write programs that search arrays

Overview:

This lab will make you capable of dealing with arrays in C program by creating single and two

dimensional array, inserting and accessing elements from array.

P a g e | 4

Concept of arrays:

Array is used to store sequential collection of data of the same data type.

Example: storing a string that contains series of characters.

Two dimensional arrays:

Data are stored in more than one dimension. For example, table with column value and row

value. In essence, it is a list of one-dimensional arrays.

Declaration & initialization of 2D array

A two-dimensional array can be viewed as a table which has x number of rows and y number of

columns. A 2-dimensional array a, which contains three rows and four columns can be shown as

below:

Example: int a [3] [4]; // a [rows] [columns]

Accessing / inserting elements into 2D array:

You have to use two for loops to work with both row value and column value as shown in below

example;

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

{

for(j=0;j<4;j++)

{

scanf(“%d”,&a[ I ] [ j ]); // inserting numbers into the array

printf(“%d”, a[ i ] [ j ]); // printing array elements

}

}

Initialization of a 2 dimensional array:

Two dimensional arrays may be initialized by specifying bracketed values for each row. The

following code declares and initialize an array with 3 rows and 4 columns.

int a[3][4] = {

{0, 1, 2, 3} , /* initializers for row indexed by 0 */

{4, 5, 6, 7} , /* initializers for row indexed by 1 */

{8, 9, 10, 11} /* initializers for row indexed by 2 */

};

P a g e | 5

The nested braces, which indicate the intended row, are optional. The following initialization is

equivalent to previous example:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Function Communication with Two-Dimensional Array:

//Passing a Row

void circle(int [ ]); // function declaration

Row = 5;

Col = 5;

int data [row][col]; // 2D array declaration

circle (data [row#]); //function calling and passing 1 row as argument

void circle(int x[ ]) //function definition

{

}

//Passing the whole array

void circle(int x[ ] [5] ); /*function declaration , should specify the size of columns where

size of rows is not necessary in the case of fixed size array. */

int data [5][5]; //array declaration

circle (data); //function calling and passing the array as argument

void circle( int x[ ] [5] ) //function definition

{

}

P a g e | 6

Exercise 1: Write a function that copies a one-dimensional array of 15 numbers into a two- dimensional array of 3 rows and 5 columns. The function should print the two dimensional

array in table format as shown below.

Exercise 2: Write a function SumArray that takes an integer two-dimensional array of size 4x4 as an argument, and prints the sum of all the numbers in the array. Write a main function

that prompts the user to enter 16 numbers into a 4x4 integer array, and then call the function

SumArray and pass the array as argument.

Exercise 3: Write a function Smallest that takes a float two-dimensional array of size 3x4 as an argument, and returns the smallest positive number in the array. Write a main function

that prompts the user to enter 12 float numbers into a two dimensional array of size 3x4, and

then call the function Smallest and pass the array as argument. Print the smallest positive

number in the main function.

//sample output

Enter 16 numbers into the array:

7 8 9 6

4 5 6 2

1 8 7 6

5 6 8 4

The sum of all numbers in the array is: 92

//sample output

Enter 15 numbers into a single dimensional array: 5 8 9 6 5 8 7 4 8 9 6 6 8 1 2

Two dimensional array elements are:

5 8 9 6 5

8 7 4 8 9

6 6 8 1 2

//sample output

Enter 12 numbers into the array:

7.5 8.2 9.5 6.0

4.5 -5.1 -2.5 2.7

1.5 8.0 7.3 -6.0

The smallest positive number in the array is: 1.5

P a g e | 7

Exercise 4: Write a function check that takes two arguments; an integer two-dimensional array of size 3x3, and an integer X. The function should search for X in the array, and returns

whether X is found in the array. Write a main function that prompts the user to enter 9

numbers into a 2D array of size 3x3, and to enter an integer X. Then call the function check

and pass the array and X as arguments. In the main, print whether X is found in the array, or

not found.

//sample output

Enter 9 numbers into the array:

5 2 6

3 4 2

4 1 3

Enter a number to search for in the array: 4

4 is found in the array