0% found this document useful (0 votes)
34 views31 pages

Tong Hop

Uploaded by

10423184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views31 pages

Tong Hop

Uploaded by

10423184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

WARM UP 1

1. The difference between RAM and ROM?


- RAM (random access memory) is volatile. Your program and data are stored in
RAM when you are using the computer.
- ROM (read-only memory) contains fundamental instructions that cannot be lost or
changed by the user. ROM is non-volatile.
2. The difference between Memory and Secondary Storage?
- The memory unit stores information. Each computer contains memory of two main
types: RAM and ROM.
- Secondary storage devices are used to be permanent storage area for programs
and data.
3. What is a software?
- Software means a program or a set of programs
4. The difference between Application Software and System Software?
- Application software consists of those programs written to perform particular tasks
required by the users.
- System software is the collection of programs that must be available to any
computer system for it to operate.
5. What are stages in software development?
Software development consists of
three overlapping phases
- Development and Design
- Documentation
- Maintenance
6. What is machine language?
- Machine languages are the lowest level of computer languages.
- Programs written in machine language consist of 1s and 0s.
- Programs in machine language can control directly to the computer’s hardware.
- A machine language instruction consists of two parts: an instruction part and an
address part
7. What is a compiler?
- Programs in a high-level languages must be translated into a low level language
using a program called a compiler
8. Order the machine language, assembly language, C, C++, Java from lowest to
highest language.
9. What is an Algorithm?
- Algorithm is a sequence of steps that describes how the data are to be processed to
produce the desired outputs
10. Write the algorithm for the following computer program using pseudocode
a. let the user input a (positive) number (called num)
b. calculate the sum of numbers from 1 to num
c. print the result to screen
1. Start
2. Input:
- Prompt the user to enter a positive number (num)
3. Initialization:
- Set sum = 0
4. Loop:
- For i = 1 to num
- sum = sum + i
5. Output:
- Print the sum to the screen
6. End

WARM UP 2
1. In your opinion, what is the Internet?
- The Internet is often referred to as the Information Superhighway because it
connects millions of people across the globe.
2. The differences between LAN, MAN, and WAN
- WAN is an acronym for Wide Area Network. LAN is a network that usually connects
a small group of computers in a given geographical area. MAN is a comparatively
wider network that covers large regions- like towns, cities, etc. The WAN network
spans to an even larger locality.
3. What is an URL?
- A URL (Uniform Resource Locator) is a unique identifier used to locate a resource
on the Internet. It is also referred to as a web address.
4. What are the basic parts of an email?
- Emails can be broken into 5 major parts: the sender, subject line, salutations, body,
and CTA.
5. Name 6 search engines that you know?
- AOL Search, Ask, Big, Google, Yahoo!
6. Name 5 meta search engines that you know?
- Dogpile, Ixquick, MetaCrawler, Search, Clusty.
7. Name 5 Electronic Commerce sites that you know?
- Amazon · eBay · Alibaba · AliExpress · Walmart
8. Name 5 Cloud Computing platforms that you know?
Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), IBM Cloud,
Alibaba Cloud.
9. Name 5 Internet Security Suites that you know?
- Norton, WatchGuard Total Security Suite, Total AV, Trend Micro, Avast.

WARM UP 3
WARM UP 4
4.Write a C++ code for a program that calculates the factorial of an input number?
5.Write a C++ program that calculates a random number 1 through 100. The program then
asks the user to guess the number. If the user guesses too high or too low then the
program should output "too high" or "too low" accordingly. The program must let the user
continue to guess until the user correctly guesses the number

WARM UP 5
1.Write a C++ program that gets a number from input and prints all the armstrong numbers
from 1 to the given number.
2.Armstrong numbers are those numbers whose value is equal to the sum of digits of the
number to their third power.
For example, 153 is an armstrong number because 153 = 1^3 + 5^3 + 3^3.
3.Write a C++ program that gets a binary number from input and converts the number
to decimal.

WARM UP 6
1. Write a C++ code for a program that intakes four numbers from the input and print
out the biggest number.
2. Write a C++ code for a program that gets a positive number (called n) from the input
and displays all prime numbers from 1 to n . A prime number is the number that
divides only 1 and itself (Note: 1 is not a prime number).

