Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COURSE
Introduction of C Programming
CREDIT HOUR
SEMESTER
LECTURER
LAB REPORT 5
BY
List of Member
Muhammad Haziq Bin Saiful Bahari Azli
Amirul Syahmi Bin Shukran
Muhammad Hazizi Bin Ahmad Khairi
Student ID
2014806964
2014830238
2014207188
PREPARED FOR:
AMIRA WARDIAH HASANUDIN
OBJECTIVES
1.
2.
3.
4.
THEORY
ARRAY DATA STRUCTURE.
For aggregated data with ame type of element,an array can be used as data structure.An array
is a collection of related data elements of the same data type that are referenced by a common
name.
All the elements of an array occupy a set of contiguous memory locations and by using an
index or subscript we can identify each element.
By using an array,the declaration can be simplified like this:
Int mark=[100];
This will reserve 100 contiguous/sequential memory locations for storing the integer data
type.
Graphically can be depicted as:
Name of array
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
-50
6
11
0
671
-34
8
a[7]
a[8]
a[9]
73
1
5
Position number
(subscript) of the element
in array a
ARRAY INITIALIZATION
An array may be initialization at the time of its declaration,which means to give initial values
to an array.Initialization of an array may take the following form:
Similarly the third line assigns the characters a to vowel[0],e to vowel[1] and so on.Note
again,for characters we must use the single apostrophe () to enclose them.Also,the last
character in the array vowel is the NULL character (\0).
Initialization of an array of type char for holding strings may takes the following form:
Char array_name[size]=string_lateral_constant;
For example,the array vowel in the above example could have been written more compactly
as follows:
Char vowel[6]=aeiou;
When the value assigned to a character array is a string(which must be enclosed in double
quotes),the compiler automically supplies the NULL character but we still have to reserve
one extra place for the NULL.
TWO-DIMENSIONAL ARRAY
A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a twodimensional integer array of size x,y you would write something as follows:
type arrayName [ x ][ y ];
To initialize a two dimensional, A specified bracketed values are use for each row. Following
is an array with 3 rows and each row has 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 */
};
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};
PROCEDURE
PART A: ARRAY DECLARATION
1.
2.
3.
4.
5.
6.
printf(%d %d%d,arr_a[y],arr_a[x],arr_a[x+y],arr_a[x-y];
7. Execute the program and the output.
8. Using single printf statement,print output of array arr_a for the first and last elements.
PART C: INPUT-OUTPUT
1.
2.
3.
4.
5.
}
printf(\narray num[%d]: , i);
for(i=0;i<4;i++)
printf(%d , num[i]);
6. Execute the program and insert integer numbers for all num array elements.
PART D: CHARACTER ARRAY
1.
2.
3.
4.
5.
for(i=0;i<max; i++){
if(num[i]>large)
large = num[i];
if(num[i]<small)
small = num[i];
}
7. Using printf statement, print out the large and small values.
8. Execute the program and observe the output.
CONCLUSION
From this experiment, we are able to declare an array properly in Part A. We know that an
array is a variable that can store many values. In Part B, we are able to initialize a value to an
array. We found out there was an error before we modify the printf from the procedure. In
Part C, we are able to input a value to the array and display the output through the use of
printf. In Part D, we learned that an array can store a string. We also found out that if the
input has a space between the characters it would not read the next line. In Part E by using the
loop repetition, we are able to make a program that displays the sum of the value that we
input into the program. In Part F, by using the if statement in the program we had made a
program that would find the largest and smallest value in the array. In Part G, we learn how to
make an array inside a function. In Part H, we were able to learn how to define a two
dimensional array that is list of an array.