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

Hi

This document is an assignment for the Programming Fundamentals course at the University of Management and Technology, Lahore, due on April 30, 2024. It includes various programming exercises, error identification tasks, and code output predictions, focusing on understanding expressions and control structures in programming. The assignment emphasizes handwritten submissions and may include quizzes or viva assessments.

Uploaded by

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

Hi

This document is an assignment for the Programming Fundamentals course at the University of Management and Technology, Lahore, due on April 30, 2024. It includes various programming exercises, error identification tasks, and code output predictions, focusing on understanding expressions and control structures in programming. The assignment emphasizes handwritten submissions and may include quizzes or viva assessments.

Uploaded by

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

University of Management and Technology, Lahore

Campus

Assignment # 02 – Fall 2023


Course Name: Programming Fundamental Course Code: CC1021 Credit Hours: 3(2,1)
Course Instructor/s: Mr. Muhammad Tahir Sohail Program Name BS Computer Science
Semester/ Section: BSCS 1st, V3 Maximum Marks: 30
Submission Moderator
Tuesday, April 30, 2024
Deadline: Signature
Instructions:
• Understanding the problems is a part of the assignments.
• Submit the handwritten document to me in the class.
o Print the question paper for this assignment.
o Then solve the Questions by hand, in the given space on printed Pages.
• The assignment assessment will be based on your effort.
• I can take either a quiz or in-class viva for this assignment by calling anyone from the class.

Questions: CLO-2, CLO-3


Evaluate the given expressions:
Sample Output

int n = 7 – 8.0 + 8 % 9 * 5.0 / 4.0; n=9

int n = 4 – 3 + 23 % 8 / 4 * 4; n=5

Page 1 of 21
(8 - 3 / 4 >= 11 * (5 – 2) % 3) True

True != !true True

true || false && false True

double n= 2.5 + 8 / 5 3.5

int x = 2 % 2 + 2 * 2 – 2 / 2 3

Page 2 of 21
float x= float(17 / 4) + int(2.5 – 2) 4

double b = 3.0 / 2 + 4 % 3 – 1.5; 12

int d = (5 + 3) * 2 – 7 / 3; 14

boolean c = (8 > 5) && !(2 == 2) || (3 * 4 <= 12); True

boolean e = (4.0 / 2 != 2) || (3.5 > 4.0 && 2 <= 1); true

Page 3 of 21
int f = 2 * (4 / 2) + 3 % 2; 5

double g = 1.5 * 3 + 2 / 2.0; 5.0

boolean h = !(true && false) || (true && false) && true; True

int i = 10 / 2 + 3 * (8 % 3) – 1; 12

double j = 2.0 + 3 / 2 + 4 * 1.5 – 2.5; 7.0

Page 4 of 21
Sample Output
int x = 7, y = 14, z = 3; True
x >= 5 && y != 12

int p = 15, q = 10, r = 5; True


( p < q || q ) = = ( r && r > 3 )

int m = 4, n = 8, o = 2; False
!(m + n > o * 3) && n % o == 0

int d = 6, e = 3, f = 9; False
d * e - f != 12 || d / e == f % 2

int g = 2, h = 7, i = 5; True
g + h > i && !(g == h) || g * h == i

Page 5 of 21
int a = 8, b = 12, c = 4; True
!(a < b) && c != 2 || a - b > 0

int j = 10, k = 6, l = 2; False


(j / k == l && j % k != 0) || !(j > k)

int u = 5, v = 3, w = 1; True
(u + v) * w == u * w + v * w && !(u - v > w)

int s = 7, t = 4, x = 2; True
s > t && t < x || s == t * x

int aa = 6, bb = 3, cc = 2; False
!(aa % bb == cc && bb > cc) && aa / bb == cc

Page 6 of 21
Find errors in the following statements and correct them.

char c = "ABC"; Character variable can store single character.

#define msg "Hello World"; Define directive is not terminated by semicolon

char no[] = Hello World; String values are written in double quotes

Comma cannot be used between data type and variable


long, population =15000;
name.

define char ch 'B' The sign # is used with define. It does not use data types.

float a b c; Variables are separated by commas in declaration.

const double = 100; Data types cannot be used as variables.

int = 35; Value cannot be assigned to data types.

Statement must be terminated by semicolon instead of dot.


float average = 90.15.

z=4.0 w*y;

y = yz

a = 6b3;

c = 3(a + b);

z = 7w + xy;

(x + y)z;

