EEE117-Computer Programming Laboratory

2019-2020 Fall Semester

ANNOUNCEMENTS:

2016-2017 Fall Semester EEE-117 Lab Final Examination Sltns

  1. What do the following cout statements print? Each row of the table represents a line of code in the same program!

int i    =  0;

int x    =  5;

int* y           =  new int (3);

int** z         =  &y;

int A[5]          =      { 1, 2, 3, 4, 5 };

int Y[3][3]      =      { 1, 2, 3,

4, 5, 6,

7, 8, 9 };

 

Code Printed on cout
cout << *y; 3
cout << **z; 3
 cout << *&x; 5
cout << A[4]; 5
cout << *(A+2); 3
cout << *(A+*y); 4
cout << A[**z]; 4
cout << A[x]; Junk
cout << ++i; 1
cout << i++; 1
cout << i; 2
cout << (i=-1); -1
cout << Y[1][2]; 6

——————————————————-

{

int i             = 1;

double x      = 1.111;

cout << i << ”  ” << x << “\n”;

{

int x         = 2;

double i    = 2.222;

cout << i << ”  ” << x << “\n”;

}

}

          Printed on cout

1  1.111

2.222 2

**************************************************

  1. How many times will the program execute the code inside the loop? >> Answer: 4

 

int Sum = 0;

int Num = 1;

while (Sum < 10)

{

Sum = Sum + Num;

Num = Num + 1;

}

cout << “Sum = ” << Sum << “, ” << endl << “Num = ” << Num << “. ” << endl;

          Printed on cout

Sum = 10,

Num = 5.

  1. Create a 2-dimensional integer array with 10 rows and 5 columns named “Array” !

int Array[10][5]

 

  1. Please complete the missing parts of the codes below!

 

4.1. First code is a C++ function that returns the max value of  above array and its location within the array as reference parameters.

 

void FindMax(int Array[10][5], int &maxVal,

int &maxRow, int &maxCol)

{

maxVal = Array[0][0];

for( int i=0 ; i < 10 ; i++ )

{

for ( int j = 0 ; j < 5 ; j++ )

{

if (Array[i][j] > maxVal)

{

maxVal     = Array[ i ][ j ];

maxRow    = i;

maxCol     = j;

}

}

}

}

 

4.2. Second code is an anoher C++ function that is intended to count the number of negative values in an array (remember that 0 is not anegative value!)

 

int Process(int Array[ ], const int array_size)

{

int Count = 0;

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

{

if (Array[i] < 0)

Count++;

}

return Count;

}

 

2017-2018 Fall Semester EEE-117 Lab Final Examination Sltns

  1. (6pts) #include <iostream>

int main( )

{ if (1)

{

int x = 5;

} // x destroyed here

else

{

int x = 6;

} // x destroyed here

std::cout << x; // x isn’t defined here

return 0;

}

Answer

error

2. (8pts) int x = 5;

int y = x++;

Answer

X = 6, Y = 5

 

  1. (6pts) What are the error types?
  2. Syntax Error
  3. Logic Error
  4. Run-Time Error

 

  1. (15pts ) int x = 5, y = 5;

cout << x << ” ” << y << endl;

cout << ++x << ” ” << –y << endl;

cout << x << ” ” << y << endl;

cout << x++ << ” ” << y– << endl;

cout << x << ” ” << y << endl;

Answer

5 5

6 4

6 4

6 4

7 3

 

  1. (15pts ) #include <iostream>

int main( ){

for (char c = ‘a’; c <= ‘e’; ++c)

{

std::cout << c ;

for (int i = 0; i < 3; ++i)

std::cout << i;

std::cout << ‘\n’;

}

return 0;

}

 

Answer
a012
b012
c012
d012
e012
  1. (10 pts) #include <iostream>

void addOne (int& y) // y is a reference variable

{

y = y + 1;

} // y is destroyed here

int main(

)

