2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 5 Arrays ALQUDS University Department of Computer Engineering Objective: After completing this session, the students should be able to: 1. Understand the syntax of array declaration, array assignments and array initialization. 2. Write programs to model using large sets of data using arrays. 3. Manipulate the array data structure. Background: An array is a series of homogeneous pieces of data that are all identical in type, but the type can also be quite complex. Array is a data structure that groups together entries of similar data type. Arrays can be used to represent polynomials, matrices, and other things. Syntax of array in ANSI C: The syntax of array in ANSI C is as follows: type array_name [size]; type array_name [size] = {list of array values}; Here the type is a variable type defined in C such as int, double, char, etc. The type can also be a user-defined type or a class defined in C++ (or Java). The following figure shows an integer array called c. This array contains 12 elements. Any one of these elements may be referred to by giving the name of the array followed Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 1
by the position number of the particular element in square brackets ([ ]). The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6], and, in general, the ith element of array c is referred to as c[ i - 1 ]. Array names, like other variable names, can contain only letter, digit and underscore characters. Array names cannot begin with a digit character. The position number contained within square brackets is more formally called a subscript or index. A subscript must be an integer or an integer expression. If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, if a = 5 and b = 6, then the statement c[ a + b ] += 2 is valid. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 2
Example: int a[5]; // array declared without initialization int a[5] = {1, 2, 3}; // array declared with 5 entries but only 3 initialized (the rest two will be 0). char name[5]; /* define a string of characters */ int a[5] = {1, 2, 3, 4, 5, 6}; // this definition is invalid. It may compile but will crash at run time since the number of entries initialized exceeds the array size. Experiment 5.1: Step1: Which of the following array declaration and / or initialization is valid (no compile or run time error)? Try them on your machine, state the reasons why/why not they are valid. a) int a[80]; b) int a[10] = {0}; c) int a [10] = {50}; d) int a[3] = {0, 1, 2, 3}; e) int a[ ] = {0, 1,2}; Step2: Write code to initialize an integer array of 1000 elements to {1, 2, 3, 4, 5,, 1000}. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 3
Multidimensional arrays Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays is to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two subscripts: The first (by convention) identifies the element s row and the second (by convention) identifies the element s column. Tables or arrays that require two subscripts to identify a particular element are called double subscripted arrays or 2-dementional arrays. Note that multiple-subscripted arrays can have more than two subscripts. The ANSI standard states that an ANSI C system must support at least 12 array subscripts. The following illustrates a double-subscripted array, a. The array contains three rows and four columns, so it is said to be a 3-by-4 array. In general, an array with m rows and n columns is called an m-by-n array. Arrays can be any dimension. Here is an example of 2 dimensional array of order 2 x 2 declared as: int Matrix[4][3]; Initialized as Matrix[0][0]= 4; Matrix[0][1]= 2; Matrix[1][0]= 3; Matrix[1][1]= 9; Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 4
Experiment 5.2: Step 1: Create an array for n numbers where the value for n is given by the user. Use a 1-D array, then read the values of elements using looping construct, then find the sum as well as average of these numbers. What do you notice? Step 2: Using 2-D arrays and loops to declare and set a 3x3 matrix, calculate the sum and average of these elements. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 5
Experiment 5.3: Step 1: Write a C program to read a set of numbers into an array then find the maximum or largest element present in an array. The program should also print the location or index at which maximum element occurs in array. Step 2: Write a C program to read a set of numbers (10 numbers) into an array, and then calculate the corresponding value for each of these numbers according to the function: f(x) = x 2-25 if x<0, and f(x) = 10*sin(x) otherwise After that, the program should print out a table of each number and its corresponding value, along with a histogram of asterisks (*) in each line, this histogram represents the value of the function for each number. Hint: Use a 2D array to store values, and then decide the suitable range for the histogram, and then use a looping construct to print out these asterisks. Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 6
Post lab5 Write a C program that reads 2-dimentional arrays and then performs the following functions: i) Addition of Two Matrices ii) Multiplication of Two Matrices Algorithm/Analysis/Reasoning C program --------- -------------------------------------------- Output/Screenshots: Dr. Labib Arafeh, Mr. Yacoub Sabatin, Eng. Allam Abu-Mwais, Eng. Nouraldeen Jibreen 7