(x + 3y) + (2x – y)

Page 7 of 21
For the given code write down the output for given values.
Sample Code Output

int m=5,n=10,q=20;
if(q/n*m) William Gates Carlos Slim Helu
cout<<"William Gates";
else
cout<<" Warren Buffet";
cout<<" Carlos Slim Helu";
int x;
if(x == 3) When x = -5
{
cout<<"x = "<< x<<endl; x = -5
cout<<"The if condition is fulfilled!\n"; The if condition is not fulfilled!
}
else
{ When x = 3
cout<<"x = "<< x<<endl;
cout<<"The if condition is not fulfilled!\n"; x=3
} The if condition is fulfilled
int main() When x = 3
{
int x;
if (x < 1) When x = -2
cout<<"hello"; Hello no
if (x == 5)
cout<<"hi"; When x = 5
else hi
cout<<"no";
}
int numb1, numb2; numb1=5 , numb2=3
if(numb1==numb2) 5>3
cout<<"Result: “<<numb1<<”=”<<numb2;
else numb1=2 , numb2=3
if(numb1>numb2) 3>2
cout<<"Result: "<<numb1<<”>”<<numb2; numb1 = 5, numb2=5
else 5=5
cout<<"Result: "<<numb2<<”>”<<numb1;
int k; When k = 20
if(k < 90) Grade F
if(k < 80)
if(k < 70) When k = 75
if(k < 60) Grade C
cout<<"Grade F.\n";
else When k = 62
cout<<"Grade D.\n"; Grade D
else
cout<<"Grade C.\n"; When k = 95
else Grade A
cout<<"Grade B.\n";
else When k = 85
cout<<"Grade A.\n"; Grade B

Page 8 of 21
When K = 63
int k;
Grade D
if(k > 69)
{ When K = 90
if(k > 89)
cout<<"Grade A.\n"; Grade A
else
{ When K = 75
if(k > 79)
cout<<"Grade B.\n"; Grade C
else
cout<<"Grade C.\n"; When K = 59
}
} Grade F

else When K = 83
{
if(k > 59) Grade B
cout<<"Grade D.\n";
else
cout<<"Grade F.\n";
}
When K = 45
int k, a=0, b=0, c=0, d=0, f=0; A's = 0 B's = 0 C's = 0 D's = 0 F's = 1

if(k <= 59) When K = 68


f = f + 1;
else A's = 0 B's = 0 C's = 0 D's = 1 F's = 0
{
if(k <= 89) When K = 75
{
if(k <= 69) A's = 0 B's = 0 C's = 1 D's = 0 F's = 0
d = d + 1;
else When K = 88
{ if(k <= 79)
c = c + 1; A's = 0 B's = 1 C's = 0 D's = 0 F's = 0
else
b = b + 1;
} When K = 95
}
else A's = 1 B's = 0 C's = 0 D's = 0 F's = 0
a = a + 1;
}

cout<<"A\'s = "<< a;
cout<<"B\'s = " <<b;
cout<<"C\'s = " <<c;
cout<<"D\'s = " <<d;
cout<<"F\'s = “<< f;

Page 9 of 21
degreesOutside= 80, numberOfClouds=10
int degreesOutside;
int numberOfClouds; no output
if (degreesOutside > 70 && numberOfClouds < 5) degreesOutside= 80, numberOfClouds= -1
{
cout<<"Wear sun screen!"; Wear sun screen!
}

Output
When myAge = 95
int myAge;
You are not quite in your
if ((myAge >= 0 && myAge < 3) || myAge > 90 && myAge<=100) peak.
cout<<"You are not quite in your peak."; When myAge = 0

You are not quite in your


peak.
When myAge = 55

No output

int year, train; year = 13, train=5 ,curr_post=’B’


char curr_post; Become Assistant Manager first.
Need more training.
if(year >= 10) Need more experience.
if(train >= 5)
if(curr_post == 'A' || curr_post =='a') year = 10, train=2 ,curr_post=’a’
cout<<"Qualified For Manager!\n";
else Need more training.
{ Need more experience.
cout<<"Become Assistant Manager
first.\n";
cout<<"Need more training.\n";
cout<<"Need more experience.\n"; year = 5, train=12 ,curr_post=’a’
}
else Need more experience.
{
cout<<"Need more training.\n";
cout<<"Need more experience.\n";
}
else
cout<<"Need more experience.\n";

Page 10 of 21
Page 11 of 21
int a = 5; b =
int b = ++a + 2;

