Homework #4-2
Homework #4-2
C Programming
◼ 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.
#include <stdio.h>
int main()
{
int a = 0, b = 0;
int sum = 0;
return 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);
return 0; return 0;
} }
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);
◼ “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.
◼ “과제”
◼ 과제는 교과과정의 내용을 소화하여 실질적인 활용 능력을 갖추기
위한 교육활동이다. 학생은 모든 과제를 정직하고 성실하게 수행함
으로써 과제에 의도된 지식과 기술을 얻기 위해 최선을 다해야 한
다.
◼ 담당교수가 명시적으로 허락한 경우를 제외하고 다른 사람이 작성하
였거나 인터넷 등에서 획득한 과제물, 또는 프로그램 코드의 일부,
또는 전체를 이용하는 것은 부정행위에 해당한다.
◼ 자신의 과제물을 타인에게 보여주거나 빌려주는 것은 공정한 평가를
방해하고, 해당 학생의 학업 성취를 저해하는 부정행위에 해당한다.
◼ 팀 과제가 아닌 경우 두 명 이상이 함께 과제를 수행하여 이를 개별
적으로 제출하는 것은 부정행위에 해당한다.
◼ 서로 다른 학생이 제출한 제출물간 유사도가 통상적으로 발생할 수
있는 정도를 크게 넘어서는 경우, 또는 자신이 제출한 과제물에 대
하여 구체적인 설명을 하지 못하는 경우에는 부정행위로 의심받거
나 판정될 수 있다.
int main()
{
int i = 0;
// previous code
/*
<old code>
*/
// updated code
<new code>
return 0;
}
◼ 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
◼ 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]);
return 0;
}
◼ 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.
shooting star