0% found this document useful (0 votes)
120 views

Introduction To Computer Science-103 Final Exam

The document contains the final exam questions for an Introduction to Computer Science course. Question 1 asks about a deadlock situation involving 3 concurrent processes accessing 4 files. Question 2 asks to draw a diagram of a linked list storing student records with 3 fields. Question 3 asks to write an algorithm in pseudocode to compare the contents of two stacks.

Uploaded by

Trungkeeper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

Introduction To Computer Science-103 Final Exam

The document contains the final exam questions for an Introduction to Computer Science course. Question 1 asks about a deadlock situation involving 3 concurrent processes accessing 4 files. Question 2 asks to draw a diagram of a linked list storing student records with 3 fields. Question 3 asks to write an algorithm in pseudocode to compare the contents of two stacks.

Uploaded by

Trungkeeper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to Computer Science-103

Final exam
1. Three processes (A, B, and C) are running concurrently. Process A has acquired
File1, but needs File 2. Process B has acquired File3, but needs File 1. Process C
has acquired File2, but needs File3. Draw a diagram for these processes. Is this a
deadlock situation? (6%) (P7-9)

This is a deadlock situation (see Figure P7-9) because all four conditions of
deadlock (mutual exclusion, resource holding, no preemption, and circular waiting)
are all present.

2. Draw a diagram to show a linked list in which the data part is a student record
with three fields: id, name, and grade. (6%) (P11-13)

The linked list of records


3. Write an algorithm in pseudocode to compare the contents of two stacks. (8%)
(P12-7) Checking the equality of two stacks

4. Figure.1 is a source code translation process. A source is compiled by means of


four tools which are lexical analyzer, syntax analyzer, semantic analyzer, code
generator, respectively. What is the corresponding tool of each phase? (4%)
(Figure 9.1)

(A) (B) (C) (D)

Figure .1
5. Please follow the program to show the results. (4%)
int x = 0;
if (x = 0 || x == 0)
printf("%d\n", x);
printf("%d\n", x);

1
1
6. Write an algorithm in pseudocode to apply binary search on an array of elements.
(6%) (P11-5)

The algorithm shows the binary search routine in pseudocode (see Chapter 8).
Note that we perform the binary search on sorted array. If flag is true, it means x is
found and i is its location. If flag is false, it means x is not found; i is the location
where the target supposed to be.
7. If the subprogram calculate (A, B, P, S) accepts the value of A and B and
calculates their sum S and product P, which variable do you pass by value and
which one by reference? (6%) (P9-23)

A and B should be passed by value, S and P by reference.

8. Write an algorithm to delete an element in a sorted array. The algorithm must call
a search algorithm to find the location of insertion. (6%) (P11-7)

The algorithm that insert an element in a sorted array has two parts. Part a shows
the main algorithm. Part b shows the algorithm named shiftup called by the insert
algorithm.

a. Algorithm P11-7a shows the main algorithm.

b. Algorithm11-7b shows the auxiliary algorithm used by the main algorithm.

The shift-up algorithm used by the insert algorithm


9. A mono-programming operating system runs programs that on average need 10
microseconds access to the CPU and 70 microseconds access to the I/O devices.
What percentage of time is the CPU idle? (6%)

70 / (70 + 10) × 100 = 87.5%

10. Using the selection sort algorithm, manually sort the following list and show your
work in each pass using a table: (6%) (P8-5)

14 7 23 31 40 56 78 9 2

The status of the list and the location of the wall after each pass of the selection
sort algorithm is shown below:

11. Using the UML diagram for the product algorithm, draw a diagram to calculation
the value of xn, when x and n are two given integers. (6%) (P8-38)

Figure 8-38 shows the UML for finding the power of an integer with an integral
exponent.
12. Please follow the program to show the results. (6%)

#include <stdio.h>
void delete_element (int value, int&array_size, int array[]){
int i,;
int location = array_size + 1;

for(i = 0;i< array_size;i++){


if (array[i]==value)
location = i;
}
for (i = location ;i<= array_size;i++)
array[i] = array[i+1];

array_size--;
}
int main(){
int i;
int arraysize = 10;
int a[10] = {0,1,2,3,4,5,6,7,8,9};

for (i=0;i<arraysize;i++)
printf("%d ",a[i]);

delete_element(5,arraysize,a);
printf("\n") ;

for (i=0;i<arraysize;i++)
printf("%d ",a[i]);

return 0;
}

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 6 7 8 9
13. Please show how to build a linked list from scratch using the insertion algorithm
(by following algorithm 11.3) (6%) (P11-16)

Algorithm P11-16 shows the routine in pseudocode for building a linked. The
routing uses the InsertLinkedList algorithm.
14. Show the contents of stack S1 and the value of variable x and y after the following
algorithm segment is executed. (6%) (P12-5)

stack (S1)
Push (S1,5)
Push (S1,3)
Push (S1,2)
if (not empty (S1))
{
pop(S1, x)
}
if (not empty (S1))
{
pop(S1, y)
}
push (S1, 6)

15. Write an algorithm to find the average of the numbers in a linked list of numbers.
(6%) (P11-17)
16. Write an algorithm that reverses the elements of an array so that the last element
becomes the first, the second to the last becomes the second, and so forth. (6%)
(P11-2)
註解 [m1]: 新增此題
17. Change the following code segments to use an if-else statement: (6%)

#include < stdio.h >


int main()
{
float a,b,ans;
char key;
printf("input two number:");
scanf("%f %f",&a,&b);
printf("press +,-,*,/:");
key=getch();
switch(key)
{
case '+':
ans=a+b;
break;
case '-':
ans=a-b;
break;
case '*':
ans=a*b;
break;
case '/':
ans=a/b;
break;
default:
printf("Undefined key\n");
exit(0);
}
printf("%f%c%f=%f\n",a,key,b,ans);
return 0;
}
#include < stdio.h >
#include < conio.h >
void main(void)
{
float a,b,ans;
char key;
printf("input two number:");
scanf("%f %f",&a,&b);
printf("press +,-,*,/:");
key=getch();
if ( key=='+' )
{
ans=a+b;
}
else if ( key=='-' )
{
ans=a-b;
}
else if ( key=='*' )
{
ans=a*b;
}
else if ( key=='/' )
{
ans=a/b;
}
else
{
printf("Undefined key\n");
exit(0);
}
printf("%f%c%f=%f\n",a,key,b,ans);
}

You might also like