Exercises

So that you can test your understanding of this material, here are some exercises.

1.If the program in Figure 4.19 is run, what will be displayed?
Figure 4.19. Exercise 1 (code\morbas00.cpp)
#include <iostream>
#include "Vec.h"
using namespace std;

int main()
{
   Vec<short> x(5);
   short Result;
   short i;

   for (i = 0; i < 5; i ++)
      {
      x[i] = 2 * i;
      }

   for (i = 0; i < 5; i ++)
      {
      Result = Result + x[i];
      }

   cout << Result << endl;

   return 0;
}
2.If the program in Figure 4.20 is run, what will be displayed?
Figure 4.20. Exercise 2 (code\morbas01.cpp)
 #include <iostream> #include "Vec.h" using namespace std; int main() { Vec<short> x(4); short Result; short i; x[0] = 3; for (i = 1; i < 4; i ++) x[i] = x[i-1] * 2; Result = 0; for (i = 0; i < 4; i ...

Get C++: A Dialog Programming with the C++ Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.