Professional Documents
Culture Documents
1 Review
1 Review
Hassan Khosravi
CPSC 259
Page 1
Learning Goal
Briefly review some key programming concepts from
CPSC 259
Page 2
CPSC 259
Page 3
(c)
(b)
CPSC 259
var1 = var2;
var2 = var1;
Page 4
(c)
(b)
CPSC 259
var1 = var2;
var2 = var1;
Page 5
(a) 65.0
(b) 65.5
(c) 65
(d) 66
CPSC 259
Page 6
(a) 65.0
(b) 65.5
(c) 65
(d) 66
CPSC 259
Page 7
(a) 1
(b) 0
(c) 0.25
(d) 4
CPSC 259
Page 8
(a) 1
(b) 0.0
(c) 0.25
(d) 4
CPSC 259
Page 9
t && !f
(b)
!t && f
(c)
t || f
(d)
!(t && f)
(e)
t || ( !f && !t )
CPSC 259
Page 10
(a)
t && !f
(b)
!t && f
(c)
t || f
(d)
!(t && f)
(e)
t || ( !f && !t )
CPSC 259
Page 11
CPSC 259
a = 0 , b= 6 , r = 2
a = 0 , b= 6 , r = 1
a = -5 , b= 6 , r = undefined
a = -5 , b= 6 , r = 2
None of the above
int r;!
int a = -5;!
int b = 6;!
if( a < 0 || b > 0 )!
r = 1;!
else!
r = 2;!
a = 0;
Page 12
CPSC 259
a = 0 , b= 6 , r = 2
a = 0 , b= 6 , r = 1
a = -5 , b= 6 , r = undefined
a = -5 , b= 6 , r = 2
None of the above
int r;!
int a = -5;!
int b = 6;!
if( a < 0 || b > 0 )!
r = 1;!
else!
r = 2;!
a = 0;
Page 13
Question looping
Consider the following code segment: What values do i and
j have after this code segment has executed?
(a)
(b)
(c)
(d)
(e)
CPSC 259
i=4,j=5
i=5,j=5
i=4,j=6
i=5,j=6
None of the above
int i = 1;!
int j = 0;!
!
while( i < 5 && j < 4 ){!
j = j + i;!
i++;!
}!
printf( "i = %d, j = %d", i,j);!
Page 14
statement
1
2
T
3
3
int i = 1;!
int j = 0;!
!
while( i < 5 && j < 4 ){!
j = j + i;!
i++;!
}!
printf( "i = %d, j = %d", i,j);!
T
6
4
(c)
CPSC 259
i=4,j=6
Page 15
If you are not registered in a lab section, attend one of the lab
sections next week and tell the corresponding TAs that you are not
registered.
They will try their best to accommodate you
You can pretty much use any version of Visual studio that you like.
Recommended exercises from the second edition of the textbook
CPSC 259
Page 16
Question looping
How many times is the printf statement executed in
the following C code?
int x = 1;
while ( x < 15/4 ) {
printf ("x = %d\n", x);
x++;
}
A. never
B. once
C. twice
D. three times
E. four or more times (or infinite loop)
CPSC 259
Page 17
output
1
1
2
2
3
B. once
C. twice
D. three times
E. four or more times (or infinite loop)
CPSC 259
Page 18
count1=3,count2=8
count1=3,count2=10
count1=2,count2=8
count1=2,count2=9
None of the above
CPSC 259
int i;!
int j;!
!
int count1 = 0;!
int count2 = 0;!
!
for( i = 0; i < 3; i++ ) {!
count1++;!
for( j = 1; j < 4; j++ )!
count2++;!
}
Page 19
CPSC 259
int i;!
int j;!
!
int count1 = 0;!
int count2 = 0;!
!
for( i = 0; i < 3; i++ ) {!
count1++;!
for( j = 1; j < 4; j++ )!
count2++;!
}
Page 20
main.
Extremely difficult to debug.
Extremely difficult to re-use any part of the code.
Modularity: A design principle used to manage the complexity of
CPSC 259
Page 21
Functions review
Information to function
can come from
parameters or an input
stream
Input stream
data
Parameters
Function
Output stream
data
Return
value
Information from
function can come
through a return
value or an output
stream
CPSC 259
Page 22
CPSC 259
Page 23
return statement.
3. Pass back the answer (if any) via the return statement.
4. Destroy all local variables in the function (i.e., release/free
memory).
(Well start fresh if this function is ever called again.)
5. Return to the caller.
6. Finish the rest of the calling statement (after replacing the
Page 24
CPSC 259
Page 25
Question function
Consider the following code segment: Fill in the blanks
below to show what is output to the screen when this
program runs?
void myFunc( int a, int b ){!
a = a + 4;!
b = b - 4;!
printf( "In myFunc a = %d b = %d\n", a, b );!
}!
!
int main(){!
int a = 5;!
int b = 7;!
myFunc( a, b );!
printf( "In main a = %d
return 0;!
}!
!
CPSC 259
b = %d\n", a, b );!
Page 26
/* prototype */!
/* function call */
Page 27
b = %d\n", a, b );!
Page 28
Review: Arrays
An array is a collection of similar data elements. These data elements
have the same data type.
CPSC 259
2nd
element
3rd
element
marks[1] marks[2]
4th
element
5th
element
6th
element
marks[3]
7th
element
8th
element
9th
element
10th
element
marks[9]
Page 29
Elements of an array
Accessing elements of an array
You can use a loop
int i, marks[10];!
for(i=0;i<10;i++)!
marks[i] = -1;
Inputting Values
int i, marks[10];!
for(i=0;i<10;i++)!
scanf(%d,&marks[i]);
CPSC 259
Assigning Values
for(i=0;i<10;i++)!
arr2[i] = arr1[i];
Page 30
Question 1D array
Consider the following code segment. What is the value of
sum after this code segment has executed?
int data[] = { 2, 4, 8, 16, 32, 64 };!
int sum = 0;!
int index = 1;!
!
while( index < 4 ){!
sum += data[ index ];!
index++;!
}
(a)
(b)
(c)
(d)
(e)
CPSC 259
sum = 30
sum = 60
sum = 32
sum = 14
None of the above
Page 31
Answer: 28
CPSC 259
(a)
(b)
(c)
(d)
(e)
APSC 160 Review
sum = 30
sum = 60
sum = 32
sum = 14
None of the above
Page 32
Question 1D array
Page 33
Question 1D array
int doSomething( int data[], int numEls, int someVal ){!
int index, found = -1;!
for( index = 0; index < numEls; index++ )!
if( data[ index ] == someVal )!
found = index;!
return found;!
}
data
numEls someVal
found
index
data[index]
12,12,89
3
12
-1
0
0
12
1
12
89
(c)
If
someVal
is
contained
in
the
rst
numEls
entries
of
the
array
data,
it
returns
the
index
of
the
last
slot
at
which
someVal
is
found,
otherwise
it
returns
-1.
CPSC 259
Page 34
Answer: __ __ __
Page 35
by-reference).
double getMaximum( double data[], int size );/* prototype */!
answer = getMaximum( myArray, length ); /* function call */
Page 36
Answer: 0 0 0
Page 37
Page 38
min = x[0];!
i = 1;!
while (i < SIZE)!
{!
if (x[i] < min)!
min = x[i];!
i++;!
}
APSC 160 Review
Page 39
int marks[3][4];
First Dimension
Second Dimension
array.
(0,0)
CPSC 259
(0, 1)
(0,2)
(0,3)
(1,0)
(1,1)
(1,2)
(1,3)
(2,0)
(2,1)
(2,2)
(2,3)
Page 40
Question algorithm
Consider the following pseudocode :
read a number
while less then 100 integers read & the last number read is not negative
do
output the last number read
read another number
end of the while loop
Suppose this program was modified to output the average of all the
positive integers and the number of positive integers that exceeded the
average. The simplest change to the program would:
(a) Not require an array or an additional loop.
(b) Require an array but no additional loop.
(c) Not require an array but am additional loop would be required.
(d) Require an array and at least one additional loop.
(e) None of the above
CPSC 259
Page 41
Page 42
Question algorithm
Consider the following pseudocode :
read a number
while less then 100 integers read & the last number read is not negative
do
output the last number read
read another number
end of the while loop
Suppose the program given in the above pseudo code was instead
modified to output the number of positive integers entered by the
user, and their average. The simplest change to the program would:
(a) Not require an array or an additional loop.
(b) Require an array but no additional loop.
(c) Not require an array but am additional loop would be required.
(d) Require an array and at least one additional loop.
(e) None of the above
CPSC 259
Page 43
CPSC 259
Page 44
arr2 = arr1;
(b)int arr2[ SIZE ]
= arr1;
= { arr1 };
Page 45
arr2 = arr1;
(b)int arr2[ SIZE ]
= arr1;
= { arr1 };
Page 46
Question Searching
34
89
18
21
-1
Page 47
Question Searching
34
89
18
21
-1
Page 48
CPSC 259
Page 49
N
2N
N * (N +1)
1
1
1
(1+ 2 +...N )
N +1
2
*1+ * 2 +... * N =
=
=
N
N
N
N
N
2
2N * (2N +1)
1
1
1
(1+ 2 +...2N )
2N +1
2
*1+
* 2 +...
* 2N =
=
=
N +1
2N
2N
2N
2N
2N
2
Page 50