Câu hỏi môn Tin học đại cương| Môn Tin học đại cương| Trường Đại học Bách Khoa Hà Nội

Ex1: Adds two numbers.
Input  Description: number 1 and number 2

Output Description: Result

Ex2: Write a program that asks the user to type the width and the length of a rectangle and then outputs to the screen the perimeter  and the area of that rectangle

Note: The width or the length less than and equal to 0 --> print out "Error"

Input  Description: the width  and the length

Output Description: the perimeter  and the area 

Lesson 2:
Ex1: Adds two numbers.
Input Description:number 1andnumber 2
OutputDescription:Result
Ex2: Write a program that asks the user to type the width and the length of a rectangle and then outputs
to the screen the perimeter and the area of that rectangle
Note: The width or the lengthless than and equal to 0 --> print out "Error"
InputDescription:the widthandthe length
OutputDescription:theperimeterandthearea
Ex3: Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This
program can use only 2 variables.
InputDescription:
Integer 1
Integer 2
Integer 3
Integer 4
Integer 5
OutputDescription:the averageof the 5 integers
Ex4: Write a program that asks the user to type 2 integers A and B and exchange the value of A and B.
InputDescription :
Integer A
Integer B
OutputDescription :
Integer B
Integer A
Ex5: Write a program that asks the user to type the price without tax of one kilogram of tomatoes, the
number of kilograms you want to buy and the tax in percent units. The program must write the total price
including taxes.
InputDescription:
price without taxes
weight
taxes
OutputDescription:total price including taxes
Lesson 3:
Ex1: Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between
56 and 78 (both included). In the other case, it writes "YOU LOSE". Nested if statements
InputDescription:Integer
Output Description:YOU WINorYOU LOSE
Ex2: Write a program that asks the user to type an integer N and that indicates if N is a prime number or
not.
Note:
If this number is a prime number print "it is a prime number"
If this number is not a prime number print "it is not a prime number"
Input Description:integer
Output Description:it is a prime numberorit is not a prime number
Ex3: Write a program that checks if the angles given can make a triangle or not (their sum must be 180
degrees).
Note:
If the angles given can make a triangle print "It is a triangle"
If the angles given can not make a triangle print "This is not a triangle"
Input Description:
integer
integer
integer
Output Description:It is a triangleorThis is not a triangle
Lesson 4:
Ex1: Read positive numbers and print the average between them when you type a number smaller than 1.
Note:
If the first number < 1 ---> printERROR
Output correct to 4 digits after the comma.
Input Description:An array of positive numbersthatend with a number smaller than 1.
Output Description: Theaverageof them except for the last number.
Ex2: Write a program that asks the user to type the value of N and compute N!.
Note:
If N<0 print "Error"
Input Description:integerN.
Output Description:N!.
Ex3: Write a program that asks the user to type the value of N and compute N!.
Note:
If N<0 print "Error"
Input Description:integerN.
Output Description:N!.
Ex3: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
if n is not a positive integer, print out "Error"
Input Description: Apositive integernand apositive number x
Output Description: Theresultsof theabove expressions.
Ex4: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
if n is not a positive integer, print out "Error"
Input Description: Apositive integernand apositive number x
Output Description: Theresultsof theabove expressions.
Ex5: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Ex6: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Input Description: Apositive number xandapositive integer n
Output Description: Theresultsof theabove expressions.
Ex7: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Input Description: Apositive number xandapositive integer n
Output Description: Theresultsof theabove expressions.
Ex8: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Input Description: Apositive number xandapositive integer n
Output Description: Theresultsof theabove expressions.
Ex9: Write a program that asks the user to type the values of x and n and compute the following
expressions:
Note:
Output correct to 4 digits after the comma, each printed on one line.
If n < 1 or x <0 print "Error"
Input Description: Apositive number xandapositive integer
Output Description: Theresultsof theabove expressions.
Lesson 5:
Ex1: Read n integers into an array, print the input values from last to first, then sum the elements of the
array.
Note:
If n <= 0 print "Error"
Input Description: The first line contains apositive integer n, which is thelength of the input array.
The second linecontains n integers.
Output Description: The first output line is theelements of the arrayprinted inreversed order.
The second output line isthe sum of elements of the array
Ex2: Compute the maximum value of an array. Return the index of the maximum value in an array.
Input Description:The first line contains apositive integer n, which is thelength of the input array.
The second linecontains n elements of the array.
Output Description:The index of themaximum valuein an array. If there are2ormore elements with the
same valueas themaximum value,print allthe indices of those numbers out,separated by spaces.
Note:
Valuein an arraycount from 0
If n <= 0 print "Error"
Ex3: Read an integer n and type n integers into array A. Enter an integer V. Write a program that searches
if V is in array A. The program writes “V is in the array” or “V is not inthe array”
Input Description:The first line contains apositive integer n, which isthe length of the input array.
The second linecontains n integerswhich areelements of the array.
The third linecontains an integer V.
Output Description: A string that has the format V is in the array or V is not in the array, with thevalue
of Vin place of the letter V.
Lesson 6:
Ex1: Write a program that sorts an array into ascending order.
Note:
First, read an integer N (the number of elements of the array), then read N integers elements of
the array.
The program needs to print N elements of the array in ascending order.
Input Description:
- First-line contains an integer N the number of elements of the array.
- Second-line contains N integers the elements of the array.
Output Description:
First-line contains N integers elements of the array after sorting.
Ex2: Write a program that reads an array of N float numbers. Add one element x before the
element.
Note:
The elements of the array are indexed from 1 to N.
If k=n+1, insert x after the last element of the array.
Check if element exists or k=n+1. If not, print Error.
Round all float numbers to 1 decimal place.
Input Description:
First-line contains number N the number of elements of the array.
Second-line contains N float numbers elements of the array.
The third line contains two numbers k x the position to insert and the value of the number to
insert.
Output Description: -First line contains N+1 elements of the array after inserting number x at position
Ex3: Write a program that reads two matrices:
Matrix X of size M*N (M rows, N columns).
Matrix Y of size N*K (N rows, K columns).
Do multiplication of matrices X and Y, store the result in matrix Z (Z=X*Y)
Input Description:
First-line contains 3 numbers M, N, K.
Line 2 to line M+1: Each line i (2<=i<=M+1) contains N integers denoting the row of
matrix X.
Line M+2 to line M+N+1: Each line i (M+2<=i<=M+N+1) contains K integers denoting the
row of matrix Y.
Output Description:
Line 1 to line M: Each line I (1<=i<=K) contains K integers denoting the row of matrix Z.
Lesson 7:
Ex1: Write a program that reads a string S and an integer N. Shorten the string so that it only contains the
first N character.
Note: If the string has fewer than N characters then do not change it
Input Description:
First-line contains string S.
Second-line contains an integer N.
Output Description:
String S after being shortened.
Ex2: Write a program that reads two strings S and T. Check if string S contains string T.
Note:
String S is considered to contain string T if we can obtain string T by removing x left-most
characters and y right-most characters out of S (x and y can be 0).
If string S contains string T then output True else output False.
Input Description:
String S
String T
Output Description:TrueorFalse
Ex3: Write a program that reads two strings S and T and an integer N. Check if string T occurs in the suffix
of S starting at position N.
Note:
Strings are indexed from 0.
The suffix of S starting at position N is the last |S|-N characters of S. |S| is the length of S.
Input Description:
String S
String T
Integer N
Output Description:Print 1 if string T occurs in string S after position N, else print 0.
Ex4: Write a program that reads two strings S and T. Count the number of times that each character
appears in both strings.
NoteFirst output a string of distinct characters in both strings. Then, for each of those characters output
the number of its occurrences in both strings.
Input Description:
String S
String T
Output Description:
In line 1, a string of distinct characters appears in S or T. (Sort these characters in ascending order
according to their ASCII value).
From line 2, on each line is the number of a character’s occurrences (according to the order in the
string on line 1).
Ex5: Write a program that reads a decimal number and converts it into binary.
Input Description:A decimal integer
Output Description:The binary equivalent of the input number.
Lesson 8:
Ex1: Write a C function that takes a positive integer n as an argument and returns the largest power of 2
less than or equal to n.
Input Description: A positive integer n
Output Description: The largest power of 2 less than or equal to n
Ex2: Write functionprimethat indicates an integer N is a prime number or not (return 1 if N is prime and 0
otherwise)
Read m integers. Calculate and print an average of prime numbers using functionprimeDescription input:
The first line contains a positive integer m. The second line contains m integers
Description output: Average of prime numbers in m integers using functionprime. If there is no prime
number, print Error
Ex3: Write a function to calculate the following function
a) Save the following numbers -1.5, -1, …, 100 to X. Calculate the values of f with the inputs are elements
of X and save to array Y.
b) Input an integer n (1 n 20). Save the elements of array Y greater than n to array Z and print out their
sum.
c) Sort array Z in ascending order.
Input Description:An integer n (1 n 20)
Output Description:
The first output line contains the sum of elements in array Z.
The second line contains the elements of array Z in ascending order.
Ex4: Given a mathematical function H(x, y) defined as follows
a) Write the C function Comp_H(double, double) that receives 2 real numbers x, y, computes and returns
the value of H(x, y).
b) Write a program using the C function Comp_H(double) to compute the values of H(x, y) at the
following inputs: (-2, 5), (-1.5, 4), (-1, 3), …, (5, -9), (5.5, -10), (6, -11) (the difference between 2 successive x
is 0.5, between 2 successive y is -1), then store all outputs in array B.
c) Display all B's elements in ascending order.
Lesson 9:
Ex1: Store Information and Display it Using Structure
Run this code:
Ex2: Program to add two distances in the inch-feet system
Input Description:
Enter 1st distance
Enter feet:
Enter inch:
Enter 2nd distance
Enter feet:
Enter inch:
OutputDescription:
Sum of distances =Result
Ex3: Use struct to enter information for different cities. Each city (city) has: city name (name), population
(population),total income (income).
Then show the city with the highest total income and the most populous city.
Example:
Input: 2
hanoi
1000
2000
hcm
2000
5000
Output:
The city with the largest population: hcm
The city with the highest total income: hcm
| 1/9

