The third week of Computer Science CS50 (Harvard) training begins and we continue to learn the C language. The first task: the implementation of a voting system where the candidate with the most votes wins, the so-called. plurality system or simply “winner takes all” (plurality.c). The user can choose one from the list of candidates and vote for him. When the votes are counted, the candidate with the most votes is declared the winner of the election.
In the second week of the Computer Science CS50 (Harvard) course, after completing all the individual tasks, we move on to the C lab, which must be completed in a group. It will be an interesting task – to write the code for the game “Scrabble” (Scrabble.c). The essence of the task: the players come up with words that score a different number of points depending on the letters in this word, while each letter is assigned a different number of points from 1 to 10 and then these points are added up (the sum of the points of the letters gives the total number of points in the word).
For this task, the following table of correspondences of letters and points is used:
We continue to do homework from Problem set 2 in Computer science CS50 (Harvard). Last time, we learned how to encrypt text by shifting letters by a certain number (key) entered by the user. And this time we will solve the problem more complicated, it will be the Substitution.c program and it also encrypts the text by replacing some letters with others according to the principle that the user enters on the command line when the program starts. This principle looks like a string of letters of the alphabet arranged in a reversed order, as in the example:
$ ./substitution JTREKYAVOGDXPSNCUIZLFBMWHQ plaintext: HELLO ciphertext: VKXXN
We continue to take the Computer Science CS50 (Harvard) course, and this time we will do the task of calculating the level of complexity of the text according to the Colman-Liau index (readability.c) in the C programming language. According to the idea of the two authors of the method, we can estimate the complexity of the text and thus distribute books by school grades (from simple texts, where the index is less than 1, to academically complex ones with an index greater than 16). The calculation is based on the formula:
index = 0.0588 * L - 0.296 * S - 15.8 ,
where L is the average number of letters per 100 words in the text,
S is the average number of sentences per 100 words in the text.
We continue to do homework in the Computer science CS50 (Harvard) course, and this time we need to write the program code and encrypt a text message.
The essence of the ceasar.c task is that the program asks for the user’s text input, and then displays a line of text below in encrypted form with the user’s key. The key is specified when starting the program on the command line after naming the program itself argv. So in the example of running the finished program, the user enters the key = 13 and his text “Hello, world!”, and at the output he receives this text in the form of a cipher:
While studying Greedy Algorithms at CS50 (Harvard), let’s solve the problem about coins (Cash.c). Let’s say you need to give change to a customer in a store and you want to minimize the number of coins you give out to each customer. If you are a cashier, then you have coins (25 cents), ten cents (10 cents), five cents (5 cents) and a penny (1 cent) in the cash register. How to decide which coins and how much to transfer to the buyer?
Imagine a “greedy” cashier who wants to get as big a chunk out of this problem as possible with every coin he takes out of the drawer. Suppose you owe the customer 42 cents, then the largest first coin you can take is 25 cents. This choice will reduce the amount to be given away to 17 cents, since 42 – 25 = 17. It turns out that with the help of smaller coins we will need to solve a smaller problem. This approach from largest to smallest will give the smallest possible number of coins in change.
In the cs50 course (Harvard) there is a C programming problem to determine the correctness of a credit card number through the Luhn algorithm (credit.c). Let’s analyze this problem and show two more solutions.
So. Any credit card has a number printed on it, which is also stored in the database of the bank that issued the card. Credit card numbers are generated in such a way that they have some kind of “checksum” – a mathematical relationship between at least one number and others. This checksum allows you to quickly perform initial checks and detect typos on the client side without having to make a request to the server.
This is where the Luhn Algorithm (created by Hans Peter Lun of IBM) comes in handy, which allows you to check the validity of the card in this way:
An interesting puzzle for beginners from CS50 (Harvard).
Task: draw a pyramid from the Mario (mario-less.c) game using # symbols (lattice or hash). The user is asked: “How many blocks to make a pyramid from?”, he enters a number and the pyramid is drawn, right-aligned, like this:
# ## ### #### ##### ###### #######
igroglaz solution: Continue reading
The meaning of this construct is that what is inside the curly braces is executed first, and then the while() condition is checked. If the condition is true, then the loop is repeated, if false, then the loop ends. Thus, the loop will be executed at least once. This loop is used less frequently than the previous two because it is easier for a human to understand a regular while loop to read the condition, rather than looking for the condition at the end of the loop. Continue reading
for(first; second; third);
Inside the parentheses, the semicolon is the delimiter, not the end of the line.
first – any instruction that the machine will execute once at the very beginning of the loop, for example, you can initialize a variable.
Thus, you can create a file on your computer with the html or htm extension, in which you can prepare the following code:
A loop is a piece of code that repeats a certain number of times. The number of repetitions or the number of iterations is described with a counter.
The loop is written with while(an expression that is converted to a boolean type -> for the next iteration, you need to get true, as soon as we get false, the loop will stop);
An example of an infinite loop: while (true);
Everything else is true.
Data output is different depending on the interpreter:
var cat = “vera”; //cat’s name
document.write(‘Hi, ‘ + cat); print the result to the user
print(‘Hi, ‘ + cat); //one output in the Visual console, but in the browser this function starts printing the page on the printer
console.log(‘Hi, ‘ + cat); //another output option, works in the Google Chrome console
Hi, Vera //result
How to use external file in code?
A comment is a piece of code that is not interpreted. There are multi-line comments and single-line comments.
Multiline comment starts with /* and ends with */
for a few
A single line comment starts with // and goes to the end of the line
Comments in the code are needed for developers who may want to fix or complete something in the code. It is important to leave comments along the code so that users and other developers can easily understand the code.
It is customary to declare variables using the var keyword, i.e. each new variable must be written on a separate line: var cat;
Comparison operator > / <
If we write 2 < 6 , we thus ask the question, is it true or not? The answer can be in the form of boolean values – True / False.
true > false
Equality operator (soft equals) ==
We ask, 6 == 6 (six equals six), the answer will be true
We cannot compare variables of different types, that is, we cannot compare a number and a string. If we write 6 == “6” then we are asking if the number six is equal to the string six. In this case, we will also get true, because the string will be converted to a number.