3. Write a C++ code for a program that gets two numbers from the input (called n1 and
n2) and displays the Greatest common divisor of them. For example: n1 = 6, n2 = 9
then the greatest common divisor of n1 and n2 is 3.

WARM UP 7
WARM UP 8
1. Write a C++ code for a program that allows the user to input an array of n integers
(n is entered by the user) and print out the average number of the array.
2. Write a C++ code for a program that allows the user to input an array of n integers
(n is entered by the user) and print out the smallest number of the array.

3. Write a C++ code for a program that allows the user to input an array of n integers
(n is entered by the user) and print out the array with ascending order.
WARM UP 9
1.Write a C++ code for a program that allows the user to input an array of n integers (n is
entered by the user) and separates even and odd numbers of the array then put all even
numbers first, and then odd numbers. Finally, print out the array.
2.Write a C++ code for a program that allows the user to input an array of n integers (n is
entered by the user) and sort the array in reverse order. Finally, just print out the array.

3.Write a C++ code for a program that allows the user to input an array of n integers (n is
entered by the user) and print out the second largest number of the array. The second
largest number is the number that is smaller than the biggest one(s) and no other number is
bigger than it. For example: a[8] = [1,6,2,6,4,9,9,9] then 6 is the second largest number.
4.Write a C++ code for a program that allows the user to input an array of n integers (n is
entered by the user) and sorting the array with descending order. Finally, just print out the
array.
WARM UP 10
1. Write a C++ code for a program that allows the users to input an array of n integers (n is
entered by the user) and a function that separates even and odd numbers of the array and
put all even numbers first, and then odd numbers. Finally, the program will print out the
array.

#include <iostream>

// Function to separate even and odd numbers


void separateEvenOdd(int arr[], int n) {
int left = 0; // Index for even numbers
int right = n - 1; // Index for odd numbers

while (left < right) {


// Move left index to the right until an odd number is found
while (arr[left] % 2 == 0 && left < right) {
left++;
}

// Move right index to the left until an even number is found


while (arr[right] % 2 == 1 && left < right) {
right--;
}

// Swap even and odd numbers


if (left < right) {
std::swap(arr[left], arr[right]);
left++;
right--;
}
}
}

int main() {
using namespace std;

int n;

// Get the size of the array from the user


cout << "Enter the size of the array: ";
cin >> n;

// Get the array elements from the user


int arr[n];
cout << "Enter " << n << " integers:" << endl;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}

// Call the function to separate even and odd numbers


separateEvenOdd(arr, n);

// Print the modified array


cout << "Modified Array: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

2. Write a C++ code for a program that allows the users to input an array of n integers (n is
entered by the user) and a function to sort the array in reverse order. Finally, the program
will print out the array.

#include <iostream>

// Function to sort the array in reverse order


void reverseSort(int arr[], int n) {
for (int i = 0; i < n / 2; ++i) {
// Swap elements symmetrically around the middle
std::swap(arr[i], arr[n - i - 1]);
}
}

int main() {
using namespace std;

int n;

// Get the size of the array from the user


cout << "Enter the size of the array: ";
cin >> n;

// Get the array elements from the user


int arr[n];
cout << "Enter " << n << " integers:" << endl;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
// Call the function to sort the array in reverse order
reverseSort(arr, n);

// Print the sorted array


cout << "Sorted Array in Reverse Order: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

3. Write a C++ code for a program that allows the users to input an array of n integers (n is
entered by the user) and a function to return the second largest number of the array. Then
the program will print out the number. The second largest number is the number that is
smaller than the biggest one(s) and no other number is bigger than it. For example:
a[8]=[1,6,2,6,4,9,9,9] then 6 is the second largest number.

#include <iostream>
using namespace std;

// returns the index of second largest


// if second largest didn't exist return -1
int secondLargest(int arr[], int n) {
int first = 0, second = -1;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[first]) {
second = first;
first = i;
}
else if (arr[i] < arr[first]) {
if (second == -1 || arr[second] < arr[i])
second = i;
}
}
return second;
}