{

int x = 5;

std::cout << “x = ” << x << ‘\n’;

addOne(x);

std::cout << “x = ” << x << ‘\n’;

return 0;

}

Answer

x=5

x=6

  1. (10pts) #include <iostream>

void foo(int *ptr)

{

*ptr = 6;
}

int main( )

{

int value = 5;

std::cout << “value = ” << value;

foo(&value);

std::cout << “value = ” << value;

return 0;

}

Answer

value = 5,value = 6

 

  1. (15pts) #include <iostream>

void setToNull(int *tempPtr)

{

tempPtr = 0;

}

int main( )

{

int five = 5;

int *ptr = &five;

std::cout << *ptr;

setToNull(ptr);

std::cout << *ptr;

return 0;

} Answer: 55

 

  1. (3pts) Create an integer array with 10 rows and 5 columns named “Array” !

 

int Array [10][5];

 

 

  1. int z = 1988;

Pointer = &z;

 

z = 1988

&z = address

Pointer = address

*Pointer = 1988

 

2017-2018 Fall Semester EEE-117 Lab Final Examination Sltns

 

1.intx=1,y=0, z=1;                      x  y  z

if (x>y && x>z) {y=x;z=x+1;}          2  0  3
else if(x+y>=z) {x++;z=x+1;}
else y=z+x;

 

2.intx=0,y=0, z=1;                      x  y  z

if(x) {x–;y=x+2;}              1  0 1
else x++;

 

3.intx=2,y=3, z=5;                      x  y  z

if(x+1==y) y=y+1;                        2  4  5
else x++;

 

4.Change the following C++ code to a for loop:
x=1;
while(x<10){
cout<<x<<“\t”;
++x;

}           ANSWER:for(x=1;x<10;x++){     cout<<x<<“\t”;  }

 

5.#include<iostream>
usingnamespacestd;

int main(){
int list [10] = {1,2,3,4,5,6,7,8,9,10};
int i;
for(i=0; i<5; i++)
{                    ANSWER:  10 9 8 7 6 5 4 3 2 1
int temp = list[i];
list [i] = list [9-i];
list [9-i] = temp;
}
for(i=0;i<10;i++)
cout<<list[i]<<“\t”;

return 0;
}

 

  1. #include<iostream>
    usingnamespacestd;
    int main( ){
    int list1[5]={1,3,5,7,9};
    int list2[5];
    int i;               ANSWER:  2 4 6 8 10
    for (i=0; i<5; i++)
    list2 [i] = list1[i]+1;
    for ( i=0; i<5; i++)
    cout << list2 [i] << “\t”;

return 0;
}

 

7.#include <iostream>     ANSWER: BONUS
using namespace std;
int max3(int x, int y, int z);
int main( ){
int x, y, z, max;
cout << “Enter x, y and z separated by space:”;
cin >> x >> y >> z;
max = max3 (x, y, z);
cout << “The max value is ” << max;
return 0;
}

int max3(intx,inty,int z){
if(x>y)
if(x>z) return x;
else return z;
else if(y>z) return y;
else return z;
}

 

8.What is the output of the following C++ program?
#include<iostream>
using namespace std;
int main( ){
int x=10,y=15;
int mul=x*y;
cout<<x<<“*”<<y<<“=”<<mul;
return 0;
}                       ANSWER:x * y = 150

 

 

 

  1. What would be the output from the following C++ program when run using 3 4 as input data?
    #include<iostream>
    using namespace std;
    int subtr (int x, int y);
    int main( ){
    int x, y, res;
    cout << “Enter x and y separated by space:”;
    cin >> x >> y;
    res = subtr (x, y);
    cout << “Result=” << res;
    return 0;
    }                       ANSWER:Result=-1
    int subtr ( int x, int y){
    return ( x – y );
    }

 

  1. True or False?

