top of page

FE1008/CY1402-Computing 2016-2017 Semester 2-Question 3

Below are comments and further explanation on question 3.

Question 3.a)

This part is a giveaway question, only thing to note is that the question asks for declaration STATEMENTS. Therefore, you need to remember to put the semicolons at the end of each statement. I have provided some alternatives to some of the answers.

Question 3.a)i)

  1. int num1[10];

  2. int num1[10] = {0}; // initialise all num1 elements to 0, not necessary.

  3. int num1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // initialise with 10 zeros.

Question 3.a)ii)

  1. int num2[2][10] = { {1,2,3,4,5,6,7,8,9,10} }; // only initialise the first row, since not enough initializers.

  2. int num2[2][10] = { {1,2,3,4,5,6,7,8,9,10}, {0} }; // initialise first row with the numbers and second row with 1 zero, the rest follows with zeros. But this way of writing makes it clear to you that you have 2 inner arrays in that outer array.

For 2D arrays, the first value inside the square bracket is the row value and the second square brackets contain the column value. 2D array is like having an array inside another array, we can think of num2 as a integer array of containing 2 row elements, while each row is also an array containing 10 column elements, total of 2 * 10 = 20 array elements in this 2D array.

Question 3.a)iii)

int i;

Question 3.a)iv)

  1. char ch1[] = "Teddy"; // no need for the number of elements in the square brackets since we are initialising it with "Teddy" at the same time.

  2. char ch1[6] = "Teddy"; // number of elements is 6 in that string.

If you insist in filling up the square brackets, please remember that a string/array-of-characters always end with a terminator character. Hence, the string "Teddy" contains 6 characters.

Question 3.b)

A very straight forward question, requires your knowledge on for loops. A simpler version is this:

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

{

printf("Enter integer %d", i+1);

scanf("%d", &num1[i]);

}

However, in reality, this might not be the best solution since it does not check if the input is actually an integer. I have written a more fool-proof code for this question, the source code can be found here.

Note that array's index, start from 0 (not 1). So a common way of writing for loops dealing with array is as such:

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

{

...

}

Normally, the counter, i, is given a value of zero and the conditional expression: i is STRICTLY SMALLER THAN the total number of elements in that array.

Question 3.c)

Essentially, this question requires us to add elements with the same index from the 3 arrays together and put it inside num1 array using a while loop.

// assume i = 0

while (i < 6)

{

//i < 6 makes this run up to i = 5, index 5 is the sixth element

num1[i] += num2[0][i] + ch1[i];

i++; }

Note: we want the first 6 cells of num2 which refers to the 1st row of num2 and that's why we use index 0 for the row index. Alternatively, you could try to use pointers to do the job but it will be much more complex and I shall not cover it here.

Another minor note is that if you are not comfortable with using things like "+=" or lumping things together, you can rewrite the above statement with the following alternatives:

1) num1[i] = num1[i] + num2[0][i] + ch1[i];

2) num1[i] = num1[i] + num2[0][i];

num1[i] = num1[i] + ch1[i];

3) num1[i] += num2[0][i];

num1[i] += ch1[i];

4) int sum = num1[i] + num2[0][i] + ch1[i];

num1[i] = sum;

Question 3.d)

This part is slightly more difficult but should be still manageable for you. Here we only need to worry about the first 6 cells. They have said that num1 array contains all zeros which simplify our calculation to just num2 and ch1 arrays.

When a character is added to an integer, it will be implicitly converted to a corresponding integer value according to the ASCII Table.

Index num2[] ch1[] ASCII final value

0 1 'T' 84 1 + 84 = 85

1 2 'e' 101 2 + 101 = 103

2 3 'd' 100 3 + 100 = 103

3 4 'd' 100 4 + 100 = 104

4 5 'y' 121 5 + 121 = 126

5 6 '\0' 0 6 + 0 = 6

Question 3.e)

Size of array num1 before and after the WHILE operation is that same at 4*10 = 40 bytes. It is because the size of array (10) has already been decided at the time of declaration and will not change throughout the program. Furthermore, changing the value of each cell only changes the representation of the bits in the 4 bytes of the integer and does not alter the byte size of the integer cell; i.e. integer 1 and integer 32000 both requires 4 bytes in memory but each bit in those 4 bytes will be represented differently so that the computer can differentiate the 2 numbers apart.

If you would like to see my source code, or my other code variations, please check out FE1008/CY1402-Computing 2016/2017 Semester 2-Comments & Source Code.

That's all for Question 3. If you have any doubts, opinions or suggestions, feel free to leave them in forum section or email me @ KYX@outlook.sg. Thanks~

Related Posts

See All
Recent Posts

Related Posts

Leave your comments or questions below:

bottom of page