Vous êtes sur la page 1sur 10

Collage of Engineering (COE) EEEB114: Programming For Engineers

LAB 9: Array
Student Name: Student ID: Section:

9.1) LEARNING OBJECTIVES


By the end of this lab session, you should be able to:
Understand pointer & array, one dimensional / two dimensional array declaration and
initialization.
Implement the concept of array and string data in programs. (CO8).

9.2) PRE LAB ASSIGNMENT


Read 9.3.

9.3) BACKGROUND
An array is a group of consecutive memory locations related by the fact that they all have the
same name and the same type. To refer to a particular location or element in the array, we
specify the name of the array (index or subscript) and the position number of the particular
element in the array. Array declaration is made by specifying the data type, its name and the
number of space (size) so that the computer may reserve the appropriate amount of memory;
general syntax as follows data_type array_name[size];. There are 2 ways to initialize an array:
during compilation and during execution.

When we pass an array to a function, we are actually passing the pointer to the first element in
the array to the function. Therefore, any changes to the array inside the function will also
change the actual array inside the calling function.

It is possible to create an array which has more than one dimension. For e.g., 2D array: int
array[4][2]; or 3D array: int array[4][2][10];. Graphical representation of a 2D array as follows:
Page 1 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

Note: variable initialization shown above can also be done this way:
int myarray[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};

Page 2 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

9.4) IN LAB ACTIVITIES


Activity A: Accessing Array Elements

1. Type and execute the program shown in Figure 1.

Figure 1

2. Print screen the output of the program.

3. Recalling Worksheet 8, determine the following:

x[0] x[1] x[2] x[3] x[4] x[5]


Value
Address

Page 3 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

4. Describe the function of the program shown in Figure 1.


___________________________________________________________________________
__________________________________________________________________________

5. Insert break; in line 14. Execute the program and justify the value of sum.
___________________________________________________________________________
___________________________________________________________________________

6. Insert continue; in line 12. Execute the program and justify the value of sum.
___________________________________________________________________________
___________________________________________________________________________

Activity B: Searching for a value

1. Copy and paste program shown in Figure 2.


#include <stdio.h>
#define a 6

main ()
{
int i;
double small, x[a] = {6.1, 3.2, 89.3, 21.4, 20.5, 20.6};

small = x[0];

for (i = 1; i < a; i++)

if (small > x[i])


small = x[i];

printf("\nThe smallest element in x-array is: %.2f.\n", small);


}

Figure 2

Page 4 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

2. Execute the program and print screen the output obtained.

3. Describe the function of the program shown in Figure 2.


___________________________________________________________________________
__________________________________________________________________________

4. Modify code in Figure 2 to search for the biggest value. Indicate in which line the change
was made.
___________________________________________________________________________
__________________________________________________________________________

5. Execute the program and print screen the output obtained.

Page 5 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

Activity C: Exchanging Values of Elements - Swap

1. Copy and paste program shown in Figure 3.


#include <stdio.h>
#define a 2

main ()
{
int i;
double small, x[a] = {6.1, 3.2};

x[2] = x[0];
x[0] = x[1];
x[1] = x[2];

printf("\nThe elements in x-array is: %.2f,%.2f.\n", x[0], x[1]);


}

Figure 3

2. Execute the program and print screen the output obtained.

3. Describe the function of the program shown in Figure 3.


______________________________________________________________________________
___________________________________________________________________________

Page 6 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

Activity D: Exchanging Values of Elements - Sorting

1. Copy and paste program shown in Figure 4.

#include <stdio.h>
#define size 1000

main ()
{
int i, j, number, temp, list[size];
printf("Enter a list size:");
scanf("%d", &number);
for (i = 0; i < number; i++)
{
printf("Enter a number:\n");
scanf("%d", &list[i]);
}
for (i = 0; i < number-1; i++)
{
for (j = i + 1; j < number; j++)
{
if (list[i]>list[j])
{
temp = list[i];
list[i] = list [j];
list[j] = temp;
}
}
}
printf("The sort list is:\n");
for (i = 0; i < number; i++)
printf("%d\n", list[i]);
}

Figure 4
2. Print screen the output.

Page 7 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

Activity E: Two-dimensional Arrays Square Root and Square Operations

1. Copy and paste program shown in Figure 5.

#include <stdio.h>
#include <math.h>

main ()
{
int i, j;
int a[3][2]={{1,2},{3,4},{5,6}};

double sum = 0, answer_sqrt = 0;

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


{
for (j = 0; j < 2; j++)
if (a[i][j]>0)
{
sum = sum + pow(a[i][j],2);
}
}
answer_sqrt = sqrt(sum);
printf("Square root of the sum of square is: %.3f\n", answer_sqrt);
}

Figure 5

2. Print screen the output.

3. Describe the function of the program shown in Figure 5.


___________________________________________________________________________
___________________________________________________________________________

Page 8 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

Activity F: Two-dimensional Arrays Pointers

1. Copy and paste program shown in Figure 6. Execute the program and print screen the
output.

#include <stdio.h>
#define m 4
#define n 5

main ()
{
int
x[m][n]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}}
;

double output;

double get_output (int*,int,int);

output = get_output (*x,m,n);


printf("\nThe output is: %.2f\n", output);
}
double get_output (int *ptr, int x, int y)
{
int i, total = 0;
double output;
for (i = 0; i < x*y; i++)
{
total += *(ptr+i);
}
output = (double)total / ((double)x*y);
return output;
}

Figure 6

Page 9 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers

2. Describe the function of the program shown in Figure 6.


___________________________________________________________________________
___________________________________________________________________________

Activity G: Two-dimensional Arrays Matrix Multiplication

1. Write a program which performs matrix multiplication as indicated in Figure 7.

7 8
1 2 3
A=( ) B =( 9 0)
4 5 6
1 2
C=A*B

Figure 7

9.5) STATE YOUR LEARNING CURVE


Note: conclude what youve learned from this lab activity.

Page 10 of 10
Prepared By Sarveswaren May 2015

Vous aimerez peut-être aussi