Take-Home Quiz 3 Solutions
(each question is worth 2 pts)
#include "rect.h"SimpleWindow MyWin("My shape", 17.0, 9.0);
int ApiMain() {
MyWin.Open() ;
// Draw a rectangle made up of red and blue squares
for (int i=1; i<=5; i++) {
for (int j=1; j<=5; j++) {
if ( j >= i) {
RectangleShape R1(MyWin, i, j, Blue, 0.75, 0.75) ;
R1.Draw();
}
}
}
return 0 ;
}
. . . . . . . . . . . . . . .
#include <iostream>
using namespace std;
int main(){
for (int count = 1; count < 10; count++) {
cout << "counting backwards to 1 starting with " << count << ": ";
switch (count) {
case 10:
cout << "ten ";
case 9:
cout << "nine ";
case 8:
cout << "eight ";
case 7:
cout << "seven ";
case 6:
cout << "six ";
case 5:
cout << "five ";
case 4:
cout << "four ";
case 3:
cout << "three ";
case 2:
cout << "two ";
case 1:
cout << "one" << endl;
}
}
return 0;
}
When the missing bracket is added as shown above the output is: counting backwards to 1 starting with 1: one counting backwards to 1 starting with 2: two one counting backwards to 1 starting with 3: three two one counting backwards to 1 starting with 4: four three two one counting backwards to 1 starting with 5: five four three two one counting backwards to 1 starting with 6: six five four three two one counting backwards to 1 starting with 7: seven six five four three two one counting backwards to 1 starting with 8: eight seven six five four three two one counting backwards to 1 starting with 9: nine eight seven six five four three two one
for(int i = 1, int sum = 0; i < 11; i++)
sum += i;
int sum (int a, int b)
{
return a+b;
}
#include <fstream.h>
#include <math.h>
int main() {
cout << "Enter a positive integer" << endl;
int Value = 0;
cin >> Value;
cout << "The square root of that integer is: " << sqrt(Value) << endl;
return 0;
}
int value = 0;
while(value < 1 || value > 9) {
cout << "Enter a integer between 1 and 9" << endl;
cin >> value;
}
<#include <fstream.h>
int main() {
ifstream fin("mydata.txt"); ç== 1
int ValuesProcessed = 0;
float ValueSum = 0;
float Value;
while (fin >> Value) {
ValueSum += Value;
++ValuesProcessed;
}
if (ValuesProcessed > 0) {
ofstream fout("average.txt");
float Average = ValueSum / ValuesProcessed;
fout << "Average: " << Average << endl;
}
else
cerr << "No list to average" << endl;
return 0;
}
An object of type ifstream is declared. The identifier for that object is fin and it opens the file mydata.txt for reading.
#include <iostream.h>
void Swap(int Value1, int Value2) {
int Temp = Value1;
Value1 = Value2;
Value2 = Temp;
return;
}
int main() {
int Number1 = 4;
int Number2 = 3;
if (Number1 > Number2) {
Swap(Number1, Number2);
}
cout << "The numbers in sorted order:"
<< Number1 << ", " << Number2 << endl;
return 0;
}
The numbers in sorted order: 4, 3
void fun(int a, int b);
The return type void means that the function does not return anything.
#include <iostream.h>
int i = 10;
void fun() {
{
int i = 1;
cout << i << endl;
{
float i = 3.2;
cout << i << endl;
char i = 'a';
cout << i << endl;
}
cout << i << endl;
}
cout << i << endl;
}
There is a compilation error in the above code because the identifier i is declared twice in the same block. This question was thrown out.