Part I: Dry-Run The Following Codes, Assuming No Errors: (30, 5 Each)
Part I: Dry-Run The Following Codes, Assuming No Errors: (30, 5 Each)
B.
struct Box{ int h, w, l; }; Output
int main(){
Box b;
b.h = 3; b.w = 5; b.l = 2;
int w=10, n=20;
cout ≪ b.l ≪ ' ' ≪ b.w ≪ ' ' ≪ n;
cout ≪ ' ' ≪ w ≪ ' ' ≪ b.w * b.l;
...
C.
Output
int a[4][3]={{1,2,4},{5,3,7},{0,9,6},{8,9,0}};
cout ≪ a[3][2] ≪ ' ' ≪ a[0][1] ≪ '\n';
cout ≪ a[2][1] ≪ ' ' ≪ a[1][2] ≪ '\n';
cout ≪ *a[1];
D.
void p(int x[], int SIZE){ Output
for (int i=0;i<SIZE;i++)
cout ≪ x[i] ≪ ' ';
}
int main(){
int a[3][3]={{1,2,4},{5,3,7},{0,9,6}};
p(a[1],2);
p(a[2],2);
p(&a[0][1],1);
}
E.
int a[3][3] = {{1,2,4},{5,3,7},{0,9,6}}; Output
int *p[3];
p[1] = a[2]; p[2] = a[0]; p[0] = &a[1][1];
cout ≪ p[2][2] ≪ ' ' ≪ p[0][1] ≪ '\n';
cout ≪ p[2][3];
F.
ifstream in("N.txt"); Output N.txt
for (i=1;i<4;i++){ 23 31 25 57 81
in >> n1 >> n2 >> n3; 90 27 70 82 98
cout ≪ n2 ≪ ' '; 64 11 91 22 88
} 66 77 33 44 55
in >> n1 >> n2 >> n3;
in >> n1 >> n2 >> n3;
cout ≪ '\n' ≪ n1 ≪ ' ' ≪ n3;
Part II: Find syntax & logical error in the code [20, 5 each]
Write S for Syntax & L for logical Errors & give reason. If a variable is not declared, it is neither syntax nor
logical error for this part. Each section may have multiple errors, specify all of them.
A.
double a[5] = {-2.50, 3, 2.6, 3, 5.6};
double *p = a[3]; int count=0;
for (m=0;m<5;m++)
cout << a[m]+a[m+1] <<' ';
for (i=0;i<5;i++)
for (j=i;j<5;i++)
if (a[i]= =a[j]) count++;//to find count of repeating elements
B.
struct Point{ int x, y; };
int main(){
Point p;
p.x = 3; p.y = 4;
cout ≪ "Point:" ≪ p ≪ '\n';
cin >> p.x >> ' ' >> p.y;
cout ≪ "Point:" ≪ p ≪ '\n';
float avg= (p.x + p.y) / 2;
cout<<"Average:"<<avg<<'\n';
C.
int a[4][3]={{1,2,4},{5,3,7},{0,9,6},{8,9,0}};
cout ≪ a[0] ≪ ' ' ≪ a[1] ≪ ' ' ≪ a[2] ≪ ' ' ≪ a[3] ≪ '\n';
cout ≪ a[2][1] ≪ ' ' ≪ a[1][2] ≪ '\n';
cout ≪ a[0][4] ≪ '\n';
cout ≪ *(a[0]) ≪ '\n';
D.
void p(int x[], int SIZE){
for (int i=0;i<SIZE;i++)
cout ≪ x[i] ≪ ' ';
}
int main(){
int a[3][3]={{1,2,4},{5,3,7},{0,9,6}};
p(a[1],3);
p(a,9);
p(&a[2][0],3);
}
Question # 02 [110, 10 each]
B. Write code in main to display printable ASCII chart in the following form using loops. The ASCII code of
space is 32 therefore there is nothing visible after 32. Print 8 codes & characters in each line.
32 33 ! 34 " 35 # 36 $ 37 % 38 & 39 '
40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 /
...
120 x 121 y 122 z 123 { 124 | 125 } 126 ~
C. Write function to print the sum of elements of both diagonal of a square matrix (2DArray) of any order.
D. Write down the contents (including new lines, tabs, etc) of the file generated by the following code.
#include <iostream> ofstream s;
#include <fstream> s.open("file.txt");
using namespace std;
int main() s << "PUCIT, Punjab University" << endl;
{ s << "Old Campus, Lahore" << endl;
int a = 837; s << endl;
int b = 26; s << a << '\t' << b << *p << d << endl;
char c = 32; // ascii of space character
float d = 0.025; return 0;
char * p = &c; }
E. A job requires Bachelor as minimum qualification. However if a candidate has more qualification,
candidate can achieve more marks depending on percentage of marks obtained. You have to write a
structure to store data of candidates applied for the job. Define structure to store: (See example of data
on next page in section F.)
number of candidates
dynamic arrays to store first name & last name of candidates in separately
dynamic array to store number of degrees earned by each candidate
2D dynamic array to store marks obtained by candidates in each degree [each candidate may
have 3 to 5 degrees, in order of Matric, Inter, Bachelor, Masters, MPhil]
F. Declare a variable of structure defined in part E & store given data appropriately:
Number of candidates: 2
First Name Last Name Number of Degrees Matric Inter Bachelor Master MPhil
Akmal Raza 3 880 745 530
Basit Ali 4 875 760 545 870
G. Open a text file job.txt and store data of job stored in structure defined in part E using appropriate loops.
//Assume job is a variable of your defined structure, therefore just open file & write code
H. Consider data arrays to stores only positive numbers. Arrays have length more than values. Sentinel value
-1 is stored on adjacent next cell of last value. The indexes after sentinel values are garbage. Your task is
to write join function to join (by appending) numbers in array named first to array named second,
assuming second array have necessary and sufficient space to store elements of both arrays.
void join(int *first, int *second)
I. In the scenario of previous part H, there is a modification that second array has size insufficient to store
data of both arrays. You have to write another function by managing size of second array appropriately.
It is required to reuse code by calling join function of part H inside this function.
void joinByResize(int *first, int *second)
J. Binary file "cities.bin" has data of N cities in binary format. First four bytes is an integer value N that is
number of cities. Rest of the file has data/information about N individual cities. You think it in text file
format as described below but the headings & line below heading is just for visualization otherwise they
are not part of file, similarly numbers shown to give idea about type, and otherwise they are in binary
format. Here data is example for you otherwise file may contain any number of records.
You have to read all the data and display information of city having area more than 1000. Hint: count
number of '=' signs under City heading to get maximum length of city.
2
City Area Population Longitude Latitude
==================== ====== ========== ========= =========
Lahore 1772 6318745 74.329376 31.582045
Rahimyar Khan 833 832645 69.457104 32.733351
K. Consider the scenario given in previous part J; this time challenge is to update population of a city. You
have to write code to get city name and its new population from user and using minimum file reading
and writing, search city name in it and if found update population with new value.