Sei sulla pagina 1di 9

FACULTY OF ELECTRICAL ENGINEERING

UNIVERSITI TEKNOLOGI MARA


TERENGGANU
(DUNGUN CAMPUS)
PROGRAMME

Diploma in Electrical Engineering

COURSE

Introduction of C Programming

CREDIT HOUR

4 hrs (2 hr lecture, 2hr Lab)

SEMESTER

Nov 2014 - Apr 2015

LECTURER

AMIRA WARDIAH BT HASANUDIN (Sarjana 1121)

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.

To understand the use of an array in storing multiple tables of values


To be able to use arrays in a program function.
To be able to define and use multiple dimensional arrays.
To understand the use of two dimensional array

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.

Create a new project named DeclareArray.


Include the stdio.h header file and define the main ( ) function.
Declare an integer variable named x and assign value of 3 to the variable.
Declare an integer variable named y and assign value of 1 to the variable.
Declare a integer array variable named arr_a with 5 number of element/array index.
Type the following statements.
arr_a[0]=5;
arr_a[1]=7;
arr_a[2]=9;
arr_a[3]=11;
arr_a[4]=13;

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 B: ARRAY INITIALIZATION


1. Create a new project named InitializeArray.
2. Include the stdio.h header file and define the main ( ) function.
3. Type the following statements.
Int num[4]={1,2};
Char huruf [4]={a,i};
Char nama[4]=tom
Int i;
for(i=0;i<4;i++
printf(%c\t %s\t%d\n\n,huruf[i],nama[i],num[i]);
4. Execute the program and observe the output.
5. Modify the printf with the following statement.
printf (%c\t%s\t%d\n\n,huruf[i],nama,num[i]);
6. Execute the program and observe the output.Explain why the program can be
executed correctly after printf statement modification.

PART C: INPUT-OUTPUT
1.
2.
3.
4.
5.

Create a new project named Array.


Include the stdio.h header file and define the main ( ) function.
Declare an integer array variable named num with 4 number of element.
Declare an integer variable named i.
Type the following statements.
for (i=0; i<4; i++){
printf(\ninput num [%d]: , i);
scanf(%d , num[i]);

}
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.

Create a new project named CharArray.


Include stdio.h header file and define the main ( ) function.
Declare an integer variable named i.
Declare a character array variable named string1 with 20 array index.
Type the following statements.
char string2 [ ]= string literal;
printf(Enter a string: );
scanf(%s, &string1);
printf(string1 is: %s\nstring2 is: %s\n
string1 with spaces between characters is:\n,
string1, string2);

for(i=0;string1[i] != \0; i++)


printf(%c, string1 [i]);
6. Execute the program.
7. Type the input Character Array.

PART E: BASIC OPERATION OF ARRAY


1.
2.
3.
4.
5.

Create a new project named SumElementArray.


Include the stdio.h header file and define the main ( ) function.
Declare an integer variable named i.
Declare an integer variable named sum and assign value of 10 to the variable.
Declare an integer array variable named x with 8 array index and initialize all the
elements which are 7, 5,4,2,1,8,18 and 9.
6. Type the following statements.
Printf(Before : \nsum= %d\narray x = , sum);
For(i=0; i<8;i++)
printf(%d, x[i]);
x[3]=22;
sum = x[5] + x[3];
sum += x[2];
x[6] += 1;
x[7] = x[0] + x[3];
printf(\nAfter : \nsum =%d\narray x = , sum);
for(i=0; i<8;i++)
printf(%d , x[i]);
printf(\n\n);
7. Execute the program and observe the output.

PART F: SORTING ARRAY


1.
2.
3.
4.
5.
6.

Create a new project named SortArray.


Include the stdio.h header file.
Define a constant named named max with value 8/
Define the main function.
Declare three integers variable named i, large and small.
Type the following statements.
int num[8]={4,8,13,0,-5,3,20,-1};
printf(Array: );
for(i=0;i<max;i++)
printf(%d , num[i]);
large = num[0];
small = num[0];

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.

PART G: PASSING ARRAY TO FUNCTION


1. Create a new project named MultiArray.
2. Include stdio.h header file.
3. Declare a prototype integer function called addNumbers with one parameter, an
integer array.
4. Define the main function.
5. Inside the main function, declare an integer variable named i.
6. Then, declare an integer array variable named array with 5 number of element.
7. Type the following statements inside the main function.
printf(Enter 5 integers separated by spaces: );
for(i-0;i<5;i++){
scanf(%d, &array[i]);
}
printf(\nTheir sum is: %d\n, addNumbers(array));
return 0;
8. Define addNumbers function with an integer parameter named fiveNumbers.
9. Inside the addNumbers ( ) function, declare an integer variable named i.
10. Then, declare an integer variable named sum and assign value of 0 to the variable.
11. Type the following statements inside the addNumbers function.
for(=i=0;i<5;i++)
sum+=fiveNumbers[i];
return sum;

12. Execute the program and observe the output.


PART H: MULTI-DIMENSIONAL ARRAY
1.
2.
3.
4.

Create a new project named MultiArray.


Include the stdio.h header file and define the main function.
Declare two integer variable named i and j.
Declare two integer variables named arr with 2-dimensional index, row of 2 and
column of 6.
5. Type the following statements.
for(i=0;i<2;i++){
printf(Enter 6 integers separated by spaces: );
for(j=0;j<;j++){
scanf(%d, &arr[i][j]);
}
printf(\n);
}
printf(You entered:\n);
for(i=0;i<2;i++){
for(j=0;j<;j++){
printf(%d, arr[i][j]);
}
printf(\n);
}
6. 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.

Potrebbero piacerti anche