Date: November 21th, 2007
Time: 13:10-15:00
Open book; turn off computer & mobile phone
#include <iostream> int main() { const int MAX = 100; int i = 1, sum = 0; loop: sum += i; // Add current value of i to sum if (i++ <= MAX) goto loop; // Go back to loop std::cout << std::endl << "sum = " << sum << std::endl << "i = " << i << std::endl; return 0; }
#include <iostream> using std::cout; using std::endl; int main() { int i, sum; for (i=10, sum=0; i>5; sum+= i--) cout << "i = " << i << endl; cout << "sum = " << sum << endl; }
#include <iostream> int main() { char c1 = 'A'; std::cout << static_cast<char>(c1 | 0x20) << std::endl; char c2 = 'g'; std::cout << static_cast<char>(c2 ^ 0x20) << std::endl; c1 ^= c2; c2 ^= c1; c1 ^= c2; std::cout << static_cast<char>(c1) << " , " << static_cast<char>(c2) << std::endl; (c2 >>= 6) <<= 1; c2 |= c2 << (1 << 2); std::cout << static_cast<char>(c2 ^ c1) << std::endl; }
Show the output of the following CLR console program. #include "stdafx.h" using namespace System; int main(array<System::String ^> ^args) { int x = 120; Console::WriteLine("The value of x outside the for loop is {0}", x); for(int i = 0 ; i<3 ; i++) { int x = i; Console::WriteLine("The value of x inside the for loop is {0}", x); } Console::WriteLine("The value of x outside the for loop is {0}", x); return 0; }
// Ex2_08.cpp // Demonstrating variable scope #include <iostream> using std::cout; using std::endl; int count1 = 100; namespace mySpace { int count1 = 200; } namespace mySpace { int count2 = count1 + 20; } int main() { int count1 = 10; cout << "local count1 = " << count1 << endl; cout << "mySpace count2 = " << mySpace::count2 << endl; cout << "Global count1 = " << ::count1 << endl; }
// Ex3_06.cpp // Multiple case actions #include <iostream> using std::cin; using std::cout; using std::endl; int main() { char letter = 0; short vowel = 0, consonant = 0; do { cin >> letter; switch (letter*(letter >= 'a' & letter <= 'z')) { case 'a': case 'e': case 'i': case 'o': case 'u': vowel++; break; case 0: break; default: consonant++; } } while (letter != '.'); cout << "The string has " << vowel << " vowel" << (vowel>1 ? "s" : "") << " and " << consonant << " consonant" << (consonant>1 ? "s." : ".") << endl; }
#include <iostream> int main() { for (int i = 1; i < 6; i++) { if (i==3) continue; std::cout << i << std::endl; } }
// Exercising the comma operator #include <iostream> int main() { short num1, num2, num3, num4; num4 = ++(num1 = 10, num2 = 20, num3 = 30); std::cout << num4 << std::endl; return 0; }
#include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; using std::setw; int main() { int N = 0; int i, j; do { cout << "N = ? "; cin >> N; for (i=1; i<=N; i++) { int w = (1) ; for (j=N-i; j>=0; j--) cout << "*"; cout << setw(w); for (j=N-i; j>=0; j--) cout << "*"; cout << endl; } for (i= (2) ; i>0; i--) { int w = (3) ; for (j=N-i; j>=0; j--) cout << "*"; cout << setw(w); for (j=N-i; j>=0; j--) cout << "*"; cout << endl; } } while (N > 0); return 0; }If we want to obatin the following results, what expressions should be filled into the three blanks?
N = ? 5 ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** N = ? 6 ****** ****** ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ****** ******