programming practice - seoul national...

35
Programming Practice 2019-09-18 Week 3

Upload: others

Post on 01-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Programming Practice2019-09-18

Week 3

Page 2: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

NoticeSubmission Deadline, Servers, Accounts, etc.

Page 3: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Submission Deadline- Until the following Lecture day 15:30 (not lab session)- ex) This week’s assignment - until 23rd September 15:30- We do not accept late submission, so try to finish it during the lab session

Page 4: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Connecting to server via SSHFor CS department students (주전공/복수전공/부전공): martini.snucse.org

Other students: pp1.snucse.org

You should have a working account for one of these servers.

If you still don’t have an account or cannot connect to server, contact TA as soon as possible.

Page 5: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Connecting to server via SSH (타과생 전용)If you got your account credentials via email, then make sure to change your password

Enter server via SSH and execute the following command in the shell:

$ passwd

Enter a good password (at least 10 characters)

Page 6: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Practice LectureData Type, ASCII Code, I/O Redirection, EOF, ...

Page 7: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Data Type● C language has some primitive types such as

● Character: char, signed char, unsigned char● Integer: (unsigned or signed) short, int, long, and long long● Floating-point number: float, double, long double

Page 8: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Data Type - Character or Integer

Page 9: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Data Type - Floating-point

Page 10: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Data Type - Type Conversion● For binary operations with operands of different types,

the “lower” type is promoted to the “higher” type before operation proceeds.● For assignment operations, the value of the right side is converted to the type

of the left, which is the type of the result● Type Casting: Explicit Conversion

○ int a=3;printf(“%f\n”, (float) a/2);

Page 11: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

ASCII Code● A character encoding-scheme● Each character constant has its corresponding integer value● No particular relationship between the value of the character constant

representing a digit and the digit’s intrinsic integer value● ‘2’ != 2● ‘2’ == 50

Page 12: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

ASCII Code

Page 13: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection● I/O = Input / Output● To redirect standard input/output/error to a file● You can use file content as the input for a program

or save the output of a program as file content● You can reuse input and output

Page 14: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection● Usage example

○ $ ./program < input○ $ ./program > output○ $ ./program < input > output

* ‘$’ sign means terminal (do not type ‘$’)

Page 15: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection

Page 16: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection

Page 17: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection

Page 18: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection

Page 19: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection Practice

$ gcc adder.c -o adder

$ ./adder

1 3

4

#include <stdio.h>

int main() {

int a, b;

scanf(“%d %d”, &a, &b);

printf(“%d\n”, a+b);

return 0;

}

adder.cshell (terminal)

adder

1 3 4

Page 20: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection Practice

$ gcc adder.c -o adder

$ ./adder < input.txt

4

#include <stdio.h>

int main(){

??

}

adder.cshell (terminal)

adder

1 3 4

intput.txt

1 3

Page 21: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

I/O Redirection Practice

$ gcc adder.c -o adder

$ ./adder > output.txt

1 3

#include <stdio.h>

int main(){

??

}

adder.cshell (terminal)

adder

1 3 4

output.txt

4

Page 22: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

End of File (EOF)● a symbolic constant that stands for End of file● a condition where no more data can be read from a data source

○ file or stream● In terminal, we can <control +d > as EOF

Page 23: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

End of File (EOF)● a symbolic constant that stands for End Of File● a condition where no more data can be read from a data source

○ file or stream● In terminal, we can <control +d > as EOF

● In C, we can check whether EOF comes○ while ( scanf(“%d”, &a) != EOF)

or

○ while ( (c= getchar()) != EOF)

Page 24: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

EOF Practice

$ gcc adder.c -o adder

$ ./adder

3 10 2 16 8

[ctrl + d]

39

$

#include <stdio.h>

int main(){

int a, s=0;

while(scanf(“%d”, &a) != EOF){

s+=a;

}

printf(“%d\n”, s);

return 0;

}

adder.cshell (terminal)

Page 25: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

EOF Practice

$ gcc adder.c -o adder

$ ./adder

3 10 2 16 8

[ctrl + d]

39

$

#include <stdio.h>

int main(){

int a, s=0;

while(scanf(“%d”, &a) != EOF){

s+=a;

}

printf(“%d\n”, s);

return 0;

}

