If the program in Figure 4.19 is run, what will be displayed?
Figure 4.19. Exercise 1 (codemorbas00.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;
}
If the program in Figure 4.20 is run, what will be displayed?
Figure 4.20. Exercise 2 (codemorbas01.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 ++)
Result = Result + x[i];
cout << Result << endl;
return 0;
}
Modify the program from exercise 3 to ask the user to type as many weights as desired, stopping as soon as a 0 is entered. Add up all of the weights entered and display the total on the screen at the end of the program.
Answers to exercises can be found at the end of the chapter.