๊ต์๋ ๋ค์ ๋ค์ด๋ ๋์ ์ ๋ง ๋๋ฌด ์ ํํ์๋ค
์ ํ๋ธ ์๋ง ์๋ ์ผ๊ธฐํ๋ฉด ์ปดํจํฐ๊ฐ ์์ฃผ ์ ๋ฐ์์ธ ๊ฒ ๊ฐ์ ๊ทธ๋ฐ ๋ฐ์
ํ๊ธฐ:
์์ธ์ด๋ฅผ ์ด๋ค๊ณ ๊ฐ์ .
์์ฃผ ๊ธธ๊ณ , ๋ฐ๋ณต์ ์ธ ์์ธ์ด๋ฅผ ์ด๋ค๊ณ ํด๋ณด์. -> ๋ด์ฉ์ด ์ข๋ค๊ณ ํด๋ ์ฅํฉํ๋ฉด ์ข์ ์ ์ X์ฒ๋ผ
์ปดํจํฐ๋ ์ฐ๋์์ธ ์ฝ๋๊ฐ ํ์ํ๋ค.
3 access to write good code:
์์ธ์ด์์ ์ข์ puctuation, indenting ๋ฑ๊ณผ ๊ฐ๋ค. ์ฝ๋์คํ์ผ์ด ์ ์ฝํ๋๋ก ํ๋ ๊ฒ ์ค์ํ๋ค.
์ง๋ ์ฃผ ์ฐ๋ฆฌ๋ ์คํฌ๋์นํด์ ์ผ๋๋ฐ,
์ด์ ๋ ํค๋ณด๋ ์ธ๊ฒ.
CS50 IDE(Intergrated Developmnet Environment) ์ธ ๊ฒ.
ํด๋ผ์ฐ๋ ํด์์ ์.
ide.cs50.io
๋ฐฉ๋ฌธํ์ธ์!
์คํฌ๋ฆฐ ์์๋ ๋ธ๋์ด๊ณ , ๊ฑฐ๊ธฐ๋ ์ฝ๋ ์ธ ๊ฒ.
์๋๋ถ๋ถ์ terminal window์ธ๋ฐ, command run ํ ์ ์๋ ๊ณณ.
==> ์ด๊ฑด ์์ฃผ ์ปค๋จผํ ํ๋ก๊ทธ๋จ ์๋ํฐ์ ํ์์
ํ์ผ - ๋ดํ์ผ - save - hello.c ๋ก ์ ์ฅ
C ํ๋ก๊ทธ๋จ์ .c๋ก ๋๋๋ค.
์ผ๋จ ๋ญ์ง ๋ชจ๋ฅด์ง๋ง ์ผ๋จ ์น๋ค.
์์ ๋ญ ๋ง์ด ์น๊ธด ํ์ง๋ง, ๊ทธ๋ฅ ํ๋ค. ์คํ์ํค๋ค.
์ฐ๋ฆฌ ์๋๋ ํญํ์์ ์์ดํฐ์์ ์คํ์ํค๋ ค๋ฉด, ๊ทธ๋ฐ๋ฐ ์ฐ๋ฆฌ๋ ์๋์ผ, ํค๋ณด๋ ์จ์ผํด.
GUI, ์์ด์ Command line interface๋ก ์๋์์ผ.
์ปดํจํฐ๊ฐ ์ ์ผํ๊ฒ ์ดํดํ ์ ์๋ ์ธ์ด๋ ๋ญ์ง?
binarary, 1์ 0๋ง ์ฌ์ฉํ๋.
๋ฐ๋ผ์ ์ฐ๋ฆฌ๊ฐ ์น ์์ด ์ฝ๋๋ค์ ์ปดํจํฐ๋ ์ดํด ๋ชปํ๊ณ , ๊ทธ๋์ ์ด๊ฑฐ 0์ 1๋ก ๋ฐ๊ฟ์ผ ํ๋ค.
๊ทธ๋ ๋ค๋ฉด ์์ค์ฝ๋๋ฅผ ๋จธ์ ์ฝ๋(0, 1) ๋ก ์ด๋ป๊ฒ ๋ฐ๊ฟ๊น?
์ปดํ์ผ๋ฌ๋ผ๋ ํ๋ก๊ทธ๋จ์ด ํ์ํ๋ค.
์ฐ๋ฆฌ๋ ์ฌ๊ธฐ์ make๋ผ๊ณ ํ๊ฒ ๋ค.
make hello๋ฅผ ์น๋ฉด -- ์ด์ํ๊ฒ ๋์ค๋ฉด์ -- ๋คํํ ๋นจ๊ฐ ๋ ธ๋ ์๋ฌ๋ ์๋จ๊ณ -- ./hello๋ฅผ ์ณ๋ณด๋ฉด, go into the computer and look for the program hello์ ์๋ฏธ.
function์ ์๋ฏธ: ์ก์ ,
์ธํ to function = parameter or argument
c์์ ๋ญ ์ฐ๊ณ ์ถ์ผ๋ฉด printf( )๋ฅผ ์ด๋ค. f๋ formated์ ์ค๋ง. ๊ทธ๋ฆฌ๊ณ ๋ฐ์ดํ๋ ๋ง์ง๋ง์ ์ธ๋ฏธ์ฝ๋ก ๋.
printf("Hello, world!");
์๊ท๋จผํธ(์ธํ)์ function์ ๋ฃ์ผ๋ฉด side effects๋ฅผ ๊ฐ์ ธ์จ๋ค.
say๋ธ๋ก๊ณผ ask ๋ธ๋ก์ ์ฐจ์ด
๋ผ์ด๋ธ๋ฌ๋ฆฌ:
string : ์ปดํจํฐ์ฌ์ด์ธ์ค์ ํ ์คํธ์.
string answer = get_string("What is your name?");
= : assignment ๋ผ๋ ์๋ฏธ. ์ค๋ฅธ์ชฝ์ ๊ฒ์ ์ผ์ชฝ์ผ๋ก ์ฎ๊ธด๋ค.
์ธ๋ฏธ์ฝ๋ก ๊ฐ์๊ฑฐ ๋นผ๋จน์ ์ ์๋ค. ๊ทธ๋ฌ๋๊น ๋๋ฌด ์ข์ ํ์ง ๋ง๊ณ , ํ์ ๋ฑ์ ์ดํดํ์ธ์.
์ฝ๋์ ๋ญ ๋ฐ๋๋ฉด ํญ์ ๋ฆฌ์ปดํ์ผ ์์ง๋ง๊ณ ! make hello
ํ๋ก๊ทธ๋จ์์ variable์์๋ discriptive variable ๋ฅผ ์ ํ๋ ๊ฒ์ ์ถ์ฒํ๋ค.
x,y,z ๋ ์ํ ์ธ๊ณ์์ ๋ง์ด ์ฐ์ง๋ง ์ค์ ์ธ๊ณ๋ก ์ค๋ฉด ์๋ฌด ๊ฒ๋ ์๋๊ฒ ๋๋๊น!
์ฝ๋ ์ค๊ฐ์์ ์ํฐ์น๋ฉด ๋ ํท๊ฐ๋ฆด ๊ฒ. ๊ทธ๋์ ์ค ๋ฐ๊ฟ ๋์๋ \n์ ์น๋๋ก ํ๋ ด
์ ๋ฌด์จ ๊ณผ์ ์ ์ถํ๋๊ฑฐ ๋๋ฌด ์ด๋ ค์์ ๋ง ํ๋ค๊ฐ
๋น๋ฐ๋ฒํธ๋ฅผ ๊ฐ?๋กํ๊ฑฐ๊ฐ์ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ์ด์ด์๋ค.. ๋น๋ฒ ๊น๋จน์ง ๋ง๋ผํ๋๋ฐ
Git์ผ๋ก ์ ์ถํ๋ ๋ฐฉ๋ฒ - SSH ํ์ฉ
์ฐ์ฌ๊ณก์ ๋์ ์ฌ๋ฆฐ ๊ณผ์ ๊ฐ ์๋์ผ๋ก ์ฌ๊ธฐ ์ฌ๋ผ๊ฐ๋๊ฑฐ ๋ณด๋๊น ๋๋ฌด ๊ฐ๊ฒฉ์ด๋ค ใ ใ -->
๊ณผ์ ์ ์ถํ๋์ง ํ์ธํ๋ ๋งํฌ: https://cs50.me/cs50x
header files
<stdio.h> == standard input output
give more functionality.
help50
ํ๋ก๊ทธ๋จ์ ๋ง์น ์ ์๋ ๋ฐฑ๋ง๊ฐ์ง๊ฐ ์๋๋ฐ(์ธ๋ฏธ์ฝ๋ก ์๋ฃ๋๋ค๋ ์ง, ํ์ดํฌ ๋ฑ๋ฑ)
์ด๋ฐ๊ฑฐ์์ ๋ ๋์์ค ์ ์๋ ํด์ด ์์ด
trouble shoot problem
์๋ฅผ ๋ค์ด stadio.h ์์ผ๋ค๊ณ ํด๋ด.
๋ด ์ปดํจํฐ๋ printf๊ฐ ๋ญ์ง ๋ชจ๋ฅผ๊ฑฐ์ผ. ๊ทธ๋ผ ๋ด ์ฝ๋๋ณด๋ค ๊ธด ์๋ฌ ๋ฉ์์ง๊ฐ ๋ธ
ํจํด์ ํ์ ํด๋ณด๋๋ก ํ์.
์ ์ผ ์ค์ํ๊ฑด. ๋ฉ์ค์ ์ด ๋๋ฉด, ๊ฒฐ๊ณผ ๊ฐ์ฅ ์ ์ค์ ๋๊ฐ ๋ฉ์ค์ ํ ์ค ๋๋ฒ๊ฐ ๋ฐ ๊ฒ.
๋ฌด์จ ๋ง์ธ์ง ๋ชจ๋ฅด์ง๋ง, ์ด์ help50 ํด์ ์จ์ ํด๋จผ ํ๋ ๋ค๋ฆฌํ ์ด๋๋ฐ์ด์ค๋ฅผ ๋ฐ์๋ณด๋๋ก ํ์.
ํฐ๋ฏธ๋ ์๋์ฐ์ help50 make hello ๋ฅผ ์จ๋ณด๋๋ก ํ์.
๊ทธ๋ผ ํด๋จผ ํ๋ ๋ค๋ฆฌํ ์ธ์ด๋ก ๋์ ์๋ชป์ ๋งํด์ค ๊ฒ์ด์ผ.
๋ printf ๊ฐ ์ ์ธ๋์๋๋ฐ stdio.h ๋นผ๋จน์๋? ์ด๋ฐ์์ผ๋ก.
style50
์ํฐ ์์ฐ๊ณ ํ ์ค์ ์ฝ๋ ๋ค์ฐ๋๊ฑด ๋ ์์ ๋ฏธ๋์ด์ ํ์ธ์์ด์ ์์ด๊ฑฐ๋ ๋๊ฐ์ผ๋๊น ๋ฐท์คํ์ผ.
๋ชป์๊ฒผ์์.
style50์ ๊ทธ๋ผ ์คํ์์ผ๋ณด์ ์ฝ๋์ ์คํ์ผ์ ๋ง๋ค์ด์ค!
style50 hello.c๋ผ๊ณ ํ๋ฉด ์ฝ๋ ์ ๋ ฌ ํด์ค
//๋ ์ฝ๋ฉํธ๋ฅผ ์ํ ๊ฒ.
check50
๋์ ์ฝ๋๊ฐ ๋ง๋์ง ์๋ ค์ค๋ค.
ํ๋ก๋ธ๋ผ ์ ๊ณผ ๋ฉ์ ๋ฐ๋ผ, ์ด๋ป๊ฒ ์ณ ํด์ผํ๋์ง ๋ฐ๋ผ
check50 cs50/problems/hello
ํ๋จผ ์ ๋ก๋ ํ๊ณ ,
git์ ์คํ๋์ด๋ฆ ๋น๋ฐ๋ฒํธ ์ฐ๊ณ
์ ๋ก๋ฉํ๊ณ .... --> ๊ธฐ๋ค๋ฆฌ๋ฉด --> ์๋ํ๋ค.
green smile faces๋ก ์๋ต
red frown face๋ฉด ์๋ชปํ๊ฑฐ์ผ ใ _ใ
yello face = we weren't' able to run the test
CS50 IDE's file browser
hello๋ฅผ ๋๋ฅด๋ฉด ๋นจ๊ฐ๊ณผ ๋ท ๋ค์ ์ฐ์
์? missed interpreter.
๊ฑ ๋ซ๊ณ hello.c ๋๋ฅด์ธ์
์ 7๋ถ์ ํด์ ํ ๋ค์ ๋์์ต๋๋ค.
CS50 IDE's command-line interface
ls : list (files and folders in your current folder)
hello* ๋ผ๊ณ ๋์ด์๋ ๊ฒ. executable.
๋ง์ฝ ์ง์ฐ๊ณ ์ถ์ผ๋ฉด
rm hello ํ๊ณ y / yes ์น๋ฉด ์ฌ๋ผ์ง๋ค.
๋ง์ฝ change the program,
mv hello.c goodbye.c --> move the hello.c to goodbye.c(rename)
๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ค๊ณ ์ถ์ผ๋ฉด
mkdir (make directory)
mkdir lecture ๋ง๋ค๋ฉด lecture ํด๋๊ฐ ๋ง๋ค์ด์ง๋ค.
mv hello.c lecture -->
cd : change directory
parent folder ๋ก ์ฎ๊ธฐ๊ณ ์ถ์ ๋,
mv hello .. (์์ ํด๋๋ก ์ฎ๊น)
cd .. (๋์ ์์น๋ฅผ ์์ ํด๋๋ก ์ฎ๊ฒจ๊ฐ)
rmdir (remove a directory)
-------------------
truncation.c
float : ์์์ ์ผ๋ก ๋ต์ด ๋์ค๋ ๊ฒ.
int๋ก ํ๋ฉด ์์์ ์ดํ์ ๊ฐ์ ์ฌ๋ผ์ง๋ค.
------------------------
์คํฌ๋์น์์ counter์
C์์ int counter = 0;
counter = counter + 1; (whatever the result is on the right, input on the left)
counter += 1;
counter ++;
variable :
Abstraction
์ ์ด๋ค ๋ฌธ์ฅ ๋์๋ ;์ด ์๋๋ฐ, ์๋ ๊ณณ๋ ์๋์?
--> just because, ์ธ์ด๋ฅผ ๋ง๋๋ ์ฌ๋์ด ๊ทธ๋ ๊ฒ ํ๊ธฐ๋ก ํจ.
์ปดํ์ผ๋ฌ๋ ์๋ถํฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์ฝ์ด ๋ด๋ ค๊ฐ.
void ==> ์ ๊ธฐ ์๋ meow๋ผ๋ ํจ์๊ฐ ์์ ์์ ์ธ๋ฐ(์์๋๋ผ๊ณ ), ์์ง ์๋์ผ.
meowํจ์๋ return ์ด ์๊ธฐ ๋๋ฌธ์, void ์ฌ์ฉ.
{} ---> ์ค๊ดํธ๋ curly braces๋ผ๊ณ ์ฝ๋๋ค.
return ์ด ์ ์ ์ฉํ๊ฐ?
positve.C
์ํผ๋ง๋ฆฌ์ค ๊ฒ์
๊ฒ์ ์ ๋น์ฃผ์ผ ? ๋ฐ์ค๋ค C์์๋ ????๋ก ํ๋ค.
mario.c
๋ค๋ฅดํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
๊ณผ์ :
Toward the end of World 1-1 in Nintendo’s Super Mario Brothers, Mario must ascend right-aligned pyramid of blocks, a la the below.
Let’s recreate that pyramid in C, albeit in text, using hashes (#) for bricks, a la the below. Each hash is a bit taller than it is wide, so the pyramid itself is also be taller than it is wide.
#
##
###
####
#####
######
#######
########
The program we’ll write will be called mario. And let’s allow the user to decide just how tall the pyramid should be by first prompting them for a positive integer between, say, 1 and 8, inclusive.
Here’s how the program might work if the user inputs 8 when prompted:
$ ./mario
Height: 8
#
##
###
####
#####
######
#######
########
Here’s how the program might work if the user inputs 4 when prompted:
$ ./mario
Height: 4
#
##
###
####
Here’s how the program might work if the user inputs 2 when prompted:
$ ./mario
Height: 2
#
##
And here’s how the program might work if the user inputs 1 when prompted:
$ ./mario
Height: 1
#
If the user doesn’t, in fact, input a positive integer between 1 and 8, inclusive, when prompted, the program should re-prompt the user until they cooperate:
$ ./mario
Height: -1
Height: 0
Height: 42
Height: 50
Height: 4
#
##
###
####
๐ก ๋์ ๊ณผ์ ํด์ค:
mario.c
#include <cs50.h>
#include <stdio.h>
int get_positive_int(string prompt);
int main(void)
{
int number = get_positive_int("Height: ");
for (int height = 0; height < number; height++)
{
for (int dots = number - height - 2; dots >= 0; dots--)
{
printf(" ");
}
for (int row = 0; row <= height; row++)
{
printf("#");
}
printf("\n");
}
}
int get_positive_int(string prompt)
{
int number;
do
{
number = get_int("%s", prompt);
}
while (number < 1 || number > 8);
return number;
}
๋ฌธ์ 2.
Cash
Greedy Algorithms
When making change, odds are you want to minimize the number of coins you’re dispensing for each customer, lest you run out (or annoy the customer!). Fortunately, computer science has given cashiers everywhere ways to minimize numbers of coins due: greedy algorithms.
According to the National Institute of Standards and Technology (NIST), a greedy algorithm is one “that always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.”
What’s all that mean? Well, suppose that a cashier owes a customer some change and in that cashier’s drawer are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢). The problem to be solved is to decide which coins and how many of each to hand to the customer. Think of a “greedy” cashier as one who wants to take the biggest bite out of this problem as possible with each coin they take out of the drawer. For instance, if some customer is owed 41¢, the biggest first (i.e., best immediate, or local) bite that can be taken is 25¢. (That bite is “best” inasmuch as it gets us closer to 0¢ faster than any other coin would.) Note that a bite of this size would whittle what was a 41¢ problem down to a 16¢ problem, since 41 - 25 = 16. That is, the remainder is a similar but smaller problem. Needless to say, another 25¢ bite would be too big (assuming the cashier prefers not to lose money), and so our greedy cashier would move on to a bite of size 10¢, leaving him or her with a 6¢ problem. At that point, greed calls for one 5¢ bite followed by one 1¢ bite, at which point the problem is solved. The customer receives one quarter, one dime, one nickel, and one penny: four coins in total.
It turns out that this greedy approach (i.e., algorithm) is not only locally optimal but also globally so for America’s currency (and also the European Union’s). That is, so long as a cashier has enough of each coin, this largest-to-smallest approach will yield the fewest coins possible. How few? Well, you tell us!
Implementation Details
Implement, in a file called cash.c in a ~/pset1/cash directory, a program that first asks the user how much change is owed and then prints the minimum number of coins with which that change can be made.
- Use get_float to get the user’s input and printf to output your answer. Assume that the only coins available are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢).
- We ask that you use get_float so that you can handle dollars and cents, albeit sans dollar sign. In other words, if some customer is owed $9.75 (as in the case where a newspaper costs 25¢ but the customer pays with a $10 bill), assume that your program’s input will be 9.75 and not $9.75 or 975. However, if some customer is owed $9 exactly, assume that your program’s input will be 9.00 or just 9 but, again, not $9 or 900. Of course, by nature of floating-point values, your program will likely work with inputs like 9.0 and 9.000 as well; you need not worry about checking whether the user’s input is “formatted” like money should be.
- You need not try to check whether a user’s input is too large to fit in a float. Using get_float alone will ensure that the user’s input is indeed a floating-point (or integral) value but not that it is non-negative.
- If the user fails to provide a non-negative value, your program should re-prompt the user for a valid amount again and again until the user complies.
- So that we can automate some tests of your code, be sure that your program’s last line of output is only the minimum number of coins possible: an integer followed by \n.
- Beware the inherent imprecision of floating-point values. Recall floats.c from class, wherein, if x is 2, and y is 10, x / y is not precisely two tenths! And so, before making change, you’ll probably want to convert the user’s inputted dollars to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up!
- Take care to round your cents to the nearest penny, as with round, which is declared in math.h. For instance, if dollars is a float with the user’s input (e.g., 0.20), then code likewill safely convert 0.20 (or even 0.200000002980232238769531250) to 20.
-
int cents = round(dollars * 100);
Your program should behave per the examples below.
$ ./cash
Change owed: 0.41
4
$ ./cash
Change owed: -0.41
Change owed: foo
Change owed: 0.41
4
๐ก ๋์ ๊ณผ์ ํด์ค:
cash.c
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
float change;
do
{
change = get_float("How much change is owned?: "); // asking how much change is owned
}
while (change < 0);
int convertCents = round(change * 100); // convert decimal to integer
int coins = 0;
while (convertCents >= 25) // coin counts for quarter
{
convertCents = convertCents - 25;
coins ++ ;
}
while (convertCents >= 10) // coin counts for dime
{
convertCents = convertCents - 10;
coins ++;
}
while (convertCents >= 5) // coin counts for nickel
{
convertCents = convertCents - 5;
coins ++;
}
while (convertCents >= 1) // coin counts for penny
{
convertCents = convertCents - 1;
coins ++;
}
printf("%d\n", coins);
}
๊น์์ ์์ฃผ ์น์ ํ๊ฒ ๋์ ์ฝ๋๊ฐ ์ ์๋ํ๋์ง,
์คํ์ผ์ ์ ๋ง์ถฐ์ ์์ฑํ๋์ง ๊ฒํ ํด์ฃผ๋๋ฐ,
์คํ์ด์ค๋ ๊ดํธ ์์น๊ฐ ์์์ง ์์ผ๋ฉด ๊ฐ์ฐจ์์ด ๊ฐ์ ์ ํ๋ค.
๊ณ ๋ก cash๋ฅผ 5๋ฒ์ด๋ ์ฌ์ ์ถํ๋ค๋ ์ด์ผ๊ธฐ,,
์ด๊ฒ ๋ ํ๋ ํ๋ ํด๊ฐ๋ ๋ง์ด ์๋ค โบ๏ธ
'Coding > [EdX] CS 50' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Week 4] Memory (0) | 2021.12.06 |
---|---|
[Week 3] Algorithms (0) | 2021.12.05 |
[Week 2] Array (0) | 2021.12.05 |
[Week 0] Scratch (0) | 2021.11.23 |
Introduction to Mathematical Thinking ๋ก ๋ณธ๊ฒฉ ์์ (0) | 2021.10.08 |