0% found this document useful (0 votes)
42 views13 pages

Homework #4-2

Uploaded by

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

Homework #4-2

Uploaded by

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

Homework #4

C Programming

Handong Global University


Mission

◼ Solve problem 1~3

◼ Submission
◼ Submit a hw4_<student_ID>.zip file containing
hw4_1_<student_ID>.c, hw4_2_<student_ID>.c, and
hw4_3_<student_ID>.c on LMS.
◼ Source files hw4_*.c should contain the followings:
 Algorithm in pseudo code as comments preceded by “//”.
 See the next page.
 C program code that implements the pseudo code
cf. Submit only source files (.c files)
◼ Compile with –Wall option
e.g.) gcc my_code.c –Wall
◼ The code should be properly indented.

◼ Due date: PM 11:00, Oct. 23th

Handong Global University


Code Example (add.c)

#include <stdio.h>

int main()
{
int a = 0, b = 0;
int sum = 0;

// read two integer numbers


printf("Input two numbers: ");
scanf("%d %d", &a, &b);

// add the two numbers


sum = a + b;

// print the result


printf("%d + %d = %d\n", a, b, sum);

return 0;
}

Handong Global University


Indentation is Crucial for Readability

◼ Properly indented code (Good) ◼ Not indented code (Bad)

int main() int main()


{ {
int height = 0; int height = 0;
int i = 0, j = 0; int i = 0, j = 0;

do { do {
printf("Input the height of triangle: "); printf("Input the height of triangle: ");
scanf("%d", &height); scanf("%d", &height);
} while (height % 2 == 0); } while (height % 2 == 0);

for(i = 1; i <= height; i++){ for(i = 1; i <= height; i++){


int start = height - i; int start = height - i;
int end = height + i - 1; int end = height + i - 1;

for(j = 0; j < start; j++) for(j = 0; j < start; j++)


putchar(' '); putchar(' ');
for(; j < end; j++) for(; j < end; j++)
putchar('*'); putchar('*');
putchar('\n'); putchar('\n');
} }

return 0; return 0;
} }

Handong Global University


Rules of Indentation

◼ Start function header from the first column


◼ Use tab to represent indentation level.
◼ The body of functions, blocks, selection (if, switch),
or repetition (while, for, do-while) statements should
be indented one more level.

Ex) if(x % 2 == 0)
printf(“x is an even number.\n”);

Ex) do {
printf(“Input a positive number:”);
scanf(“%d”, &x);
} while(x <= 0);

Handong Global University


Honor Code Guidelines (English)

◼ “Assignment”
◼ Assignments are an educational activity necessary to fully understand
the lecture, and to apply the materials to practical problems. Students
should complete all assignments with honesty and sincerity to develop
the knowledge and skills intended in the assignment.
◼ Submitting assignments or program codes written by others or acquired
from the internet without explicit approval of the professor is regarded
as cheating.
◼ Showing or lending one’s own homework to other student is also
considered cheating that disturbs fair evaluation and hinders the
academic achievement of the other student.
◼ It is regarded as cheating if two or more students conduct their
homework together and submit it individually when the homework is not
a group assignment.
◼ It can be suspected or regarded as cheating if the similarity between
assignments submitted by different students is beyond an acceptable
degree that can be considered as a coincidence or when the student
is not able to explain in detail about their homework.

Handong Global University


Honor Code Guidelines (Korean)

◼ “과제”
◼ 과제는 교과과정의 내용을 소화하여 실질적인 활용 능력을 갖추기
위한 교육활동이다. 학생은 모든 과제를 정직하고 성실하게 수행함
으로써 과제에 의도된 지식과 기술을 얻기 위해 최선을 다해야 한
다.
◼ 담당교수가 명시적으로 허락한 경우를 제외하고 다른 사람이 작성하
였거나 인터넷 등에서 획득한 과제물, 또는 프로그램 코드의 일부,
또는 전체를 이용하는 것은 부정행위에 해당한다.
◼ 자신의 과제물을 타인에게 보여주거나 빌려주는 것은 공정한 평가를
방해하고, 해당 학생의 학업 성취를 저해하는 부정행위에 해당한다.
◼ 팀 과제가 아닌 경우 두 명 이상이 함께 과제를 수행하여 이를 개별
적으로 제출하는 것은 부정행위에 해당한다.
◼ 서로 다른 학생이 제출한 제출물간 유사도가 통상적으로 발생할 수
있는 정도를 크게 넘어서는 경우, 또는 자신이 제출한 과제물에 대
하여 구체적인 설명을 하지 못하는 경우에는 부정행위로 의심받거
나 판정될 수 있다.

