0% found this document useful (0 votes)
44 views4 pages

Mid Term 1 - Solution

Uploaded by

l227996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Mid Term 1 - Solution

Uploaded by

l227996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

National University of Computer and Emerging Sciences, Lahore Campus

Course Name: Object Oriented Programming Course Code: CS1004


Degree Program: BS (CS, SE, DS) Semester: Spring 2023
Exam Duration: 60 Minutes Total Marks: 20
Paper Date: 27-Feb-2023 Weight 15
Section: ALL Page(s): 4
Exam Type: Midterm-I
Student : Name:_____________________________ Roll No.________________ Section:_______
Instruction/Notes:
1. Attempt all questions. Answer in the space provided. Answers written on rough sheet will not
be marked. Do not use pencil or red ink to answer the questions. In case of confusion or ambiguity
make a reasonable assumption.

Question 1: [CLO 1] (Marks: 10)


A robot is designed to examine a crop field to detect and count the number of insects. The crop field is divided into
four parts where each part has a different crop. The robot’s camera (receptive field) can detect pests in a fixed size
10x10 square feet region at a time. Additionally, the software in the robot can detect and count number of pests in
every 1x1 square foot region. You can imagine that the crop field is just an XY plane where origon (0,0) is at the mid of
the field. Crop 1 is in 1st quadrant, crop 2 is in 2nd quadrant and so on. In order to minimize the use of pesticide, the
experts need to determine the number of pests in each 1x1 region. Your task is to get the data from the robot and
transform it such that the experts can decide the amount of pesticide for each crop in every 1x1 region. Define a C++
function that gets the top left corner (w.r.t origon) of the robot’s receptive field and a two dimensional array p_count
of 10x10 size as parameters. The array p_count contains the number of pests in 10x10 region of the crop field. The job
of this function is to dynamically create and return a two dimensional array having four rows where each row of this
array contains the number of pests in 1x1 regions for a particular crop. You need to store a sentinel value ‘-1’ at the
end of each row after storing the information from the given grid, indicating the end of list. Consider the following
example.

Here the highlighted 10x10 region has the top left corner (-4,2). There are 12 1x1 regions of crop1, 8 regions of crop 2,
32 regions of crop 3 and 48 such regions of crop 4. The resultant two dimensional array must have sizes 13, 9, 33, 49
for row1, row2, row3 and row 4 respectively. The resultant array must be as follows.

Note: You are supposed to provide a generic code that should work accurately for any given top left corner.

Department of Computer Science Page 1 of 4


int ** transform_data(int p_countn[][10], int topLeft_x, int topLeft_y) {
int n[4], col_L, row_U, x, y;
//row_U counts rows above the origion
if (topLeft_y > 0 && topLeft_y - 10 >= 0)
row_U = 10;
if (topLeft_y > 0 && topLeft_y - 10 <= 0)
row_U = topLeft_y;
if (topLeft_y <= 0)
row_U = 0;

//col_L counts colums on the left of origion


if (topLeft_x < 0 && topLeft_x+10 <=0)
col_L = 10;
if (topLeft_x < 0 && topLeft_x + 10 >= 0)
col_L = -1 * topLeft_x;
if (topLeft_x>=0)
col_L = 0;

//counts number of elements in each quadrant


n[0] = (10 - col_L) * row_U;
n[1] = col_L * row_U;
n[2] = col_L * (10 - row_U);
n[3] = (10 - col_L) * (10 - row_U);

int** data = new int* [4];


for (int i = 0;i < 4;i++)
data[i] = new int[n[i]+1];//allocate dynamic memory for each quadrant
//copy data of each quadrant in different rowws of data
int k = 0;
for (x = 0;x <row_U;x++)
for (y = col_L;y < 10;y++)
data[0][k++] = p_countn[x][y];
data[0][k] = -1;
k = 0;
for(x= 0; x< row_U;x++)
for(y=0;y<col_L;y++)
data[1][k++] = p_countn[x][y];
data[1][k] = -1;
k = 0;
for (x = row_U; x < 10;x++)
for (y = 0;y < col_L;y++)
data[2][k++] = p_countn[x][y];
data[2][k] = -1;
k = 0;
for (x = row_U; x < 10;x++)
for (y = col_L;y < 10;y++)
data[3][k++] = p_countn[x][y];
data[3][k] = -1;

return data;

Department of Computer Science Page 2 of 4


Question 2: [CLO 2] (Marks: 2*5)
For each of the following part, identify the logical error(s) if any and write the output produced by the code segment.
In case the output cannot be determined write G

CODE SEGMENT OUTPUT


int* sum(int a, int b) { Output: 11 or garbage
int sum = a + b; Error: Accessing local variable in main that is
return &sum;
}
destroyed. addr is a dangling pointer.
int main() {
int* addr = sum(5, 6);
cout << *addr;
}
int main() { Output:
int arr[5] = { 1, 4, 5, 6, 7 }; 6
int *ptr = &arr[2];
cout << (*ptr)+1 << endl;
4
cout << *(ptr-1)<<endl; 7
cout << *(ptr + 2)<<endl; No error
}
int * fun1(int* ptrs) { Output: 1 2 3 4 5 2
int* ptr = new int[7]; Error: in fun1 index out of bound + memory leak in
for (int i = 0; i < 7; i++)
ptr[i] = ptrs[i]; main function.
return ptr;
}
void display(int *ptr) {
if (ptr != nullptr) {
for (int i = 0; i < 5; i++)
cout << ptr[i] <<" ";
}
}
int main() {
int arr[5] = {1,2,3,4,5};
int* ptrs = arr;
int *pt =fun1(ptrs);
display(ptrs);
cout << *(pt+1);
return 0;
}
int main() { Output: Garbage
Error: Dangling pointers [0] and s[1]
char** s = new char* [2];
char* name1 = new char[20];
char* name2 = new char[20]; Correct 2
Partial 1
strcpy_s(name1,20, "Hello"); Incorrect 0
strcpy_s(name2, 20, "World");
s[0] = name1;
s[1] = name2;

delete[] name1;
delete[] name2;
for (int i = 0;i < 2;i++)
cout << s[i] << endl;
delete[] s;
s = nullptr;
return 0;

Department of Computer Science Page 3 of 4


}

class ABC{ Output: ABC() Called.


private: ABC(int) Called.
int a; a= 10
public: a=20
ABC(){ ---
cout<<"ABC() Called.\n";
a = 10; a= 2
} a= 100
ABC(int x){
cout<<"ABC( int ) Called.\n";
a = x;
}
void Print(){
cout<<"a = "<<a<<endl;
}
void SetValue(int y){
a = y;
}
void SomeFunction(){
a = a * 5;
}
};
void main(){
ABC obj1;
ABC obj2(20);
obj1.Print();
obj2.Print();
cout<<"- - -\n";
obj1.SetValue(2);
obj2.SomeFunction();
obj1.Print();
obj2.Print();
}

Department of Computer Science Page 4 of 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy