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

Initializing Multidimensional Arrays (P.227)

1. The code for initializing a multidimensional array is correct. It will output 3, as data[0][2] is initialized to 3. 2. The code draws 4 line segments to form a square, with corners at (100,100), (100,300), (300,300), (300,100). Each iteration of the for loop draws 1 line segment and calculates the midpoint of the previous and next points, updating the point array. 3. The code to swap variables using pass by reference is correct. It will output b a, as it swaps the values of a and b.

Uploaded by

Weng Yan
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)
56 views4 pages

Initializing Multidimensional Arrays (P.227)

1. The code for initializing a multidimensional array is correct. It will output 3, as data[0][2] is initialized to 3. 2. The code draws 4 line segments to form a square, with corners at (100,100), (100,300), (300,300), (300,100). Each iteration of the for loop draws 1 line segment and calculates the midpoint of the previous and next points, updating the point array. 3. The code to swap variables using pass by reference is correct. It will output b a, as it swaps the values of a and b.

Uploaded by

Weng Yan
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

102 3

1. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Initializing Multidimensional Arrays (P.227)
#include <iostream>
int main()
{
unsigned short data[2][4] = { { 1, 2, 3
std::cout << data[0][2] << std::endl;
return 0;
}

}, {5, 6

} };

2. (10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after
you run the program? Please specify the coordinates of endpoints of each segment, or the coordinates of
centers and boundaries.
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CPoint FourPoints[4] = { CPoint(100,100), CPoint(100,300),
CPoint(300,300), CPoint(300,100) };
CPoint TempPoints[4];
int i, j;
for (i=0; i<3; i++)
{
pDC->MoveTo(FourPoints[0]);
for (j=1; j<=4; j++)
pDC->LineTo(FourPoints[j % 4]);
for (j=0; j<4; j++)
TempPoints[j] = CPoint( (FourPoints[j].x + FourPoints[(j+1) %
4].x)/2,
(FourPoints[j].y + FourPoints[(j+1) %
4].y)/2 );
for (j=0; j<4; j++)
FourPoints[j] = TempPoints[j];
}
}

3. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Pass-by-Reference (P.268)
#include <iostream>
void swap(int &i, int j)
{
int temp;
temp = i;
i = j;
j = temp;
return;
}

int main()
{
int a = 6;
int b = 3;
std::cout << a << b << std::endl;
swap(a, b);
std::cout << a << b << std::endl;
return 0;
}
4. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Recursive Functions (P.285)
#include <iostream>
void f(int i)
{
if (i >= 2)
f( i / 2 );
std::cout << i % 2;
return;
}
int main()
{
f(51);
std::cout << std::endl;
return 0;
}
5. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Class Destructors (P.435)
#include <iostream>
using std::cout;
using std::endl;
class CText
{
public:
char* ptext;
CText(const char* s)
{
ptext = new char[strlen(s)+1];
strcpy(ptext, s);
}
~CText()
{
cout << "Destructor called to release " << strlen(ptext)+1
<< " bytes memory of " << ptext << endl;
delete [] ptext;
}
};
int main()
{
CText c("Coconut"), m("Mango");
return 0;
}

6. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Overloading the Assignment Operator (P.454)
#include <iostream>
using std::cout;
using std::endl;
class CData
{
public:
int* pdata;
CData(int v = 0)
{ pdata = new int(v); }
CData operator=(CData d)
{ pdata = d.pdata;
return CData( *d.pdata );
}
void Print()
{ cout << *pdata << endl; }
};
int main()
{
CData a(6), b(3);
CData c;
c = a;
a = b;
a.Print();
b.Print();
c.Print();
return 0;
}
7. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Recursive Function Calls (P.285)
#include <iostream>
using std::cout;
void octal(int n)
{
if (n >= 8 )
octal(n / 8);
cout << n;
}
int main()
{
octal(63);
return 0;
}
8. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// String Objects (P.510)
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;
int main()
{
int a = 6;
string b("B");
string c(a, 'b');
string d( c + b );
cout << c << endl;
cout << d << endl;
return 0;
}
9. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it
is incorrect, point out the mistake(s).
// Searching Strings (P.523)
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main()
{
string a("NNNU");
string b("N");
cout << a.find(b) << a.find(b, 2) << endl;
return 0;
}
10. (10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// The Size of a Vector (P.655)
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> a;
int b = 6;
int c = 3;
cout << a.size() << endl;
a.push_back(b);
a.push_back(c);
cout << a.size() << endl;
return 0;
}

You might also like