Académique Documents
Professionnel Documents
Culture Documents
LAB 9: Array
Student Name: Student ID: Section:
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
Figure 1
Page 3 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
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.
___________________________________________________________________________
___________________________________________________________________________
main ()
{
int i;
double small, x[a] = {6.1, 3.2, 89.3, 21.4, 20.5, 20.6};
small = x[0];
Figure 2
Page 4 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
4. Modify code in Figure 2 to search for the biggest value. Indicate in which line the change
was made.
___________________________________________________________________________
__________________________________________________________________________
Page 5 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
main ()
{
int i;
double small, x[a] = {6.1, 3.2};
x[2] = x[0];
x[0] = x[1];
x[1] = x[2];
Figure 3
Page 6 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
#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
#include <stdio.h>
#include <math.h>
main ()
{
int i, j;
int a[3][2]={{1,2},{3,4},{5,6}};
Figure 5
Page 8 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
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;
Figure 6
Page 9 of 10
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
7 8
1 2 3
A=( ) B =( 9 0)
4 5 6
1 2
C=A*B
Figure 7
Page 10 of 10
Prepared By Sarveswaren May 2015