adder.cshell (terminal)

Page 26: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

EOF Practice

$ gcc adder.c -o adder

$ ./adder < input.txt

39

$

#include <stdio.h>

int main(){

int a, s=0;

while(scanf(“%d”, &a) != EOF){

s+=a;

}

printf(“%d\n”, s);

return 0;

}

adder.cshell (terminal)

3 10 2 16 8input.txt

Page 27: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

EOF Practice

$ gcc adder.c -o adder

$ ./adder < input.txt > output.txt

$

#include <stdio.h>

int main(){

int a, s=0;

while(scanf(“%d”, &a) != EOF){

s+=a;

}

printf(“%d\n”, s);

return 0;

}

adder.cshell (terminal)

3 10 2 16 8input.txt

39output.txt

Page 28: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Now,It’s your turn

Page 29: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Problem.

Description

Input

Output

Sample

Data-types Practice1

Let’s see how data types work in the C language! Copy and submit the following exact program that prints results of some arithmetic operations. Use the following exact code. Please make sure you understand why the code prints this output.

Exactly same as the sample output

2

4

2.333333 4

2.333333

8

2.333333 8

141006540 1000000000

8

130 130 4 1

A a

2 98 50

None.

Page 30: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Problem.

Description

Input

Output

Sample

Data-types Practice1

Let’s see how data types work in the C language! Copy and submit the following exact program that prints results of some arithmetic operations. Use the following exact code. Please make sure you understand why the code prints this output.

Exactly same as the sample output

2

4

2.333333 4

2.333333

8

2.333333 8

141006540 1000000000

8

130 130 4 1

A a

2 98 50

None.

Page 31: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Problem. No.2

Write a program that calculates the average of given integers.

Any number of integers maybe given as input_you must continuously get input until EOF.

It is Guaranteed that at most 1000 integers will be given, and the absolute value of each given integer will be less than or equal to 100000.

[Input]1 2 3 4

[Output]

2.500000

[Input]

4 2 1

[Output]

2.333333Print the average of given integers. Absolute error is allowed up to 10^-6

A single line with any number of integers

Description

Input

Output

Sample

Average

Page 32: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Problem. No.3

Write a program that gets any number of characters as input, and convert any occurrence of upper-case letters to lowercase letters and any occurrence of lower-case letters to upper-case letters.

Don’t do anything for non-alphabet characters.

For instance, there may be white spaces included in the input – just leave them as whitespaces.

Print the same line as input, except convert all upper-case letters to lower-case and all lower-case letters to upper-case.

[input]

abcd123 ABCDEF

[output]

ABCD123 abcdef

A single line with any number of characters. It is guaranteed that every character is of ASCII table

Description

Input

Output

Sample

Character Conversion

Page 33: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Caesar CipherProblem. No.4

The Caesar cipher is the one of simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a right shift of 4, A would be replaced by E, H would become L, and so on.

It is guaranteed that there is only a right of shift with a positive number. And string to be encrypted consists of the capital characters without any white spaces.

The encrypted string (only consists of the capital letter)

[Input]

4HAPPY

[Output]

LETTC

At the first line, there is the number N for shifting to right (0 <= N <= 25). At the next line, there is the string to be encrypted (only consists of the capital letter)

Description

Input

Output

Sample

The negative input (-25 <= N <= 25) is your optional self challenge!Input: -3 ABOutput: XY

Notice: You should filter input characters by using if statement which examine that input characters are the capital characters because of input buffer problem.

Page 34: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

FAQ

Page 35: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2019. 9. 18. · Programming Practice 2019-09-18 Week 3. Notice Submission

Q1. I worked on my martini (or other server). Can I copy the files to my computer?

A1. Yes, you can use scp (shell copy)

scp {id@server:path} {localpath}example: scp [email protected]:~/week3/pa1.c ~/week3/pa1.c

You can also copy local file from your computer to other server vice versascp {localpath} {id@server:path} example: scp ~/week3/pa1.c [email protected]:~/week3/pa1.c

Q2. My vim screen suddenly freezed. What happened?

A2. One possible cause: “Ctrl+s” freezes vim. To unfreeze type “Ctrl+q”