int main() {
int arr[100];
int n;
cout<<"Input size of an array: ";
cin>>n;
cout<<"Input "<<n<<" elements: ";
for (int i=0;i<n;i++){
cin>>arr[i];
}
int index = secondLargest(arr, sizeof(arr)/sizeof(arr[0]));
if (index == -1)
cout << "Second Largest didn't exist";
else
cout << "Second largest : " << arr[index];
}

4. Write a C++ code for a program that allows the user to input an array of n integers (n is
entered by the user) and a function to sort the array with descending order. Finally, the
program will print out the array.

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


bool swapped = true;
while (swapped) {
swapped = false;
for (int i = 0; i < n - 1; i++) {
if (arr[i] < arr[i + 1]) {
swap(arr[i], arr[i + 1]);
swapped = true;}
}
n--;
}
}

int main() {
int arr[100];
int n;
cout<<"Input size of an array: ";
cin>>n;
cout<<"Input "<<n<<" elements: ";
for (int i=0;i<n;i++){
cin>>arr[i];
}

bubbleSort(arr, n);
cout << "After sorting: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";}
return 0;
}
5. Binary Search is recursively defined algorithm for searching through a sorted list (array)
to find a position of a particular value. It is particularly efficient for large lists, since it avoids
the need to search sequentially (i.e. one value at a time).

The idea is as follows. Suppose we want to find a position of the value v in the previously
sorted array a[]. We first look at the value stored half way along a[]. If this happens to be v,
we are finished, and we can simply return this middle position. However, if the value at the
middle position is less than v, we now know we only have to search the 2nd half of the list,
and so we repeat the search procedure just with this half of the list. Similarly, if the value at
the middle position is greater than v, we now know we only have to search the 1st half of
the list. (Imagine looking for the page containing the definition for a particular word in a
dictionary. If using binary search, you would start at the middle page, and then just restrict
your attention either to the part of the dictionary before this page, or the half after this page,
depending on what words you saw on the middle page.)

Write a function with function declaration

int binary_search(int value, int list[], int first, int last);


which searches the values in the array "list[]" from "first" to "last" to find an array element
with value "value". If it finds "value", it returns the position of "value" in "list[]". Otherwise it
returns -1. For example, given the array:

list[] = 2,2,3,5,8,14,16,22,22,24,30

the call

binary_search(24, list, 0, 10);


returns the value 9, the call

binary_search(24, list, 2, 6);


returns the value -1, and the call

binary_search(22, list, 0, 10);


either returns the value 7.

Then write a main function to allow the user to input an array of n integers (n is entered by
the user) and a value. The function then calls the binary search function to answer if the
value is appeared in the array.
Review Questions

1 Choose WRONG sentence(s):

-A computer program is a set of instructions used to operate a computer.

-Writing computer programs is called computer programming.

-Programming languages are used to create computer programs.

-Software means a program to operate CPU and ALU.

2 Choose WRONG sentence(s):

-High level programming languages create computer programs using instructions that are much easier to
understand than natural languages such as English
-Programs in a high-level language must be translated into a low level language using a program called a
compiler

-A compiler translates programming code into a low-level format

-Assembly is a high-level programming language

3 Which one(s) are NOT identifiers in C++?

-DegToRad

-5_modules

-_density

-addNums

4 In C++, which information is NOT in the function header line?

-Type of data is returned from the function

-The name of the function

-A statement

-Parameters

5 What is the output of the statement: cout<<”|”<< setw(10)<< setprecision(3) << 15.77<<”|”;

-| 15.770|

-| 1.577|

-| 15.77|

-| 15.8|

1. Why we need to declare variable? What is the syntax of the variable declaration in C++? Give an example
code in C++.*

We need to declare variables in order to reserve memory space to store data and provide a name to access
that data within a program. Variable declaration in C++ informs the compiler about the type of data the variable
will hold and allocates the necessary memory.

The syntax of variable declaration in C++ is as follows:

data_type variable_name;
Here's an example code

#include <iostream>

