You are on page 1of 6

FACULTY OF ELECTRICAL ENGINEERING

UNIVERSITI TEKNOLOGI MARA

COMPUTER PROGRAMMING
(ECE411)

LABORATORY 1.2

ARRAYS

OBJECTIVES
1. To use the array data structure to represent lists and tables of values.
2. To define an array, initialize an array and refer to individual elements of an array.

THEORY

Array Data Structure

For aggregated data with same 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. For example, instead of declaring mark1, mark2, ..., markN to
store and manipulate a set of marks obtained by the students in certain courses, we could declare a
single array variable named mark and use an index, such as N, to refer to each element in mark. This
absolutely has simplified declaration of the variables.

For example, if 100 list of marks of integer type, they can be declared as follows:
int mark1, mark2, mark3, ..., mark100;
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:

1
Array Initialization

An array may be initialized at the time of its declaration, which means to give initial values to an array.
Initialization of an array may take the following form:

type array_name[size] = {value_list};


For example:
int id[7] = {1, 2, 3, 4, 5, 6, 7};
float x[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
char vowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};

The first line declares an integer array id and it immediately assigns the values 1, 2, 3, ..., 7 to id[0],
id[1], id[2],..., id[6].
In the second line assigns the values 5.6 to x[0], 5.7 to x[1], and so on.
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 automatically supplies the NULL character, but we still have to reserve one extra place
for the NULL.

2
PROCEDURE

PART A: ARRAY DECLARATION


1. Create a new project named: Lab1.2-PartA.
2. Include the stdio.h header file and define the main function.
3. Declare an integer variable named x and assign value of 3 to the variable.
4. Declare an integer variable named y and assign value of 1 to the variable.
5. Declare an integer array named arr_a with 5 number of elements.
6. 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 %d", arr_a[y], arr_a[x], arr_a[x+y],


arr_a[x-y]);

7. Execute the program and observe the output.


8. Using single printf statement, print output of array arr_a for the first and last elements.

PART B: ARRAY INITIALIZATION


9. Create a new project named: Lab1.2-PartB.
10. Include the stdio.h header file and define the main function.
11. Type the following statements.

int num[4]={1,2}; // Integer array initialization


char huruf[4]={'a','i'}; // Character array initialization
char nama[4]="tom"; // Character array initialization using string
literals

int i;
for(i=0; i<4; i++)
printf("%c\t%s\t%d\n\n", huruf[i], nama[i], num[i]);

12. Execute the program and observe the output.


13. Modify the printf statement into the following statement.

printf("%c\t%s\t%d\n\n", huruf[i], nama, num[i]);

14. Execute the program and observe the output. Explain why the program can be executed correctly
after the printf statement modification.

3
PART C: INPUT - OUTPUT OF ARRAY
15. Create a new project named: Lab1.2-PartC.
16. Include the stdio.h header file and define the main function.
17. Declare an integer array variable named num with 4 number of elements.
18. Declare an integer variable named i.
19. 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]);

20. Execute the program and insert integer numbers for all num array elements. Observe the output.

PART D: CHARACTER ARRAY


21. Create a new project named: Lab1.2-PartD.
22. Include the stdio.h header file and define the main function.
23. Declare an integer variable named i.
24. Declare a character array named string1 with 20 array index.
25. 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]);

26. Execute the program.


27. Type input : Character Array. Observe the output.

4
REPORT (LAB 1.2)
In a group of 3-4 students, submit a report (combine Lab 1.1 & Lab 1.2) with a standard report cover
and the following content:

- Program and Output for each part (print screen)

- Discussion (for each part) and Conclusion (relevant to the objectives)

- Exercise (coding in handwriting and print screen the output)

EXERCISE
Write a C program to store 10 integer numbers in an array and print the array values.

REFERENCES
Deitel H. M and Deitel P., C How to Program, Prentice Hall, 7th Edition, 2012.
Stephen G. K., Programming in C, Addison-Wesley Professional, 4th Edition, 2013.
Horton I., Beginning C, Apress, 5th Edition, 2013.
Martin J. G., An Easy Guide to Programming in C, CreateSpace Independent Publishing Platform,
2012.
Perry G. and Miller D., C Programming Absolute Beginner's Guide, Pearson Education, 3rd Edition,
2015.

5
ECE411 : COMPUTER PROGRAMMING

EVALUATION CRITERIA FOR LABORATORY REPORT

FACULTY OF ELECTRICAL ENGINEERING


UNIVERSITI TEKNOLOGI MARA (PULAU PINANG)

LAB NO. STUDENT NAME STUDENT ID SUBMISSION


DATE
S1.

S2.

S3.

S4.

CO- Assessment Marks Weight Score Marks


PO Criteria (w) (s) (s x w)
1 2 3 4 5 S1 S2 S3 S4 S1 S2 S3 S4

CO2- Requirement Discussion Program & Program & Program & All program 0.75
PO5 • Program or conclusion output are not output are output are & output are
variables, is not sufficiently discussed, discussed, all well
memory included discussed, some observations discussed,
contents some observations are correct. all
• Discussion observations are partially observations
based on are partially correct. are correct.
program and correct.
lab procedure
• Conclusion
CO2- Report Format Not follow Not follow Follow Cover all Cover all 0.25
PO5 • Standard cover standard standard standard report report
page format and format or not format but content but content and
• Page not cover all cover all not cover all not use follow
numbering report report content report standard standard
• Cover all report content content cover page / format
content flowchart
• Use standard
flowchart,
coding, etc.
(if any)
Total Evaluated Marks ( /10) 20
 Marks can be deducted for any delay/late of report submission

You might also like