You are on page 1of 23

DEPARTMENT OF ELECTRICAL ENGINEERING

DEC20012 – PROGRAMMING FUNDAMENTALS (PRACTICAL REPORT)

PRACTICAL WORK 1 2 3 4 5 6

TITLE Arrays

SUBMISSION DATE 13/12/2021


GROUP MEMBERS REGISTRATION NUMBER
1. SARAHMEI BINTI ABDULLAH 17DTK20F2005
2.
3.
4.
PROGRAMME
MOHD ZEID BIN ABU BAKAR
LECTURER/INSTRUCTOR
Result/Practical Work / 80%
Discussion/Question / 20%
TOTAL / 100%
LECTURER/INSTRUCTOR COMMENTS RECEIVED DATE
Practical Work 6 Rubrics
Course Learning Very Good Good Average Poor Very Poor Marks
Outcome (CLO)/ Skills/ Aspects
(Weightage = 3)
Cluster (CLS) 4 3 2 1 0
The program does not
The program fulfils only solve the original
The program fulfils its The program fulfils only The program fulfills only a
three quarter of its intended problem or is incorrect.
intended objectives and is half of its intended quarter of its intended
Task A correct.
objectives but is still
objectives. objectives. Program is Does not executes due to
correct. errors
It executes without errors Program is executable executable
Executes without errors

The program does not


The program fulfils only solve the original
The program fulfils its The program fulfils only The program fulfills only a
three quarter of its intended problem or is incorrect.
intended objectives and is half of its intended quarter of its intended
Task B correct.
objectives but is still
objectives. objectives. Program is Does not executes due to
CLO2P: correct. errors
It executes without errors Program is executable executable
Build programs Executes without errors
written in C The program does not
language for The program fulfils only solve the original
The program fulfils its The program fulfils only The program fulfills only a
assigned mini intended objectives and is
three quarter of its intended
half of its intended quarter of its intended problem or is incorrect.
project during Task C correct.
objectives but is still
objectives. objectives. Program is Does not executes due to
practical works correct. errors
It executes without errors Program is executable executable
Executes without errors
sessions
The program does not
CLS3a: The program fulfils only solve the original
The program fulfils its The program fulfils only The program fulfills only a
three quarter of its intended problem or is incorrect.
Practical Skills intended objectives and is half of its intended quarter of its intended
Task D correct.
objectives but is still
objectives. objectives. Program is Does not executes due to
correct. errors
It executes without errors Program is executable executable
Executes without errors

The program does not


The program fulfils only solve the original
The program fulfils its The program fulfils only The program fulfills only a
three quarter of its intended problem or is incorrect.
intended objectives and is half of its intended quarter of its intended
Task E correct.
objectives but is still
objectives. objectives. Program is Does not executes due to
correct. errors
It executes without errors Program is executable executable
Executes without errors

/4 [Task A]
Successfully /4 [Task B]
Task is halfway Cannot complete
Successfully completed completed all task A great extent of help
Autonomy completed with task and /4 [Task C]
all task independently with minimal is needed
moderate supervision procedures /4 [Task D]
supervision
/4 [Task E]
TOTAL /80
DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK RUBRIC
CLO 2P

POLITEKNIK SULTAN IDRIS SHAH


JABATAN KEJURUTERAAN ELEKTRIK
DEC20012 - PROGRAMMING FUNDAMENTALS
Title Arrays
Practical
Work 6
Course DEP 2A / DEP 2B / DTK 2A / DTK 2B
Session June 2019

Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.

OBJECTIVES :

Upon completion of this practical, students should be able:

1. To construct programs that use arrays in C language


2. To construct programs that two dimensional arrays in C language
3. To construct programs that use three dimensional arrays in C language

EQUIPMENTS :

1. Personal computer / Laptop


2. Dev-C++ software

Lab 6.1 Initializing & Accessing Arrays in C language

Procedures:

6.1.1 Write the following programming below and try to comprehend it. Save it as
Practical61.

#include <stdio.h> int


main ()
{
/* initializing array n with 10 integers */
int n[ 10 ] = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109 };

/* output each array element's value */


