uuuuu
uuuuu
Assignment Works-3
Group E-ET-1
KAUNAS, 2024
1 Task no.3
1.1 Task description
1. Compile the program from Example 3.1 and provide the output in the report. Explain why not all
the data from the program is outputted.
2. Modify the program in Example 3.2 so that you can enter any chosen integer between 1 and 100.
If the number is within the specified range, the program will output the squares of all numbers from
1 to the entered number. If the number exceeds 100, the program should display an error message.
3. Modify the program in Example 3.3 to include the srand() function so that a different number is
generated each time. Change the range of generated numbers to (100, 200).
4. Modify the program in Example 3.4 to display the symbols in three columns, separated by a tab
character.
5. Submit a report containing all assignment points, the code of the programs, their algorithms, and
conclusions.
- The lines with "this line is not outputted" do not appear because their conditions evaluate to
false. For example:
i == j fails because 10 != 11.
b1 && b2 fails because true && false is false.
- Logical operations determine what gets printed.
#include <iostream>
using namespace std;
int main() {
int i = 1, limit;
cout << "\nPress any key and Enter to finish the program" << '\n';
cin >> limit;
return 0;
}
#include <iostream>
#include <cstdlib>
2
#include <ctime>
using namespace std;
int main() {
int i, number;
srand(time(NULL));
number = rand() % 101 + 100;
do {
cout << "Enter your guess: ";
cin >> i;
if (i == number) {
cout << "*** Correct! ***\n";
cout << "The number is: " << number << endl;
} else {
cout << "Incorrect... ";
if (i > number)
cout << "Your guess is too high.\n";
else
cout << "Your guess is too low.\n";
}
} while (i != number);
cout << "\nPress any key and Enter to finish the program" << '\n';
cin >> i;
return 0;
}
#include <iostream>
using namespace std;
int main() {
unsigned char ch;
int count = 0;
cout << "\nPress any key and Enter to finish the program" << '\n';
cin >> ch;
return 0;
}
3
1.3 Comments and conclusions
-Include Libraries:
#include <iostream>: For input and output operations.
-Declare Variables:
int i = 1: Counter variable initialized to 1.
int limit: Variable to store user input.
-Prompt User for Input:
Ask the user to enter a number between 1 and 100 using cout.
Read the input using cin.
-Check Input Range:
If the input is between 1 and 100:
Use a while loop to iterate from 1 to limit.
For each iteration, calculate the square of the number (i * i) and display it.
If the input is out of range:
Print an error message.
-Wait for User Input to Exit:
Prompt the user to press any key to finish (cin >> limit).
- End the Program:
Use return 0; to indicate successful execution.
- Include Libraries:
#include <iostream>: For input and output operations.
#include <cstdlib>: For random number generation.
#include <ctime>: For seeding the random generator.
- Declare Variables:
int i: Variable to store the user's guess.
int number: Variable to store the generated random number.
- Initialize Random Number Generator:
Use srand(time(NULL)) to seed the random number generator.
Generate a random number between 100 and 200 (rand() % 101 + 100).
- Game Loop:
Use a do...while loop to allow repeated guessing.
Inside the loop:
o Prompt the user to guess the number.
o If the guess is correct:
Display a success message and reveal the number.
o If incorrect:
Indicate whether the guess is too high or too low.
- Wait for User Input to Exit:
Prompt the user to press any key to finish (cin >> i).
- End the Program:
Use return 0; to indicate successful execution.
4
Example 3.4 Algorithm
- Include Libraries:
#include <iostream>: For input and output operations.
- Declare Variables:
unsigned char ch: Variable to iterate through ASCII characters.
int count = 0: Counter variable for column formatting.
- Iterate Through Characters:
Use a for loop to iterate over ASCII characters starting from 32.
For each character:
o Print the character and its ASCII code.
o Increment the counter.
o After every three characters, print a newline for column formatting. Use \t for
spacing.
- Wait for User Input to Exit:
Prompt the user to press any key to finish (cin >> ch).
- End the Program:
Use return 0; to indicate successful execution.
5
Conclusion Code 3.3
6
7