CSCI 151程序设计讲解、C/C++编程调试
- 首页 >> Python编程 CSCI 151: Programming for Scientists and Engineers
Programming Assignment II
Deadline: 17 March 2021, 8:00pm
Explanation for Task1 and Task 2:
You will write programs to handle the games between the wrestlers and maintain their
scores in these tasks. First, create the following struct:
typedef struct{
int id;
char firstName[100];
char lastName[100];
int birthYear;
int score;
} wrestler;
A wrestler is identified by the following features: id (int), first name (char array/string),
last name (char array/string), birth year (int), and score (int).
Task 1 (25 points): Copy&paste the following lines to the beginning of your main
function. Your task is to make your code work and complete it according to the
descriptions given below. (Your main will contain the following lines exactly).
char inputFile[100];
//creates a char array (string)
strcpy(inputFile,"in.txt");
//copies “in.txt” into the string, inputFile
int n = getNumberOfWrestlers(inputFile);
/*retrieves the number of wrestlers in inputFile and assigns it
to variable n*/
wrestler list[n];
/*creates a wrestler array, list, of size n to store wrestlers‘
info*/
init(list, inputFile);
/*initializes list, which is defined above, with the information
in inputFile. You need to create a function named init that
takes 2 parameters suitable to its usage here*/
printScoreBoard(list, n);
/*prints the scoreboard to screen. You need to create a
function named printScoreBoard that takes 2 parameters suitable
to its usage here*/
writeTheChampion(list, n);
/*prints the champion with the maximum score to screen. You
need to create a function named writeTheChampion that takes 2
parameters suitable to its usage here*/
The format of the file “in.txt” is shown below:
Number_of_wrestlers(N)
id1 firstname1 lastname1 birthyear1 score1
id2 firstname2 lastname2 birthyear2 score2
…
idN firstnameN lastnameN birthyearN scoreN
For example, “in.txt” may contain the following lines:
4
118 john brown 2000 100
234 tomas jhonson 2001 150
56 kevin whale 2000 50
7909 michael daisy 1998 200
This means that there are 4 wrestlers in this game. The first wrestler (john brown) with id
118 was born in 2000 and has a score of 100. The second wrestler (tomas jhonson) with
id 234 was born in 2001 and has a score of 150, etc. Then, the above code fragment
initializes the wrestler list array with all this information and prints out the following
output, where the first 5 lines are generated by printScoreBoard and the last 2 by
writeTheChampion.
Printing the score board
118 john brown 2000 100
234 tomas jhonson 2001 150
56 kevin whale 2000 50
7909 michael daisy 1998 200
The champion is
7909 michael daisy 1998 200
Task 2 (25 points): Add the following lines to the end of your main. Your task is to make
your code work and complete it according to the descriptions given below. (Your main
will contain the lines given in Task 1 followed by the following lines exactly).
strcpy(inputFile,"games.txt");
//copies “games.txt” into the string, inputFile
processGames(inputFile, list, n);
/*The matches between the wrestlers are examined as given in
inputFile and then the list of size n is updated accordingly.
You need to create a function named processGames that takes 3
parameters suitable to its usage here*/
printScoreBoard(list, n);
/*prints the scoreboard to screen. This function is the same as
the one created in Task 1*/
writeTheChampion(list, n);
/*prints the champion with the maximum score to screen. This
function is the same as the one created in Task 1*/
The format of the file “games.txt” is shown below:
wrestler_id1 wrestler_id2 match_result score
...
Explanation of the format: Each line consists of 4 integers. The first two numbers
indicate that there is a match between wrestlers with wrestler_id1 and
wrestler_id2. The third number (match_result) can be either 0, 1, or 2, indicating
who the winner is. If 1 then the first wrestler wins and his score is increased by score,
which is the fourth integer in the line. If 2 then the second wrestler wins and his score is
increased by score. If 0 then there is a tie in the game and both wrestlers’ scores are
increased by score. There is no limit to the number of matches presented in
“games.txt”. processGames function has to read the “games.txt” and update list array
accordingly.
For example, assume “game.txt” contains the following lines:
118 234 1 20
234 7909 2 40
7909 56 0 10
56 118 1 50
234 7909 0 15
Then according to the first match, the wrestler with id 118 wins the match against the
wrestler with id 234 and his score is increased by 20, making it 120. According to the
second match, the wrestler with id 7909 wins the match against the wrestler with id 234
and his score is increased by 40, making it 240, etc. At the end, the above code
fragment modifies the scores using this information and prints out the following output,
where the first 5 lines are generated by printScoreBoard and the last 2 by
writeTheChampion.
Printing the score board
118 john brown 2000 120
234 tomas jhonson 2001 165
56 kevin whale 2000 110
7909 michael daisy 1998 265
The champion is
7909 michael daisy 1998 265
Task 3 (25 points). In this assignment, you are asked to obtain a binary image
consisting of 0s and 1s given a grayscale image img.txt consisting of values from 0
to 255. The size of the provided input image img.txt is 64x96. Binarization is the
process of taking a grayscale image and converting it to black-and-white, essentially
reducing the information contained within the image from 256 gray levels to 2 levels:
black and white.
a. Import the provided image called img.txt using file I/O functions.
b. Use the following structure containing the image height, width and
an array imgdata to store the image data:
struct image{
int height;
int width;
int imgdata[64][96];
};
c. Binarize the input image three times by using three different
threshold values: T1 = 64, T2 = 128, T3 = 192. To do the binarization,
perform the following step. If the input image is 𝐼[𝑚, 𝑛], then the resulting
binary image is
d. Store the 3 resulting binary images as .txt files called binary1.txt,
binary2.txt, binary3.txt for three values of T, accordingly.
Example of an input image with size 3x3:
100 130 40
20 74 50
240 30 200
After using T1 = 64, the binary image will be
1 1 0
0 1 0
1 0 1
After using T2 = 128, the binary image will be
0 1 0
0 0 0
1 0 1
After using T3 = 192, the binary image will be
0 0 0
0 0 0
1 0 1
Task 4 (25 points). Write a program that will determine a hidden integer 3-tuple
geometric progression written in consecutive order. Geometric progression is an
ordered list of numbers in which each term after the first is found by multiplying
the previous one by a fixed non-zero number called the common ratio. The initial
array should contain more than 3 integer elements and the values should be
greater than 0. Output the geometric progressions and their common ratios. If no
geometric progressions are found, output “None”.
For example,
● If the input array is (1, 2, 4, 8, 7, 9) then the output should be the
following two geometric progressions: (1, 2, 4) with common ratio
equal to 2, and (2, 4, 8) with common ratio equal to 2.
● If the input array is (1, 4, 2, 8) then there are no geometric
progressions stored in consecutive order.
● If the input array is (5, 2, 25, 5, 1, 3), then there is one geometric
progression: (25, 5, 1) with a common ratio equal to 1/5.
● If the input is (1, 2, 4, 1, 3, 9), then there are two geometric
progressions: (1, 2, 4) with a common ratio equal to 2, and (1, 3, 9)
with a common ratio equal to 3.
Rules:
● Late submissions will NOT be accepted. In the case of late submission, a grade 0
is given.
● No deadline extensions will be given.
● You will lose 50 points if the program does not compile on the online compiler,
https://repl.it/languages/c.
● 5 points will be given for indentation and 5 for comments.
● Assignments must be done individually.
● Submitted codes will be automatically checked using tools that detect plagiarism.
● You may be asked to explain your code and rewrite a part of it in front of the
instructors.
● Please submit each task in a separate .c file naming as task1.c, task2.c etc. for
each task of the assignment and submit in a zip file to the submission box of
Moodle.
● All files must be with .c format. All other formats of files will not be accepted.
Programming Assignment II
Deadline: 17 March 2021, 8:00pm
Explanation for Task1 and Task 2:
You will write programs to handle the games between the wrestlers and maintain their
scores in these tasks. First, create the following struct:
typedef struct{
int id;
char firstName[100];
char lastName[100];
int birthYear;
int score;
} wrestler;
A wrestler is identified by the following features: id (int), first name (char array/string),
last name (char array/string), birth year (int), and score (int).
Task 1 (25 points): Copy&paste the following lines to the beginning of your main
function. Your task is to make your code work and complete it according to the
descriptions given below. (Your main will contain the following lines exactly).
char inputFile[100];
//creates a char array (string)
strcpy(inputFile,"in.txt");
//copies “in.txt” into the string, inputFile
int n = getNumberOfWrestlers(inputFile);
/*retrieves the number of wrestlers in inputFile and assigns it
to variable n*/
wrestler list[n];
/*creates a wrestler array, list, of size n to store wrestlers‘
info*/
init(list, inputFile);
/*initializes list, which is defined above, with the information
in inputFile. You need to create a function named init that
takes 2 parameters suitable to its usage here*/
printScoreBoard(list, n);
/*prints the scoreboard to screen. You need to create a
function named printScoreBoard that takes 2 parameters suitable
to its usage here*/
writeTheChampion(list, n);
/*prints the champion with the maximum score to screen. You
need to create a function named writeTheChampion that takes 2
parameters suitable to its usage here*/
The format of the file “in.txt” is shown below:
Number_of_wrestlers(N)
id1 firstname1 lastname1 birthyear1 score1
id2 firstname2 lastname2 birthyear2 score2
…
idN firstnameN lastnameN birthyearN scoreN
For example, “in.txt” may contain the following lines:
4
118 john brown 2000 100
234 tomas jhonson 2001 150
56 kevin whale 2000 50
7909 michael daisy 1998 200
This means that there are 4 wrestlers in this game. The first wrestler (john brown) with id
118 was born in 2000 and has a score of 100. The second wrestler (tomas jhonson) with
id 234 was born in 2001 and has a score of 150, etc. Then, the above code fragment
initializes the wrestler list array with all this information and prints out the following
output, where the first 5 lines are generated by printScoreBoard and the last 2 by
writeTheChampion.
Printing the score board
118 john brown 2000 100
234 tomas jhonson 2001 150
56 kevin whale 2000 50
7909 michael daisy 1998 200
The champion is
7909 michael daisy 1998 200
Task 2 (25 points): Add the following lines to the end of your main. Your task is to make
your code work and complete it according to the descriptions given below. (Your main
will contain the lines given in Task 1 followed by the following lines exactly).
strcpy(inputFile,"games.txt");
//copies “games.txt” into the string, inputFile
processGames(inputFile, list, n);
/*The matches between the wrestlers are examined as given in
inputFile and then the list of size n is updated accordingly.
You need to create a function named processGames that takes 3
parameters suitable to its usage here*/
printScoreBoard(list, n);
/*prints the scoreboard to screen. This function is the same as
the one created in Task 1*/
writeTheChampion(list, n);
/*prints the champion with the maximum score to screen. This
function is the same as the one created in Task 1*/
The format of the file “games.txt” is shown below:
wrestler_id1 wrestler_id2 match_result score
...
Explanation of the format: Each line consists of 4 integers. The first two numbers
indicate that there is a match between wrestlers with wrestler_id1 and
wrestler_id2. The third number (match_result) can be either 0, 1, or 2, indicating
who the winner is. If 1 then the first wrestler wins and his score is increased by score,
which is the fourth integer in the line. If 2 then the second wrestler wins and his score is
increased by score. If 0 then there is a tie in the game and both wrestlers’ scores are
increased by score. There is no limit to the number of matches presented in
“games.txt”. processGames function has to read the “games.txt” and update list array
accordingly.
For example, assume “game.txt” contains the following lines:
118 234 1 20
234 7909 2 40
7909 56 0 10
56 118 1 50
234 7909 0 15
Then according to the first match, the wrestler with id 118 wins the match against the
wrestler with id 234 and his score is increased by 20, making it 120. According to the
second match, the wrestler with id 7909 wins the match against the wrestler with id 234
and his score is increased by 40, making it 240, etc. At the end, the above code
fragment modifies the scores using this information and prints out the following output,
where the first 5 lines are generated by printScoreBoard and the last 2 by
writeTheChampion.
Printing the score board
118 john brown 2000 120
234 tomas jhonson 2001 165
56 kevin whale 2000 110
7909 michael daisy 1998 265
The champion is
7909 michael daisy 1998 265
Task 3 (25 points). In this assignment, you are asked to obtain a binary image
consisting of 0s and 1s given a grayscale image img.txt consisting of values from 0
to 255. The size of the provided input image img.txt is 64x96. Binarization is the
process of taking a grayscale image and converting it to black-and-white, essentially
reducing the information contained within the image from 256 gray levels to 2 levels:
black and white.
a. Import the provided image called img.txt using file I/O functions.
b. Use the following structure containing the image height, width and
an array imgdata to store the image data:
struct image{
int height;
int width;
int imgdata[64][96];
};
c. Binarize the input image three times by using three different
threshold values: T1 = 64, T2 = 128, T3 = 192. To do the binarization,
perform the following step. If the input image is 𝐼[𝑚, 𝑛], then the resulting
binary image is
d. Store the 3 resulting binary images as .txt files called binary1.txt,
binary2.txt, binary3.txt for three values of T, accordingly.
Example of an input image with size 3x3:
100 130 40
20 74 50
240 30 200
After using T1 = 64, the binary image will be
1 1 0
0 1 0
1 0 1
After using T2 = 128, the binary image will be
0 1 0
0 0 0
1 0 1
After using T3 = 192, the binary image will be
0 0 0
0 0 0
1 0 1
Task 4 (25 points). Write a program that will determine a hidden integer 3-tuple
geometric progression written in consecutive order. Geometric progression is an
ordered list of numbers in which each term after the first is found by multiplying
the previous one by a fixed non-zero number called the common ratio. The initial
array should contain more than 3 integer elements and the values should be
greater than 0. Output the geometric progressions and their common ratios. If no
geometric progressions are found, output “None”.
For example,
● If the input array is (1, 2, 4, 8, 7, 9) then the output should be the
following two geometric progressions: (1, 2, 4) with common ratio
equal to 2, and (2, 4, 8) with common ratio equal to 2.
● If the input array is (1, 4, 2, 8) then there are no geometric
progressions stored in consecutive order.
● If the input array is (5, 2, 25, 5, 1, 3), then there is one geometric
progression: (25, 5, 1) with a common ratio equal to 1/5.
● If the input is (1, 2, 4, 1, 3, 9), then there are two geometric
progressions: (1, 2, 4) with a common ratio equal to 2, and (1, 3, 9)
with a common ratio equal to 3.
Rules:
● Late submissions will NOT be accepted. In the case of late submission, a grade 0
is given.
● No deadline extensions will be given.
● You will lose 50 points if the program does not compile on the online compiler,
https://repl.it/languages/c.
● 5 points will be given for indentation and 5 for comments.
● Assignments must be done individually.
● Submitted codes will be automatically checked using tools that detect plagiarism.
● You may be asked to explain your code and rewrite a part of it in front of the
instructors.
● Please submit each task in a separate .c file naming as task1.c, task2.c etc. for
each task of the assignment and submit in a zip file to the submission box of
Moodle.
● All files must be with .c format. All other formats of files will not be accepted.