int x = 3;
int y = x++ * 2 + --x;

y=

Page 12 of 21
int m = 7;
int n = m-- + ++m * 2 – 3;

n=

int p = 4;
int q = ++p * 2 – p-- + p++;

q = 10

int r = 6;
int s = r++ + --r * 3 / 2;
s = 14

int u = 8;
int v = u-- + ++u – u++ + --u;

v = 18

int w = 10;
int x = w-- + w++ + --w + w--;

x = 27

int a = 3;
int b = a++ + a-- – ++a + --a;

b=4

Page 13 of 21
int c = 5;
int d = ++c * 2 – c++ + --c * 3;

d = 19

int e = 6;
int f = e++ + ++e * e-- / --e;

f = 22

int a = 5;
int b = a++ * --a + a-- – ++a;

b = 24

int x = 4;
int y = ++x * 2 + x-- * 3 – --x;

y = 17

int e = 9;
int f = e++ + --e * 2 + ++e – e-- * 3;

f = 13

Page 14 of 21
int a = 3;
int b = a-- + ++a * 2 – a++ + --a;

b=8

int a = 3, b = 5, c = 7;
int result = ++a * (b-- + c++) – (--b + ++c);

result = 26

int x = 2, y = 4, z = 6;
int outcome = x++ + (--y * z--) – (++x * y);

outcome = -15

int m = 4, n = 6, o = 8;
int answer = ++m * (n++ + --o) – (m-- + ++n);

answer = 22

int p = 7, q = 5, r = 3;
int result = p-- * (q++ – ++r) + (--q + r--);

result = 24

Page 15 of 21
int s = 2, t = 3, u = 4;
int value = s++ * (t-- + --u) + (++t * u--);

value = 18

int v = 6, w = 3, x = 2;
int result = v++ * (++w + x--) – (--v * w);

result = 24

int a = 5, b = 2, c = 8;
int output = ++a * (b-- + --c) + (a-- + ++b * c--);

output = 68

int d = 4, e = 3, f = 7;
int value = --d * (e++ + f--) – (++e * --f);

value = 14

int j = 8, k = 6, l = 3;
int outcome = j++ * (--k + l++) – (j-- + ++k * --l);

outcome = 29

Page 16 of 21
for (int i = 1; i <= 10; i += 2)
{
cout << i << " ";
}
int n;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
cout << i << " ";
}
int i, p=1;
for(i=1; i<6; i+=1)
{
P*=2;
}
Cout<<”\npis=”<<p;

Page 17 of 21
for (float i = 0; i < 1; i += 0.1) {
cout << i << " ";
}

int x=1;
while(x != 12)
{
Cout<<x<<”\n”;
X+=2;
}
int x=1;
while (x<9)
{
X++;
If(x%2 == 1)
cout<<x<<”+”;
}

Page 18 of 21
for (int i = 0; i < 5; i++)
{
cout << i << " ";
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++) {
cout << "i: " << i << ", j: " << j <<
endl;
}
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0) {
cout << i << " ";
}
}

int count = 1;
For( ; ; count<5)
if(count<5)
Page 19 of 21
cout<<count;
else
break;

for (int i = 1; i <= 3; i++) {


for (int j = 1; j <= 3; j++) {
if (i == j) {
cout << "X ";
} else {
cout << "O ";
}
}
cout << endl;
}
int n = 0;
for(int m = 0 ; m < 10; m++)
{
n+ = m++;
}
cout << n << endl;
for (int a = 0 ; a < 3; a++)
{
cout << a<< "”;
for (int b = 0 ; b < 2; b++)
cout << b << "” ;
}
for (int i = 0; i < 10; i++)
{
if (i == 3) {
continue;
}
if (i == 7) {
break;
}
cout << i << " ";
}

Page 20 of 21
sum = 0;
outerCount = 1;
while (outerCount <= 5)
{
innerCount = 1;
while (innerCount < =outerCount)
{
sum = sum + innerCount;
innerCount++ ;
}
outerCount++;
}
int i, j, limit = 6;
for ( i = limit; i > 0; i-- )
{
for (j = 1; j<=i ; j++)
{
if (i == 1 || i = = limit || j = =1 || j = = i)
cout<<i;
else
cout<< “ “;
}
cout << endl;
}

a. How many times will the following loop be executed?

b. Using the above code, what will the value of x be after execution?

c. Using the above code, what will the value of a be after execution?

Page 21 of 21

You might also like