Preview text:

Lesson 2: Ex1: Adds two numbers.
Input Description: number 1 and number 2
Output Description: Result
Ex2: Write a program that asks the user to type the width and the length of a rectangle and then outputs
to the screen the perimeter and the area of that rectangle
Note: The width or the length less than and equal to 0 --> print out "Error"
Input Description: the width and the length
Output Description: the perimeter and the area
Ex3: Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This
program can use only 2 variables. Input Description: Integer 1 Integer 2 Integer 3 Integer 4 Integer 5
Output Description: the average of the 5 integers
Ex4: Write a program that asks the user to type 2 integers A and B and exchange the value of A and B. Input Description : Integer A Integer B Output Description : Integer B Integer A
Ex5: Write a program that asks the user to type the price without tax of one kilogram of tomatoes, the
number of kilograms you want to buy and the tax in percent units. The program must write the total price including taxes. Input Description: price without taxes weight taxes
Output Description: total price including taxes Lesson 3:
Ex1: Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between
56 and 78 (both included). In the other case, it writes "YOU LOSE". Nested if statements Input Description: Integer
Output Description: YOU WIN or YOU LOSE
Ex2: Write a program that asks the user to type an integer N and that indicates if N is a prime number or not. Note:
● If this number is a prime number print "it is a prime number"
● If this number is not a prime number print "it is not a prime number" Input Description: integer
Output Description: it is a prime number or it is not a prime number
Ex3: Write a program that checks if the angles given can make a triangle or not (their sum must be 180 degrees). Note:
● If the angles given can make a triangle print "It is a triangle"
● If the angles given can not make a triangle print "This is not a triangle" Input Description: integer integer integer
Output Description: It is a triangle or This is not a triangle Lesson 4:
Ex1: Read positive numbers and print the average between them when you type a number smaller than 1. Note:
If the first number < 1 ---> print ERROR
Output correct to 4 digits after the comma.
Input Description: An array of positive numbers that end with a number smaller than 1.
Output Description: The average of them except for the last number.
Ex2: Write a program that asks the user to type the value of N and compute N!. Note:
If N<0 print "Error"
Input Description: integer N. Output Description: N!.
Ex3: Write a program that asks the user to type the value of N and compute N!. Note:
If N<0 print "Error"
Input Description: integer N. Output Description: N!.
Ex3: Write a program that asks the user to type the values of x and n and compute the following expressions: Note:
● Output correct to 4 digits after the comma, each printed on one line.
● if n is not a positive integer, print out "Error"
Input Description: A positive integer n and a positive number x
Output Description: The results of the above expressions.
Ex4: Write a program that asks the user to type the values of x and n and compute the following expressions: Note:
● Output correct to 4 digits after the comma, each printed on one line.
● if n is not a positive integer, print out "Error"
Input Description: A positive integer n and a positive number x
Output Description: The results of the above expressions.
Ex5: Write a program that asks the user to type the values of x and n and compute the following expressions:
Ex6: Write a program that asks the user to type the values of x and n and compute the following expressions: Note:
● Output correct to 4 digits after the comma, each printed on one line.
● If n < 1 or x <0 print "Error"
Input Description: A positive number x and a positive integer n
Output Description: The results of the above expressions.
Ex7: Write a program that asks the user to type the values of x and n and compute the following expressions: Note:
● Output correct to 4 digits after the comma, each printed on one line.
● If n < 1 or x <0 print "Error"
Input Description: A positive number x and a positive integer n
Output Description: The results of the above expressions.
Ex8: Write a program that asks the user to type the values of x and n and compute the following expressions: Note:
● Output correct to 4 digits after the comma, each printed on one line.
● If n < 1 or x <0 print "Error"
Input Description: A positive number x and a positive integer n
Output Description: The results of the above expressions.
Ex9: Write a program that asks the user to type the values of x and n and compute the following expressions: Note:
● Output correct to 4 digits after the comma, each printed on one line.
● If n < 1 or x <0 print "Error"
Input Description: A positive number x and a positive integer
Output Description: The results of the above expressions. Lesson 5:
Ex1: Read n integers into an array, print the input values from last to first, then sum the elements of the array. Note:
If n <= 0 print "Error"
Input Description: The first line contains a positive integer n, which is the length of the input array.
The second line contains n integers.
Output Description: The first output line is the elements of the array printed in reversed order.
The second output line is the sum of elements of the array
Ex2: Compute the maximum value of an array. Return the index of the maximum value in an array.
Input Description: The first line contains a positive integer n, which is the length of the input array.
The second line contains n elements of the array.
Output Description: The index of the maximum value in an array. If there are 2 or more elements with the
same value as the maximum value, print all the indices of those numbers out, separated by spaces. Note:
● Value in an array count from 0
● If n <= 0 print "Error"
Ex3: Read an integer n and type n integers into array A. Enter an integer V. Write a program that searches
if V is in array A. The program writes “V is in the array” or “V is not in the array”
Input Description: The first line contains a positive integer n, which is the length of the input array.
The second line contains n integers which are elements of the array.
The third line contains an integer V.
Output Description
: A string that has the format “V is in the array” or “V is not in the array”, with the value
of V
in place of the letter V. Lesson 6:
Ex1: Write a program that sorts an array into ascending order. Note:
● First, read an integer N (the number of elements of the array), then read N integers – elements of the array.
● The program needs to print N elements of the array in ascending order. Input Description:
- First-line contains an integer N – the number of elements of the array.
- Second-line contains N integers – the elements of the array. Output Description:
First-line contains N integers – elements of the array after sorting.
Ex2: Write a program that reads an array of N float numbers. Add one element x before the element. Note:
● The elements of the array are indexed from 1 to N.
● If k=n+1, insert x after the last element of the array. ● Check if
element exists or k=n+1. If not, print “Error”.
● Round all float numbers to 1 decimal place. Input Description:
● First-line contains number N – the number of elements of the array.
● Second-line contains N float numbers – elements of the array.
● The third line contains two numbers k x – the position to insert and the value of the number to insert.
Output Description: -First line contains N+1 elements of the array after inserting number x at position
Ex3: Write a program that reads two matrices:
● Matrix X of size M*N (M rows, N columns).
● Matrix Y of size N*K (N rows, K columns).
Do multiplication of matrices X and Y, store the result in matrix Z (Z=X*Y) Input Description:
● First-line contains 3 numbers M, N, K.
● Line 2 to line M+1: Each line i (2<=i<=M+1) contains N integers denoting the row of matrix X.
● Line M+2 to line M+N+1: Each line i (M+2<=i<=M+N+1) contains K integers denoting the row of matrix Y. Output Description:
Line 1 to line M: Each line I (1<=i<=K) contains K integers denoting the row of matrix Z. Lesson 7:
Ex1: Write a program that reads a string S and an integer N. Shorten the string so that it only contains the first N character.
Note: If the string has fewer than N characters then do not change it Input Description:
● First-line contains string S.
● Second-line contains an integer N. Output Description:
String S after being shortened.
Ex2: Write a program that reads two strings S and T. Check if string S contains string T. Note:
● String S is considered to contain string T if we can obtain string T by removing x left-most
characters and y right-most characters out of S (x and y can be 0).
● If string S contains string T then output “True” else output “False”. Input Description: ● String S ● String T
Output Description: True or False
Ex3: Write a program that reads two strings S and T and an integer N. Check if string T occurs in the suffix of S starting at position N. Note:
● Strings are indexed from 0.
● The suffix of S starting at position N is the last |S|-N characters of S. |S| is the length of S. Input Description: ● String S ● String T ● Integer N
Output Description: Print “1” if string T occurs in string S after position N, else print “0”.
Ex4: Write a program that reads two strings S and T. Count the number of times that each character appears in both strings.
Note First output a string of distinct characters in both strings. Then, for each of those characters output
the number of its occurrences in both strings. Input Description: ● String S ● String T Output Description:
● In line 1, a string of distinct characters appears in S or T. (Sort these characters in ascending order
according to their ASCII value).
● From line 2, on each line is the number of a character’s occurrences (according to the order in the string on line 1).
Ex5: Write a program that reads a decimal number and converts it into binary.
Input Description: A decimal integer
Output Description: The binary equivalent of the input number. Lesson 8:
Ex1: Write a C function that takes a positive integer n as an argument and returns the largest power of 2 less than or equal to n.
Input Description: A positive integer n
Output Description: The largest power of 2 less than or equal to n
Ex2: Write function prime that indicates an integer N is a prime number or not (return 1 if N is prime and 0 otherwise)
Read m integers. Calculate and print an average of prime numbers using function prime Description input:
The first line contains a positive integer m. The second line contains m integers
Description output: Average of prime numbers in m integers using function prime. If there is no prime
number, print “Error
Ex3: Write a function to calculate the following function
a) Save the following numbers -1.5, -1, …, 100 to X. Calculate the values of f with the inputs are elements of X and save to array Y.
b) Input an integer n (1 ≤ n ≤ 20). Save the elements of array Y greater than n to array Z and print out their sum.
c) Sort array Z in ascending order.
Input Description: An integer n (1 ≤ n ≤ 20) Output Description :
The first output line contains the sum of elements in array Z.
The second line contains the elements of array Z in ascending order.
Ex4: Given a mathematical function H(x, y) defined as follows
a) Write the C function Comp_H(double, double) that receives 2 real numbers x, y, computes and returns the value of H(x, y).
b) Write a program using the C function Comp_H(double) to compute the values of H(x, y) at the
following inputs: (-2, 5), (-1.5, 4), (-1, 3), …, (5, -9), (5.5, -10), (6, -11) (the difference between 2 successive x
is 0.5, between 2 successive y is -1), then store all outputs in array B.
c) Display all B's elements in ascending order. Lesson 9:
Ex1: Store Information and Display it Using Structure Run this code:
Ex2: Program to add two distances in the inch-feet system Input Description: Enter 1st distance Enter feet: Enter inch: Enter 2nd distance Enter feet: Enter inch: Output Description:
Sum of distances = Result
Ex3: Use struct to enter information for different cities. Each city (city) has: city name (name), population
(population),total income (income).
Then show the city with the highest total income and the most populous city. Example: Input: 2 hanoi 1000 2000 hcm 2000 5000 Output:
The city with the largest population: hcm
The city with the highest total income: hcm