Question [5 Marks]
Consider the following program:
int x,y;
cin >> x >> y;
while(x!=0 && y!=0){
if(x>y){
x = x-y;
}else if(x<y){
y = y-x;
}else y=0;
}
cout << x+y << endl;
What will this program print on the following inputs:
a. 12 5 b. 27 39 c. -2 12 d. 23 0
Question [5 Marks]
Write a function that takes as input a positive integer n and returns the n-th harmonic number. Reminder: the n-th harmonic number is equal to 1+(1/2)+(1/3)+(1/4)+...+(1/n)
Question 3 [10 Marks]
Recall that the % operator calculates the integer remainder of a division (e.g. 8%3 is equal to 2). Consider the following code:
void hail(int n)
{
if(n<=1) return;
2
cout n << “ -> ” ;
if(n%2) hail(3*n+1);
else hail(n/2);
}
What will this function print for n=5? For n=17? For n=6?
Question 4 [10 Marks]
Write a function which will be given as input an array, its size and an integer p. The function will then cyclically shift the array p positions to the right: each element is moved p positions to the right, while the last p elements are moved to the beginning of the array. For example: if we have the array [ 1 2 3 4 5 6], shifting 2 positions to the right should give the array [ 5 6 1 2 3 4 ]. Your function should work correctly for negative values of p.
Question 5 [10 Marks]
Write a function which, given an array of integers, returns the integer that appears most frequently in the array. E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2.
Question 6 [10 Marks]
Write a function that is given two positive integers r,c and allocates a 2-dimensional array with r rows and c columns. The function then returns a pointer to the allocated array.
Question 7 [10 Marks]
For the following exercises recall the definition of a basic linked list node:
struct Node { int data; struct Node* next; };
Write a function which, given a pointer to the first element of a linked list returns true if all elements of the list are unique. In other words, the function should return false if and only if there exist two nodes in the list with the same data.
Question 8 [10 Marks]
Write a function which will be given a pointer to the first node of a linked list and add to the list, for each element, a copy of it appearing immediately after it. For example: suppose we have a list with the following numbers 1,2,3,4. Calling the function should produce the list 1,1,2,2,3,3,4,4.
Answers
Consider the following program:
int x,y;
cin >> x >> y;
while(x!=0 && y!=0){
if(x>y){
x = x-y;
}else if(x<y){
y = y-x;
}else y=0;
}
cout << x+y << endl;
What will this program print on the following inputs:
a. 12 5 b. 27 39 c. -2 12 d. 23 0
Question [5 Marks]
Write a function that takes as input a positive integer n and returns the n-th harmonic number. Reminder: the n-th harmonic number is equal to 1+(1/2)+(1/3)+(1/4)+...+(1/n)
Question 3 [10 Marks]
Recall that the % operator calculates the integer remainder of a division (e.g. 8%3 is equal to 2). Consider the following code:
void hail(int n)
{
if(n<=1) return;
2
cout n << “ -> ” ;
if(n%2) hail(3*n+1);
else hail(n/2);
}
What will this function print for n=5? For n=17? For n=6?
Question 4 [10 Marks]
Write a function which will be given as input an array, its size and an integer p. The function will then cyclically shift the array p positions to the right: each element is moved p positions to the right, while the last p elements are moved to the beginning of the array. For example: if we have the array [ 1 2 3 4 5 6], shifting 2 positions to the right should give the array [ 5 6 1 2 3 4 ]. Your function should work correctly for negative values of p.
Question 5 [10 Marks]
Write a function which, given an array of integers, returns the integer that appears most frequently in the array. E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2.
Question 6 [10 Marks]
Write a function that is given two positive integers r,c and allocates a 2-dimensional array with r rows and c columns. The function then returns a pointer to the allocated array.
Question 7 [10 Marks]
For the following exercises recall the definition of a basic linked list node:
struct Node { int data; struct Node* next; };
Write a function which, given a pointer to the first element of a linked list returns true if all elements of the list are unique. In other words, the function should return false if and only if there exist two nodes in the list with the same data.
Question 8 [10 Marks]
Write a function which will be given a pointer to the first node of a linked list and add to the list, for each element, a copy of it appearing immediately after it. For example: suppose we have a list with the following numbers 1,2,3,4. Calling the function should produce the list 1,1,2,2,3,3,4,4.
Answers
Question
1
a.
Input
= 12 5 output = 1
b.
Input
= 27 39 output = 3
c.
Input
= -2 12 output = Program will loop
forever
d.
Input
= 23 0 output = 23
Question
2
float n,sum=0;
cin
>> n;
while(n!=0){
sum += (1/n);
n-=1;
}
cout
<< sum;
Question
3
input: 5
5->16->8->4->2
input: 17
17->52->26->13->40->20->10->5->16->8->4->2
input: 6
6->3->10->5->16->8->4->2
Question
4
void rotate(int *A, int s, int p){
int
i;
int
temp[s];
for(i
=0; i<s; i++){
temp[i]
= A[i];
}
for(i
=0; i<s; i++){
A[((i+p)%s)]
= temp[i];
}
}
Question 5
int
modefinder(int *A){
int freq=0;
int top = 0;
int i,j,mode;
for(i=0; i<sizeof(A); i++){
for(j=0;
j<sizeof(A); j++){
if(A[i]==A[j]){
freq++;
}
}
if(freq>top){
mode
= A[i];
top
= freq;
}
freq = 0;
}
return mode;
}
Question 6
int **make2dary(int r,int c){
int
i;
int
**ary = new int *[r];
for
(i=0; i<r; i++){
ary[i]
= new int[c];
}
return
ary;
}
Question
7
bool linklistchk(struct Node *head){
while(head!=0){
struct
Node *tmp;
for(tmp
= head->next; tmp!=0; tmp=tmp->next)
if(tmp->data==head->data)
return
false;
head
= head->next;
}
return
true;
}
Question
8
void dup_eli(struct Node *head){
while(head!=0){
struct
Node *cpy = new Node;
cpy->data
= head->data;
cpy->next
= head->next;
head->next
= cpy;
head
= cpy->next;
}
}
Question 4
ReplyDeletevoid rotate(int *A, int s, int p){
int i;
int temp[s];
for(i =0; i<s; i++){
temp[i] = A[i];
}
for(i =0; i<s; i++){
A[((i+p)%s)] = temp[i];
}
}
hello , in this question can it be without pointers?