Question: Please help me to correct the following code, the current running result is not in line with expectations. #include #include #include using namespace std; const

Please help me to correct the following code, the current running result is not in line with expectations.
#include
#include
#include
using namespace std;
const int MAX_PATH_LENGTH =100;
/* TODO TASK 2: Implement the tear_cards function.
The array card is the original array, card_torn is the array after tearing,
org_size is the number of elements the array card has.
For example, if card is [2,3,5,7], after the function finishes, the card_torn array
should be [2,3,5,7,2,3,5,7]*/
void tear_cards(const int card[], int card_torn[], int org_size){
for (int i =0; i org_size; i++){
card_torn[i]= card[i];
card_torn[i + org_size]= card[i];
}
}
/* TODO TASK 3: Implement the rotate_cards function.
The function should be able to finish the job of rotating the first 'size' elements
of the array 'card' for 'times' times.
For example, rotate_cards(my_card, 7,3) means rotate the first 7 elements of array
my_card for 3 times */
void rotate_cards(int card[], int size, int times){
int* temp = new int[size];
for (int i =0; i size; i++){
temp[(i + times)% size]= card[i];
}
for (int i =0; i size; i++){
card[i]= temp[i];
}
delete[] temp;
}
/* TODO TASK 4: Implement the throw_cards function.
The array card is the original array, card_final is the array after throwing,
size is the number of cards after throwing, age is the age of the player. */
void throw_cards(const int card[], int card_final[], int size, int age){
if (age %2==0){
// Even age, discard the first card
for (int i =0; i size -1; i++){
card_final[i]= card[i +1];
}
}
else {
// Odd age, discard the second card
card_final[0]= card[0];
for (int i =1; i size; i++){
card_final[i]= card[i +1];
}
}
}
/* OPTIONAL TODO: You can rewrite this rotate_remove function to enhance your understanding of 1D-array.
However, there's no bonus for it ^^ Enjoy. */
void rotate_remove(int card[], int size){
int left_card_num = size;
while(left_card_num >1){
//move the top card to the bottom
rotate_cards(card, left_card_num, 1);
//remove the now top card
left_card_num -=1;
for(int i =0; i left_card_num; i++){
card[i]= card[i +1];
}
}
}
//--------------!! WARNING: DO NOT EDIT THIS FUNCTION !!--------------
void output_card(string step, const int card[], int size){
cout step ": ";
for(int i =0; i size; i++){
cout card[i]"";
}
cout endl;
}
int main(){
int card[4];
int year, age;
cout "Welcome to Lab4! Let's do some real magic!" endl;
cout "Please enter the file you put your secrets in: ";
char file_path[MAX_PATH_LENGTH];
cin >> file_path;
cout file_path endl;
ifstream fin(file_path);
//Step1: initialize by reading the player's input file
/* TODO TASK 1: Read secret file from the player. */
/* TODO TASK 1.1: You need to check if the file can be opened, if not, output the error message.*/
if (!fin.is_open()){
cout "Error opening file." endl;
return 1; // Terminate the program
}
/* TODO TASK 1.2: Read and initialize the array representing card, player's year of study and age
You should be aware that the cards, year, and age may appear in random order.
In the array, the 0th element represents the number on the top card,
1st element is the number on the second top card, etc. */
char type_of_information;
while(fin >> type_of_information){
if (type_of_information =='C'){
// Read the card number
int card_number;
fin >> card_number;
// Initialize the card array
card[0]= card_number;
}
else if (type_of_information =='Y'){
// Read the year of study
fin >> year;
} else if (type_of_information =='A'){
// Read the player's age
fin >> age;
}
}
fin.close();
//--------------!! WARNING: DO NOT EDIT THE CODE BELOW !!--------------
//Step2: tear the cards and duplicate
int card_torn[8];
tear_cards(card, card_torn, 4);
output_card("torn", card_torn, 8);
//Step3: rotate based on the player's year of study
rotate_cards(card_torn, 8, year);
output_card("rotate by year of study", card_torn, 8);
//Step4: hide the fourth card
int hidden_card = card_torn[3];
int card_after[7]={card_torn[0], card_torn[1], card_torn[2], card_torn[4], card_torn[5], card_torn[6], card_torn[7]};
output_card("hide the fourth", card_after, 7);
//Step5: if the player's age is even, throw away the first card; else throw away the second card
int card_final[6];
throw_cards(card_after, card_final, 6, age);
output_card("thrown by age", card_final, 6);
//Step6: rotate based on the magic spell
rotate_cards(card_final, 6,7);
output_card("rotate by spell", card_final, 6);
//Step7: move the top card to the bottom and then remove the now top card, until there's only one card left
rotate_remove(card_final, 6);
//Step8: compare the hidden card and the last card left
if(hidden_card != card_final[0]){
cout "Your hidden card [" hidden_card "] and the last card [" card_final[0]"] do not match";
} else {
cout "Your hidden card [" hidden_card "] and the last card [" card_final[0]"] match perfectly!";
}
return 0;
}
 Please help me to correct the following code, the current running

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!