( T / _ )A pointervariable is thevariablethat can storetheaddress of a variable?
( _ / F )An addressstoredby a pointer can be specifiedbytheprogrammer.
( T / _ )Theaddressoperator(&) is usedtogettheaddress of a variablebyputting it in front of thevariable.
( T / _ )Theoperator * is usedtogetvaluestored at an addressthat is pointedby a pointer.
( _ / F )A pointervariablecan’tpointtootherpointervariables.
( T / _ ) A pointer can be usedtoaccesselements of an array.
( _ / F )In C++ language, a functioncan’treturn a pointer.
( _ / F ) A function can return more than one value.
( T / _ ) A function needs function definition to perform a specific task.
( T / _ ) If a function returns no value, the return type must be declared as void.
( T / _ ) A local variable declared in a function is not usable outside that function.
( T / _ ) A function can have more than one parameter. Values of the parameters are passed to the function when it is called.
( T / _ ) ( a <= b ) || ( b <= a)

 

11.Which of thefollowingstatementsinitializesthepointertotheaddress of x variable?       ANSWER:  b
a. int *ptr=x;    b.int *ptr=&x;     c. int&ptr=&x;      d. int *ptr=*x;

 

12.Givingthefollowingdefinitionstatementsshownbelow, whichare not validamong a, b, c, d, e and f?
int x;                                 ANSWER:d f
float f;
int *pi;
float *pf;
a. x=10;  b.f=10;  c.pi=&x;   d.pi=*x;  e.pf=&x  f.pf=&pi;

13.Findanyerrors in thefollowingprototypes and fix them:
a.int sum(intx,y);                    int y
b.int sum(intx,int y)                ;
c.int sum(intx,void y);            int y

 

14.What is the output of thefollowing C++ program segment?
int i=10;
int *p,**q,***r;
p=&i;
*p=15;
q=&p;
**q=20;
r=&q;
***r=++(*p);
cout<<i;        ANSWER:21

 

15.Givingthefollowingdeclarations, which of thefollowingstatementis not invalid?                      ANSWER:  e
int i;
int *pi;
double d;
double *pd;

a.i=&pi;     b.*pi=&i;      c.pd=&pi;      d.pd=i;    e.pi=&i;

 

 

  1. 16. Fill in the blanks!

#include<iostream>

usingnamespacestd;

int main( )

{

int data[5];

cout << “Enter elements: “;

for (int i = 0; i < 5; ++i)

cin >> ____[i];                       ANSWER:data

cout<< “You entered: “;

for (int i = 0; i < 5; ++i)

cout << endl << __(data + i);         ANSWER: *

return 0;

}

 

17.intdoubleplus(intx,inty)

{

cout<<x*2+y;

}

intplusdouble(intx,int y)

{

cout<<x+2*(y);

}                       ANSWER: 1213

int main() {

doubleplus(1+2, 3+4);

plusdouble(3+4, 1+2);

return 0;

}

 

18.intscore=95;

if(score<30)                    ANSWER:Work harder!

cout<<”Yougot an FF”<<endl;

cout<<”Workharder!”;

 

19.Assumethatthepurpose of the program is toaddupalltheoddnumbersbetween 0 and 100. Choosecoorectanswertofill in theblanks:            ANSWER:  d

 

int n, sum=0;

for(… ; … ; …){

sum += n;

}

  1. n=1;n+2;n<=100
  2. n=1;n<=100;n+2
  3. n=1;n<=100;n++
  4. n=1;n<=100;n+=2
  5. None of theabove!

 

20.int r=10;

if(++r>10)

cout<< “I’m not sure!”;

else                  ANSWER:I’m not sure!

cout<< “I’m sure!”;

 

21.Assumethat N is declared as int, and a valuestored in it. How wouldyoucheckwhether N is between 0 and 10 (bothinclusive)?            ANSWER:c

  1. if (N>0 && N<10) // N is in validrange
  2. if (N>=0 || N<=10) // N is in validrange
  3. if (N>=0&&N<=10) // N is in valid range
  4. if (N<0 || N>10) // N is in validrange
  5. if (N<0&&N>10) // N is in validrange

 

22.int k=0;