printf("Element[0] = %d\n", n[0] ); printf("Element[1]
= %d\n", n[1] ); printf("Element[2] = %d\n", n[2] );
printf("Element[3] = %d\n", n[3] ); printf("Element[4]
= %d\n", n[4] ); printf("Element[5] = %d\n", n[5] );
printf("Element[6] = %d\n", n[6] ); printf("Element[7]
= %d\n", n[7] ); printf("Element[8] = %d\n", n[8] );
printf("Element[9] = %d\n", n[9] );
return 0;
}

PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6


CLO 2P

PSIS

Lab 6.2 Initializing & Accessing Arrays by using Loop

2
/ JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P
Procedures:

6.1.2 Write the following programming below and save it as Practical52.


6.1.3 Observe that for loop are being used to both initialize and access the array’s
elements.

#include <stdio.h>
int main ()
{ int n[ 10 ]; /* n is an array of 10 integers */
int i,j; /* initialize elements of array n to 0
*/

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


{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
} return
0;
}

PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6


CLO 2P

4
/ JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6


CLO 2P

PSIS

Lab 6.3 Initializing & Accessing Two Dimensional Arrays

Procedures:

6.1.4 The simplest form of a multidimensional array is the two dimensional array.
Write the following programming codes below and save it as Practical63.
6.1.5 Observe how two dimensional arrays is being initialized and accessed.

#include <stdio.h> int


main ()
{
/* an array with 5 rows and 2 columns*/ int a[5]
[2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}}; int i,
j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{ for ( j = 0; j < 2; j++ )
{ printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
} return
0;
}

6
/ JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6


CLO 2P

8
/ JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

Lab 6.4 Passing Arrays as Function Arguments

6.1.6 Carefully write the following programming codes and try to comprehend.
Save it as Practical64.

#include <stdio.h> /* function


declaration */ double getAverage(int
arr[], int size); int main ()
{
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17,
50}; double avg;
/* pass pointer to the array as an argument
*/ avg = getAverage( balance, 5 ) ; /* output
the returned value */ printf( "Average value
is: %f ", avg ); return 0; }
double getAverage(int arr[], int size)
{ int i; double avg; double sum;
for (i = 0; i < size; ++i)
{ sum += arr[i];
} avg =
sum / size;
return avg;
}

9
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

10
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

11
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P
Task A

Write a program that is able to read and display value from the following
array element by referring to its index number.

house

[0] [1] [2] [3]

Answer:

12
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

Task

Write a program

that ask the user for daily rainfall reading for a period of one
week and assign it into an array named rain[]. The program must be able to
access the array element and display it onto the screen while at the same
time calculating the summation of the daily rainfall by using for loop. The
program then display the summation.

Answer:

13
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

Task

Write a program
TASKC

to analyze the final exam marks for a class of 25 students. Ask


the user to get the marks one by one. Then calculate and print on the
screen the total and average marks of the students

Answer:

14
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

Task

Write a program
D

that reads 10 integer numbers from the user and store it in an array.
The program must be able to determine and print only even numbers and the
total of the even numbers to the screen. The program format is as follows:

Number 1: xx
Number 2: yy

Number 10: zz

The even numbers are:


cc zz

Total even numbers are: 2

Answer:

15
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

Task

Write a program
E

that uses a two dimensional array by the name grades and is


able to calculate the average marks of a group of five students for two
subjects.

grades[0] 80 70 65 89 90
grades[1] 85 80 80 82 87

[0] [1] [2] [3] [4]


Answer:

16
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P

Questions (Answer all questions)

1. Which of these statement best describes an array?


a) A data structure that shows a hierarchical behaviour
b) Arrays are immutable once initialized
c) A container of objects of similar types
d) Array is not a data structure

2. What is the right way to initialize array?


a) int num[5] = { 1, 2, 3, 4, 5};
b) int num{5} = { 1, 2, 3, 4, 5};
c) int num[5] = { 1, 2, 3, 4, 5}
d) int num(5) = { 1, 2, 3, 4, 5};

3. An array elements are always stored in memory locations.


a) sequential
b) random
c) sequential and random
d) specific

4. What is the maximum number of dimensions an array in C may have?


a) 2
b) 3
c) 4
d) Theoretically no limit

5. What is the output of the following program?

#include<stdio.h>
int main()
{
float marks[3] = {90.5,92.5,96.5};
int a=0;
while(a<3)
{
printf(“%.2f”, marks[a]);
a++;
}
}

17
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P
a) 90.5 92.5 96.5
b) 90.50 92.50 96.50
c) 90 92 96
d) Compile error

6. What is the output of C program?


#include<stdio.h>
int main()
{
int a[3] = {10,12,14};
a[1]=20;
int i=0;
while(i<3)
{
printf(“%d”, a[i]);
i++;
}
}
a) 20 12 14
b) 10 20 14
c) 10 12 20
d) 10 12 14

7. What are the types of array


a) long
b) float
c) char
d) All of the above

8. An array index starts with


a) -1
b) 0
c) 1
d) 2

9. What is the output of the following code?


#include<stdio.h>
void main()
{
int arr[10] = {1,2,3,4,5};
printf(“%d”, arr[5]);
}

a) 5
b) 6
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6
CLO 2P
c) 0
d) None of these

10

10. What seems to be the problem with the program below?

#include<stdio.h>
void main()
{
int size, i;
scanf(“%d”, &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf(“%d”, arr[i]);
printf("%d",arr[i]);
}

a) There is no problem and runs successfully


b) The code is erroneous since the statement declaring array is invalid.
c) The code is erroneous since the subscript for array used in for loop is in
the range
1 to size
d) The code is erroneous since the values of array are getting scanned
through the loop

Prepared by: Checked by: Approved by:

MOHD ZEID BIN ABU BAKAR


PENSYARAH
JABATAN KEJURUTERAAN ELEKTRIK
POLITEKNIK SULTAN IDRIS SHAH

11

PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 6

You might also like