Handong Global University


Problem 1

◼ Peer review of hw3_1 solution in study group


◼ [Review Together] In study group, review the solutions of
other members and share comments to each other. Freely
discuss to improve the solutions of all members.

◼ [Update Independently] Each member updates the solution of


hw3_1 individually applying the comments of the group mates.
 Each member should update her/his solution for herself/himself

◼ Submit the updated solution as the solution of hw4_1.


 Mark the modified parts by comments as the next page.
 If you believe your previous solution was perfect, submit it again
and put a comment “My previous solution was perfect, so I didn't
modify it!” in the first line.

Handong Global University


Problem 1

◼ Example of updated solution.

int main()
{
int i = 0;

// previous code
/*
<old code>

*/

// updated code
<new code>

return 0;
}

Handong Global University


Problem 2

◼ Read numbers from the user until the user inputs EOF characters counting
the number of input data in an integer variable.
◼ Then, display the k-largest numbers.

Example)
input a number (type EOF to finish): 3 // the user types numbers
input a number (type EOF to finish): 1
input a number (type EOF to finish): 4
input a number (type EOF to finish): 1
input a number (type EOF to finish): 5
input a number (type EOF to finish): 9
input a number (type EOF to finish): 2
input a number (type EOF to finish): 6
input a number (type EOF to finish): 5
input a number (type EOF to finish): 3
input a number (type EOF to finish): ^Z // EOF characters (CTRL-z or CTRL-d)
How many large numbers: 4 // the user typed 4
Largest numbers: 9 6 5 5 // the four largest numbers

Handong Global University


Reading Input Until EOF

◼ Read this program and learn how to read multiple values until the user types the EOF character (CTRL-z on
Windows or CTRL-d on MAC)

#include <stdio.h>

int main()
{
int data[100] = { 0 }; // array declaration
int n = 0; // # of input data

int ret = 0;
do {
// On success, scanf() returns a positive number, which is the number of successful input values.
// When the user types EOF, scanf() returns EOF code (-1).
// EOF is CTRL-z on Windows, or CTRL-d on UNIX(incl. MAC OS)
// To input EOF, type CTRL-z (or CTRL-d) and press the Enter key.
printf("input a number (type EOF to finish): ");
ret = scanf("%d", &data[n]);

printf("ret = %d, data[%d] = %d\n", ret, n, data[n]);


if(ret != EOF) // to ignore invalid input
n++;
} while(n < 100 && ret != EOF);

// display all input numbers


for(int i = 0; i < n; i++)
printf("data[%d] = %d\n", i, data[i]);

return 0;
}

Handong Global University


Problem 2

◼ Algorithm
◼ Read numbers until the user types EOF. (maximum 100
numbers)
 Store the input numbers in an array of size 100
◼ Read k in range [1, n] using do-while statement
 If the user types an invalid number, read again and again.
◼ Place the largest numbers at the beginning of the array
Repeat for i from 0 to k
Find the index of the largest number in range [i, n),
i.e., i <= j < n
Exchange that number with the i-th element.
◼ Display the first k elements.

Handong Global University


Problem 3

◼ Write a C program that show shooting star.


◼ Read horizontal and vertical speed (dx and dy).
◼ Move the star on the screen using the functions in
Console.c and Console.h.
◼ Refer to the demo video clip.
◼ Read the skeleton code carefully and extend it to complete
your solution.

shooting star

Handong Global University

You might also like