while (k<=10)

{k++;}                ANSWER:11

cout<< k;

 

23.Given:

voidchange(int&q)

{q=q+10;}

What is therightinvocation of abovefunctionfrom main()? Assumethat n=10; is declared in main() andthegoal is toincrement n by 10.            ANSWER:  a

  1. change(n);
  2. change(&n);
  3. change(&q);
  4. n=change(q);
  5. n=change();

 

24.intx=7;

int&y=x;

x++;                  ANSWER:8

cout<< y;

 

25.int x=6;

int y=7;

x += y;              ANSWER:  13 , 7

cout<< x <<” , “<< y;

 

  1. int n;

n=17+10/7;       ANSWER:  18

cout<< n;

 

27.What is the output?

ANSWER: 1471013

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

cout << i;

}

 

28.Considertheline of C++ code. What could be done tomake it morereadable?           ANSWER:  c

 

if(tree == ‘Evergreen’ &&tree_type == ‘Spruce’ &&

(tree_species== ‘torano’ || tree_species == ‘spinulosa’)) { }

 

  1. replace&&with ||
  2. changeto a switchstatement
  3. create a nestedifstatement
  4. use a NOT operatorwithinthestatement

 

 

——————————————————————————————————————–

LABORATORY CLASSES

 

1. Group (Wednesday 13 :15-15:00)

2. Group (Wednesday 15:15-17:00)

3. Group (Thursday 08:15-10:00)

4. Group (Thursday 10:15-12:00)

5. Group (Thursday 19:15-20:45)

 

Lab Groups can be seen from the link below

https://drive.google.com/open?id=18ca4Fg-hULcvzbOM_ktxDtPX8gGz7LqH

Any objections can be delivered via e-mail to the e-mail address given below!

Laboratory Assistant: Res. Asst. Adil H. CENGİZ (acengiz@cu.edu.tr)

Class: BOD-5, Ground Floor in the Environment Engineering Department.

Room: Research Assistants’ Room, 2nd Floor, EEE Department.

—————————————————————————————————————-

LABORATORY SCHEDULE

Week#1 (16-20/09/2019) Start of the course EEE117 (not Lab)

Week#2 (23-27/09/2019) Introduction to Lab, how to use program and its interface

Week#3 (30-04/9-10/2019) Introduction to C++ (LMCHAP01)

  • become familiar with the C++ environment used in the lab
  • understand the basics of program design and algorithm development
  • error types (syntax, run time & logic)
  • basics of an editor and compiler

Week#4 (07-11/10/2019) Introduction to Programming (LMCHAP02)

  • To introduce variables and named constants
  • To introduce data types (int, char, float, bool, string)
  • To introduce the assignment and “cout” statements
  • To demonstrate the use of arithmetic operators

Week#5 (14-18/10/2019) Expressions, Input, Output and Data Type Conversions (LMCHAP03)

Week#6 (21-25/10/2019) Conditional Statements (LMCHAP04)

  • relational operators
  • conditional statements
  • nested if statements
  • logical operators
  • switch statement

Week#7 (28-01/10-11/2019) Quiz & Repeat before Exam

Week#8 (04-08/11/2019) MIDTERM

Week#9 (11-15/11/2019) Looping Statements (LMCHAP05)

  • While
  • Do-while
  • For
  • Nested loops

Week#10 (18-22/11/2019) Functions (LMCHAP06.1)

  • Void Functions
  • Pass by value & pass by reference parameters

Week#11 (25-29/11/2019) Functions (LMCHAP06.2)

  • Scope, static variables, local variables & global variables
  • Functions that return a value

Week#12 (02-06/12/2019) Arrays (LMCHAP07)

Week#13 (09-13/12/2019) Pointers (LMCHAP09)

Week#14 (16-20/01/2019) Extra examples about Functions, Arrays & Pointers

Week#15 (23-27/01/2019) Repeat before Final Exam

Week#16 (30/12/2019) FINAL EXAM