int main() {

int age;

age = 25;

std::cout << "Age: " << age << std::endl;

return 0;

2.What is an assignment statement? What is the syntax of the assignment statement in C++? Give an example
code in C++.*

An assignment statement in C++ is used to assign a value to a variable. It updates the value stored in a variable
with a new value.

The syntax of the assignment statement in C++ is as follows:

variable_name = value;

Here's an example code


#include <iostream>

int main() {

int num1, num2;

num1 = 10;

num2 = 20;

std::cout << "num1: " << num1 << std::endl;

std::cout << "num2: " << num2 << std::endl;

return 0;

3.What are the syntaxes of the if statement in C++? Give an example code in C++.

In C++, there are two syntaxes for the if statement:

1. Simple if statement:

if (condition) {

// Code to be executed if the condition is true

2. if-else statement:

if (condition) {

// Code to be executed if the condition is true

} else {

// Code to be executed if the condition is false

Here's an example code


#include <iostream>

int main() {

int num = 10;

if (num > 0) {

std::cout << "The number is positive." << std::endl;

if (num % 2 == 0) {

std::cout << "The number is even." << std::endl;

} else {

std::cout << "The number is odd." << std::endl;

return 0;

4.What is the syntax of the while statement in C++? Give an example code in C++.

The syntax of the while statement in C++ is as follows:

while (condition) {

// Code to be executed repeatedly as long as the condition is true

Here's an example code

#include <iostream>

int main() {

int count = 1;

while (count <= 5) {

std::cout << "Count: " << count << std::endl;

count++;
}

return 0;

5.What is the syntax of the for statement in C++? Give an example code in C++.

The syntax of the for statement in C++ is as follows:

for (initialization; condition; increment/decrement) {

// Code to be executed in each iteration of the loop

Here's an example code

#include <iostream>

int main() {

for (int i = 1; i <= 5; i++) {

std::cout << "Iteration: " << i << std::endl;

return 0;

6. What are the differences between while statement and do..while statement?*

1. Condition Evaluation:

- `while` statement: The condition is evaluated before executing the loop body. If the condition is false initially,
the loop body will not be executed at all.

- `do...while` statement: The loop body is executed first, and then the condition is evaluated. Even if the
condition is false initially, the loop body is executed at least once.

2. Loop Control:
- `while` statement: The loop control variable(s) are typically declared and updated outside the loop.

- `do...while` statement: The loop control variable(s) are typically declared and updated within the loop body.

3. Usage:

- `while` statement: It is used when the number of iterations is not known beforehand, and the loop may not
execute at all if the condition is initially false.

- `do...while` statement: It is used when the loop body needs to be executed at least once, regardless of the
condition.

7.Explain what is potentially wrong with the line of code below:

char = -1

- The char function represents characters and positive numbers, there are no cases of negative
numbers.

8.What is the output of this program:*

⇒ Answer : t1 is 1 and t2 is 1

9.What is the output of this program:

⇒ Answer : 2 4 6 8 10 12 14 16 18 20

10.What is the output of this program when running the program and the user inputs two numbers 9 3:
⇒ answer : 5, 7,

11. Write a C++ program that calculates a random number 1 through 100. The program then asks the user to
guess the number. If the user guesses too high or too low then the program should output "too high" or "too low"
accordingly.
The program must let the user continue to guess until the user correctly guesses the number.* .

note : câu này phải bắt buộc thêm các library khác, không thì không thể ra đáp án được.

.Write a program that takes two integers (hour, minute) and converts them to seconds. (with the following code)

BONUS

Algorithm:
1. Khai bao 1 so int nao do trong khoang tu 1 den 100 (phai +1 de tru di truong hop 0-
>99 tru di 1 TH bi thieu)/ mot so number random tu 1 ->100. Trong c++ co mot ham
ran() de ho tro
2. Tao loop while
3. If input ==

write a c++ code to


user inputs 2 int number
user inputs an operator
Program executes the operator on the 2 numbers and outputs the result

For ex:
Input number 1: 8
Input number 2: 2
Input the operator: /
The expression 8/2 is 4
#include <iostream>
using namespace std;
int main() {
int a,b;
cout <<"Nhap so nguyen a:";
cin>>a;
cout<<"Nhap so nguyen b:";
cin>>b;

char op;
cout<<"input the operator:";
cin>>op;

if(op == '+') {
cout<<"The expression"<<a<<op<<b<<" is "<<(a+b);
}
else if (op == '-') {
cout<<"The expression"<<a<<op<<b<<" is "<<(a-b);
}
else if (op == '*') {
cout<<"The expression"<<a<<op<<b<<" is "<<(a*b);
}
else if (op == '%') {
cout<<"The expression"<<a<<op<<b<<" is "<<(a%b);
}
else {
cout<<"Not an expression";

}
}

WA EX7
Tạo 1 biến nhận giá trị int từ bàn phím
check xem phải năm nhuận ko

// Online C++ compiler to run C++ program online


#include <iostream>
using namespace std;
int main(){
int day, month, year;
cout<<"Input a day";
cin>>day;
cout<<"Input a month";
cin>>month;
cout<<"Input a year";
cin>>year;

int numofdays = 0;
if(month == 1){
numofdays = day;
}
else if(month == 2){
numofdays == 31 + day;
}
else if(month <=8){
int numoddmonth = month/2; //tim so thang le
int numevenmonth = (month-1)/2;//tim so thang chan khi da trai qua khi dang toi
thang month do
numofdays == numoddmonth*31 + (numevenmonth - 1)*30;

if(year%100 == 0){
if(year%400==0){
numofdays = numofdays + 29;

}
else{ numofdays = numofdays + 28;
}
}
else{
If(year%4==0){
cout<<"This is a leap year";
numofdays = numofdays + 29;
else{ cout<<"This is not a leap year";
numofdays = numofdays + 28;
}
}
}
else if(month > 8){
int numofoddmonth = (month-8)/2;
int numofevenmonth = (month + 1 - 8)/2;
numofdays = 4*31 + 2*30; //tu thang 1 den thang 8 co 3 thang chan nhung tru di
TH thang 2
numofdays = numofdays + numofoddmonth*30 + numofevenmonth*31;
if(year%100 == 0){
if(year%400==0){
numofdays = numofdays + 29;

}
else{ numofdays = numofdays + 28;
}
}
else{
If (year%4==0)
cout<<"This is a leap year";
numofdays = numofdays + 29;
else{ cout<<"This is not a leap year";
numofdays = numofdays + 28;
}

}
numofdays = numofdays + day;
}
cout<<"The day is "<<numofdays<<"th of the year"; return 0;
}

1/ #include <iostream>
using namespace std;
int main(){ //nhap vao du lieu
int n;
int a[1000];

cout<<"How many integer numbers?"


cin>>n;
for(int i = 0;i<n;i++){
cout<<"Let input the "<<i<<"th number:";
cin>>a[i];

}
int sum =0;
for(int i = 0; i<n; i++){
sum = sum + a[i];
}
cout<<"The average is:"<<sum/n;

return 0;
}
2/
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
int main() {
int n;
int a[1000];
cout<<"How many integer numbers?";
cin>>n;
for(int i = 0;i<n;i++){
cout<<"Let input the "<<i<<"th number:";
cin>>a[i];
}
int smallest = a[0];
for(int i =0;i<n;i++){
if (smallest < a[i]) {
smallest = a[i]; }
}
return 0;
}

3/
GT: a[] = 7 3 1 5 9 8
1 // in ra màn hình số 1, lần lượt các số nhỏ trong mảng//(trong từng mảng nhỏ, hoán
đổi vị trí phần tử nhỏ nhất thành phần tử đầu tiên -> từng mảng 731598 -> 137598 ->
mảng 7598 thành 5798 -> mảng 798 hoán đổi vị trí 7 vs ptu đầu tiên -> 98 -> 89
//sau đó hoán đổi vị trí của số 1 vs 7 để số 1 lên -> 1 3 7 5 9 8 & từ lần lặp thứ 2 thì xử
lý từ vị trí thứ 2 trở đi (số 3)

// Online C++ compiler to run C++ program online


#include <iostream>
using namespace std;
int main() {
int n;
int a[1000];
cout<<"How many integer numbers?";
cin>>n;
for(int i = 0;i<n;i++){
cout<<"Let input the "<<i<<"th number:";
cin>>a[i];

}
int k = 0;
while(k<n){
int ismallest = k; //vi tru thu bao nhieu trong cai array cua gia tri nho nhat
int smallest = a[k];
for(int i =k;i<n;i++){
if (smallest > a[i]) {
smallest = a[i];
ismallest = i;
}
}
cout<<"smallest:";
int temp = a[ismallest];
a[ismallest] = a[k];
a[k] = temp;

k++
}

return 0;
}

You might also like