Computer Programming II Assignment 4



CS 1713
Introduction to Computer Programming II
Assignment 4

1. (100 pts) Write a program to find the k largest elements of a file. Allocate an array of size
k and while you read the numbers from the file, store the k largest numbers in the array.
When you read the next element from the file, find if the array needs to be modified or not.
Consider the below figure as an example. Assume that next element read is 80. Since 80 is
larger than the smallest element, we need to shift elements < 80 to the right by 1 position
and create space for 80. We then insert 80 into the new position.
0 1 2 3 4
97 83
0 1 2 3 4
97 83
0 1 2 3 4
97 83 76 65 54
76 65
80 76 65
Figure 1: Insert 80 into 5 largest elements
In main() use argc and argv to read the filename and k from the user and compute and print
the k largest elements. Name your program assign4.c Sample execution of the program is
given below. First parameter is filename and second parameter is k. You need to use atoi()
in stdlib.h to convert strings to integer.
fox01> assign4 a.txt 5
996 980 956 932 929
Submit your program electronically using the blackboard system
The program you submit should be your own work. Cheating will be reported to office of academic
integrity. Both the copier and copiee will be held responsible.


There are no reviews yet.

Be the first to review “Computer Programming II Assignment 4”

Your email address will not be published.