UNIVERSITY OF KWAZULU-NATAL
School of Engineering
Discipline of Electrical, Electronic and Computer Engineering
( Howard College Campus )
Examinations: June 2013
ENEL2CA H1 : Computer Methods 1
D URATION :2 HOURS
M ARKS : 100
Examiners:
Prof. J.R. Tapamo
Mr. B. Naidoo
Instructions:
1. Answer ALL questions.
2. Ensure that your answers are clearly numbered.
3. Calculators are not allowed.
4. Answer Section A on the multiple choice answer sheet provided.
5. Ensure you have 10 pages, including this one.
2
Section A: Multiple Choice
[36 marks]
Answer on the multiple-choice sheet. Use an HB pencil only. Negative marking
will be used. Correct (+3), not attempted (0), incorrect (-0.75). For each question
choose the best of the available answers.
1. There are several errors that could occur when developing a C program. An error that is not reported
by the compiler and not explicitly reported during execution is a:
(A) Logical error
(B) Syntax error
(C) Runtime error
(D) Environment error
(E) Linking error
2. After the execution of the following fragment of code
int a = 3, b = 7, c = 4;
x = b++ * (b - a - 1) / (--c);
what will the variable x holds?
(A) 12
(B) 11.5
(C) 5.25
(D) 8
(E) none of the above
3. What value does function Andthen return when called with a value 3:
void Andthen (int x)
{
if (x > 0)
Andthen(--x);
printf("%d, ", x);
}
int main()
{
Andthen(5);
return 0;
}
(A) 1, 2, 3, 4, 5, 5,
(B) 4, 3, 2, 1, 0, 0,
(C) 5, 4, 3, 2, 1, 0,
(D) 0, 0, 1, 2, 3, 4,
(E) 0, 1, 2, 3, 4, 5,
3
4. Pointers can be used to
(A) implement stacks
(B) simulate call-by-reference
(C) manipulate dynamic data structure
(D) store address of a variable
(E) all of the above
5. Given the following fragment of code
char *Extract(char *ptr)
{
ptr +=3;
return (ptr);
}
int main()
{
char *x, *y;
x = "MARKING";
y = Extract(x);
printf("y = %s\n", y);
return 0;
}
what will be printed after the execution of the above code
(A) y = AKING
(B) y = KING
(C) y = MARKING
(D) y = ING
(E) x = 2
6. Consider the main function below. A function Mode is called to calculate the mode of the histogram
represented by the array Image.
void main() {
int Image[] = {1,2,0,23,34,90,34};
int dimension = 7;
int mode;
Mode(Image, dimension, &mode);
printf("mode = %d\n",mode);
}
Which of the following is the correct function declaration for the Mode function?
(A) double Mode(int Image, int dim,
(B) void
(C) void
int *mode);
Mode(int Image[], int dim, int mode);
(D) float
Mode(float Image[], int dim, int *mode);
Mode(int Image, int dim, int mode);
(E) void
Mode(int Image[], int dim, int *mode);
4
7. Assume that the file "number.txt" contains the following numbers:
23
33
43
23
13
13
23
10
33
10
What is printed out by the following C code fragment?
FILE *infile;
int i, j, sum = 0;
infile = fopen("number.txt", "r");
while (fscanf(infile, "%d %d", &i, &j) != 0)
sum += i < j? i: j;
printf("%i", sum);
(A) 36
(B) 84
(C) 135
(D) 98
(E) 79
8. Consider the definition of a simply linked list of integers in which the individual elements have the
following type:
typedef struct node {
int
data;
struct node *Link;
} IntList;
Given the following fragment of code
void freeList(IntList *ptr)
{
while(ptr)
{
????
}
}
Which one of the following can replace ???? for the function call freeList(List) release the memory
allocated to List?
(A) ptr = ptr->Link; free(ptr);
(B) IntList *ptri = ptr; free(ptr); ptr = ptri->Link;
(C) IntList *ptri = ptr; ptr = ptr->Link; free(ptr);
(D) IntList *ptri = ptr; free(ptri); ptr = ptr->Link;
(E) None of the above
5
9. Consider the main function below. A function rearrange is called to rearrange values in three
variables num1, num2, and num3.
#include <stdio.h>
void rearrange(int *a, int *b, int *c);
main() {
int num1 = 14, num2 = -12, num3 = 11;
rearrange(&num1, &num2, &num3);
}
void rearrange(int *a,
{
int a1,a2,a3;
a1 = *a<*b? *a : *b;
*a = a1 < *c? a1:*c;
*b = a3 < a2? a3:a2;
}
int *b, int *c)
a2 = !(*a<*b) ? *a : *b;
a3 = !(a1<*c) ? a1 : *c;
*c = !(a3 < a2)? a3:a2;
After the calling of function rearrange Which of the following is the correct?
(A) num1 = 14 , num2 = -12, num3 = 11;
(B) num1 = 14 , num2 = 11, num3 = -12;
(C) num1 = 12 , num2 = 11, num3 = 14;
(D) num1 = -12 , num2 = 11, num3 = 14;
(E) None of the above;
10. Given the following declarations and instructions:
#include <stdio.h>
main()
{ typedef struct
{ char code[4];
char state[40];
struct { int x;
int y;
int z;
}position;
} PILONE;
PILONE pilones[4] =
{ {"d234", "damage", {4, 6, 9}}, {"c231", "GoodShape", {3, 45,18}},
{"j971", "damage", {32, 12, 23}},{"p341", "GoodShape", {32, 15,10}},
};
PILONE *p, *q;
int sumres =0;
p = &pilones[0]; q = p+4;
for(; p<q; p++) sumres += p->position.x;
printf("\nsumres = %d", sumres);
printf("\n");
system("PAUSE");
}
Execution of the above code will print the following sequence?
(A) sumres = 72
(B) sumres = 78
(C) sumres = 71
(D) sumres = 70
(E) None of the above
[4]
6
11. Given the following definitions, what is the value of Array[Array[0][1]][1]?
int Array[3][4] = {1,2,11,3,25,4,5,10,6,7,12,13};
(A) 3
(B) 4
(C) 10
(D) 13
(E) 11
12. What is a proper way to open the file FileName.bin for writing as binary file?
(A) FILE *f = fwrite("FileName.bin","b");
(B) FILE *f = fopen("FileName.bin","wb");
(C) FILE *f = fopenb("FileName.bin","w");
(D) FILE *f = fwriteb("FileName.bin");
(E) FILE *f = fopenb("FileName.bin","bw");
Section B: Program Execution and Testing
[32 marks]
13. Identify and correct the errors in each of the following fragments of code:
(a) The following function, Fabs, is supposed to compute and return the absolute value of floating
point number:
[2]
void Fabs(double x)
{
if ( a < 0) return (-x);
else x;
}
(b) Assume
int b[10]; ,i;
for(i = 0; i<10;i++)
[2]
b[i, i] =2;
(c) The following piece of code should print all odd values between 1 and 10 included.
[2]
n = 3;
while(n < 10)
{
printf("%d ", n);
n +=2;
}
(d) The following piece of code should receive a real number as argument and print it.
void f(float x)
{
float x;
printf("%d", x);
}
[2]
7
(e) The following fragment of code should read 10 integers and store them in B.
[2]
int B[10] , i;
for(i = 0; i <= 10; i++)
{
scanf("%d", B[i]);
}
14. What is the output of the following code? Show your working.
[4]
#include <stdio.h>
#include <string.h>
char* response( char* str ) { /* processes string str */
int n, h, j, k;
char tmp;
n = strlen(str );
h = (n / 2);
for ( j=0, k=(n - 1); (j < h); j++, k--) {
tmp
= str[k];
str[k] = str[j];
str[j] = tmp;
} /* end for */
return str;
} /* end reverse */
int main( ) {
char s[512] ="nabrud";
printf( "Advertise: %s\n", response( s ) );
system("pause");
return 0;
} /* end main */
15. What is the output ( values in Value, Neg, Pos) of the execution of the following code? Show
your working.
[8]
#include <stdio.h>
#include <string.h>
#include <math.h>
#define SIZE 6
main()
{
int b[SIZE] = {-1,3,8,-10,23,5}, Neg=0, Pos=0;
int Value = Discover(b,SIZE, &Neg,&Pos);
printf("\nThe Value discovered is = %d\n", Value);
system("pause");
}
int Discover(int b[], int size,int *NbrNeg, int *NbrPos)
{
int i;
for(i = size -1; i>=0; i--)
{
if(b[i]<=0) (*NbrNeg)++;
else (*NbrPos)++;;
}
// abs (a) returns the absolute value of a
return (abs(*NbrPos -*NbrNeg));
}
8
16. Consider the definition of a simply linked list of integers in which the individual elements have the
following type:
typedef struct node {
int
data;
struct node *Link;
} IntList;
(a) The main C function given in figure 1 below is supposed to dynamically create the structure,
List, given in the figure 2, but there are missing instructions.
int main()
{
IntList *List = NULL, *p1 ; /* pointers */
int IntArray[4] = {-3,4,3,5};
$$$1$$$
for(i=0;i<4; i++)
{
p1 = malloc(sizeof(IntList));
$$$2$$$;
if(List == NULL)
{
/* Insert the first element */
List = p1;
$$$3$$$
}
else
{
$$$4$$$
List = p1;
}
}
system("pause");
return 0;
}
Figure 1: Main function to create the list in figure 2
Figure 2: Linked-List of -3, 4,3, and 5
(a.1)
(a.2)
(a.3)
(a.4)
What is missing at $$$1$$$
What is missing at $$$2$$$
What is missing at $$$3$$$
What is missing at $$$4$$$
(b) After the creation of List in (a) what will be the value of List->Link->link->data?
[1]
[1]
[2]
[2]
[2]
9
Section C: Design and Coding
[32 marks]
17. Write a function Atof that takes a character string as input and returns an equivalent floating point
number. Assume that each character in the input string is a digit character
(i.e. ’0’, ’1’, ..., ’9’) and that there is at most one dot(.).
For example, Atof("189.81") should return an floating point number 189.81. (You may use the
fact that 5=’5’-’0’, Conversion of floating point numbers in scientific notation [e.g. "1.7e8"]
are not considered)
[10]
18. A polygon is a closed, 2-dimensional (planar) shape made up of three or more straight line segments
connected end to end. If n points are connected to form a closed polygon as shown in figure 3, the
area of the polygon can be computed as
n−2
1 X
(xi+1 + xi )(yi+1 − yi )|
A= |
2 i=0
Notice that although the illustrated polygon has only six distinct corners, n for this polygon is
7 because the algorithm expects that the last point, (x6 , y6 ), will be a repeat of the initial point,
(x0 , y0 ).
(a) Define a type, ClosedPolygon, to represent a closed polygon that is made of the number
of points of the polygon and the array of (x, y) coordinates of the polygon’s points. The
polygon shown in Table 1 and Figure 3 will be represented by an object Polygon of type
ClosedPolygon, as follows:
[6]
Polygon = { 7,
{
{4 , 0}, {4 , 7.5}, {7 , 7.5}, {7, 3},
{9 , 0}, {7 , 0}, {4 , 0}
}
};
(b) Define a function InputPolygon that enables the user to input the polygon from the keyboard.
[4]
(c) Define a function OutputPolygon that displays the coordinates of polygon passed as an
argument.
[4]
(d) Define a function AreaPolygon that calculates the polygon area. For one test, use the data
given in table is 25.5 square units.
[8]
10
Table 1: Example of internal representation of a polygon
x y
4 0
4 7.5
7 7.5
7 3
9 0
7 0
4 0
Figure 3: Example of closed polygon
© Copyright 2026 Paperzz