题目
LO01 – Practice test
Spring 2021
Context
You are about to work on a program that reads the content of a file. The file contains a list of words plus an ID numbers between 1 and 3000 for each word. Each word is unique (appears only once in the list) and has a unique ID number (2 different words necessarily have different ID numbers). The program will ask 5 times the user to enter an integer between 1 and 3000 and will display the corresponding “word” if the given number is in the list and will remove the word from the list or it will display a message that tells you that the ID is not in the list.
Program leading principle
- First the program is going to open the data file and to memorize 500 words with their ID numbers in a data structure.
- Sort the list according to ID numbers
- Repeat 5 times
a. Ask an ID number and read it
b. Suppress the ID number in the list
To Begin:
The first 3 points corresponds to the usual process to create a C console project with CodeBlocks and the following ones are specific for the test. - Open CodeBlocks or the equivalent, create a new project (file menu) and select a console application in the popup window. Then press next and select C in the proposed list (NOT C++!!!). Next set the title to “test” followed by your personal student ID (such as test201567893), choose the folder of the project (the one you wish), and finish the project creation.
- Copy the C code at the end of that file (all the code in section program elements).
- Open the file main.c from your project in the CodeBlock left window.
- Remove the C code that is in the main.c file and paste the C code you copied from this word file.
- Build a first time the code to control that the compilation is normal (no errors, just some warnings, you don’t care about those). If you receive error messages, call the professor.
- Copy the file “data.txt” in the project folder (named test followed by your ID number such as “test201567893” in the example above). The file “data.txt” has been sent to you.
- Now go back to the main.c file in CodeBlocks. You are ready to work! Read carefully what follows.
Work to be done
In the C code given in next “Program elements” section you have: - C code in black,
- Comments in blue
- Algorithms in dark green
- Pay attention to red comments
You must not change the given C code!
Your mission is to write the C subprograms corresponding to the 3 given algorithms and, in the main program, you must add, below the commented calls, the proper C calls (3 commented lines in algorithmic style in the main program).
Type each C subprogram below its algorithm. The C subprograms must respect the algorithms principle. This is mandatory! But the algorithms are copied from the lectures and use the lecture data structure. It means that you might have to introduce mild changes to comply with the actual context of the given program. This is really important to keep in mind.
At the end of the exam, you must return the project file to your teacher following the instructions he will provide you (even if the program does not work properly).
Program elements (To be copied to your main.c file)
#include <stdio.h>
#include <stdlib.h>
struct element{int num; char text[15];};
//----------------------------------------------------
// procedure that read the list of 500 words from a file. Each word has a number.
void Read_list(struct element Tab[600], int*Nb)
{
FILE *fp;
int k;
fp=fopen("data.txt","r");
*Nb=500;
for(k=0;k<*Nb;k=k+1)
{
fscanf(fp,"%d%s",&Tab[k].num,Tab[k].text);
}
}
//----------------------------------------------------
// procedure to swap 2 items of type struct element
void Swap_elements(struct element E_in1, struct element E_in2, struct element *E_out1, struct element *E_out2)
{
*E_out1=E_in2;
*E_out2=E_in1;
}
//----------------------------------------------------
// procedure to copy an array of element to another
void Copy_array_of_elements(struct element Tab_in[],int Nb,struct element Tab_out[])
{
int index;
for(index=0;index<Nb;index=index+1)
{
Tab_out[index]=Tab_in[index];
}
}
//----------------------------------------------------
// procedure to search an element in the list
void Search_element(struct element Tab_in[],int Nb,int num_searched,int *found, int * index_found)
{
int index;
index=0;
while ((Tab_in[index].num!=num_searched)&&(index<Nb))
{
index=index+1;
}
if (Tab_in[index].numnum_searched)
{
*found = (00);
*index_found = index;
}
else
{
*found = (0==1);
index_found = -1;
}
}
//##################### YOUR TASK BEGINS THERE ###############################
// procedure to sort a list of element in an array
/ Sub-algorithm Bubble_Sort //for a list of real numbers
In : Tab_in : array [1…] of reals, Nb_Val : integer
Out : Tab_Sorted : array [1…] of reals
Variables :
Index_End_Sorted, i_scan : integer ; permutation : boolean
Instructions
Copy_array_of_real(Tab_in, Nb_Val ! Tab_Sorted)
permutation TRUE
Index_End_Sorted 0
while (Index_End_Sorted < Nb_Val-1 and permutation=TRUE) do
permutation FALSE
for i_scan from Nb_Val-1 to Index_End_Sorted+1 step -1
if Tab_Sorted[i_scan] > Tab_Sorted[i_scan+1] then
Swap_Reals(Tab_Sorted[i_scan], Tab_Sorted[i_scan+1] ! …
Tab_Sorted [i_scan], Tab_Sorted[i_scan+1] )
permutation TRUE
endif
endfor
Index_End_Sorted Index_End_Sorted+1
Endwhile */
//void Bubble_Sort… - to be written in C by adapting the above sub-algorithm (At least the array type has to be changed)
//----------------------------------------------------
// procedure to display an element on the screen
/*Sub-algorithm display
In : List_in : array[1…] of element, index2display : integer
Out : -
Variables : -
Instructions
write(“The word number”,List_in[index2display].num," is : ",List_in[index2display].text,”ans has been deleted”)
*/
//void display… - to be written in C following the above sub-algorithm
//----------------------------------------------------
//procedure to suppress a word from the list given its ref number
/Sub-algorithm Suppress_Sorted_element
In : List_in : Type_List2 , ref2del : integer
Out : Done : Boolean, List_out : Type_List2
Variables :
index_ref2del, index : integer
Instructions
List_out <-- List_in
Search_element(List_out, List_out.Nb, ref2del ! Done, index_ref2del)
if Done = True then
display(List_out,index_ref2del!)
for index from index_ref2del+1 to List_out.Nb step 1
List_out.Tab[index-1] <-- List_out.Tab[index]
endfor
List_out.Nb <-- List_out.Nb-1
endif/
//void Suppress_Sorted_element… - to be written in C by adapting the above sub-algorithm (The list data structure has to be changed)
//----------------------------------------------------
//----------------------------------------------------
int main(void)
{
struct element The_list[600];
int Nb_words; // the number of words in the list
int k, repetition; // k : dummy variable, repetition : index of the main for loop
int num_word; // the number of the searched word
Read_list(The_list,&Nb_words);
//Bubble_Sort(The_list,Nb_words!The_list) - to be changed to C
for(repetition=0;repetition<5;repetition=repetition+1)
{
printf("enter the integer ID of the word you want to delete (between 1 and 3000) : ");
scanf("%d",&num_word);
//Suppress_Sorted_element(The_list,Nb_words,num_word!The_list,Nb_words) - to be changed to C
}
printf("It remains %d words in the list.\n",Nb_words);
scanf("%d",&k);
return 0;
}
代码
C语言作答
#include <stdio.h>
#include <stdlib.h>
struct element
{
int num;
char text[15];
};
//----------------------------------------------------
// procedure that read the list of 500 words from a file. Each word has a number.
void Read_list(struct element Tab[600], int*Nb)
{
FILE *fp;
int k;
fp=fopen("data.txt","r");
*Nb=500;
for(k=0; k<*Nb; k=k+1)
{
fscanf(fp,"%d%s",&Tab[k].num,Tab[k].text);
}
}
//----------------------------------------------------
// procedure to swap 2 items of type struct element
void Swap_elements(struct element E_in1, struct element E_in2,\
struct element *E_out1, struct element *E_out2)
{
*E_out1=E_in2;
*E_out2=E_in1;
}
//----------------------------------------------------
// procedure to copy an array of element to another
void Copy_array_of_elements(struct element Tab_in[],int Nb,struct element Tab_out[])
{
int index;
for(index=0; index<Nb; index=index+1)
{
Tab_out[index]=Tab_in[index];
}
}
//----------------------------------------------------
// procedure to search an element in the list
void Search_element(struct element Tab_in[],int Nb,int num_searched\
,int *found, int * index_found)
{
int index;
index=0;
while ((Tab_in[index].num!=num_searched)&&(index<Nb))
{
index=index+1;
}
if (Tab_in[index].num==num_searched)
{//找到了 返回true和编号
*found = (0==0);
*index_found = index;
}
else
{
*found = (0==1);
*index_found = -1;
}
}
//##################### YOUR TASK BEGINS THERE ###############################
// procedure to sort a list of element in an array
/* Sub-algorithm Bubble_Sort //for a list of real numbers
In : Tab_in : array [1..] of reals, Nb_Val : integer
Out : Tab_Sorted : array [1..] of reals
Variables :
Index_End_Sorted, i_scan : integer ; permutation : boolean
Instructions
Copy_array_of_real(Tab_in, Nb_Val ! Tab_Sorted)
permutation TRUE
Index_End_Sorted 0
while (Index_End_Sorted < Nb_Val-1 and permutation=TRUE) do
permutation FALSE
for i_scan from Nb_Val-1 to Index_End_Sorted+1 step -1
if Tab_Sorted[i_scan] > Tab_Sorted[i_scan+1] then
Swap_Reals(Tab_Sorted[i_scan], Tab_Sorted[i_scan+1] ! …
Tab_Sorted [i_scan], Tab_Sorted[i_scan+1] )
permutation TRUE
endif
endfor
Index_End_Sorted Index_End_Sorted+1
Endwhile */
void Bubble_Sort(struct element* Tab_in,int Nb){
int i,j;
//外层循环控制轮数
for (i=0;i<Nb-1;i++)
{ //内层循环控制次数
for (j=0;j<Nb-i-1;j++)
{
if (Tab_in[j].num<Tab_in[j+1].num)
{
Swap_elements(Tab_in[j], Tab_in[j+1],&Tab_in[j], &Tab_in[j+1]);
}
}
}
}
//----------------------------------------------------
// procedure to display an element on the screen
/*Sub-algorithm display
In : List_in : array[1..] of element, index2display : integer
Out : -
Variables : -
Instructions
write("The word number",List_in[index2display].num," is : "
,List_in[index2display].text,”ans has been deleted”)
*/
void display(struct element* List_in,int index2display){
printf("The word number %d is : %s ,ans has been deleted\r\n"\
,List_in[index2display].num,List_in[index2display].text);
}
//----------------------------------------------------
//procedure to suppress a word from the list given its ref number
/*Sub-algorithm Suppress_Sorted_element
In : List_in : Type_List2 , ref2del : integer
Out : Done : Boolean, List_out : Type_List2
Variables :
index_ref2del, index : integer
Instructions
List_out <-- List_in
Search_element(List_out, List_out.Nb, ref2del ! Done, index_ref2del)
if Done = True then
display(List_out,index_ref2del!)
for index from index_ref2del+1 to List_out.Nb step 1
List_out.Tab[index-1] <-- List_out.Tab[index]
endfor
List_out.Nb <-- List_out.Nb-1
endif*/
void Suppress_Sorted_element(struct element* List_in,int* Nb,int ref2del){
int found,idx_found,i;
Search_element(List_in,*Nb,ref2del,&found,&idx_found);
if(found==(0==0)){
display(List_in,idx_found);
for(i=idx_found;i<*Nb-1;i++){
List_in[i]=List_in[i+1];
}
*Nb-=1;
}
}
//----------------------------------------------------
//----------------------------------------------------
int main(void)
{
struct element The_list[600];
int Nb_words; // the number of words in the list
int k, repetition; // k : dummy variable, repetition : index of the main for loop
int num_word; // the number of the searched word
Read_list(The_list,&Nb_words);
Bubble_Sort(The_list,Nb_words);
for(repetition=0; repetition<5; repetition=repetition+1)
{
printf("enter the integer ID of the word you want to delete (between 1 and 3000) : ");
scanf("%d",&num_word);
Suppress_Sorted_element(The_list,&Nb_words,num_word);
}
printf("It remains %d words in the list.\n",Nb_words);
scanf("%d",&k);
return 0;
}