Question

Question 8) Suppose a program is supposed to merge two files in the following manner: write...

Question 8)

Suppose a program is supposed to merge two files in the following manner: write two lines from the first file, write two lines from the second file, and repeat the process until all of the lines have been written to the new merged file. For example suppose we have the following two input files:

File1.txt

File2.txt

AAA

BBB

CCC

DDD

000

111

222

333

Then the merged output file would be:

File3.txt

AAA

BBB

000

111

CCC

DDD

222

333

Which of the options below is a correct implementation of the program?

NOTE: Assume the two input files always have the same number of lines

      Assume the number of lines is always even in both files

     Assume both files always have at least two (2) lines

The infile1 variable is associated with the File1.txt file

The infile2 variable is associated with the File2.txt file

The total_lines variable stores the total number of lines in the two files together (8 in the example shown above)

(the options are on the next page)

Which of the options below is a correct implementation of the program?

Group of answer choices

infile1>>file_line1;

infile2>>file_line2;

do {

       outfile<<file_line1<<endl;

       outfile<<file_line2<<endl;

      }

    while(infile1>>file_line1 &&

               infile>>file_line2);

int total_lines = count_lines(filename1) +

                             count_lines(filename2);

for(int i=0; i<(total_lines/2); i++)

{

    if((i % 2) == 0)    

         infile1>>file_line;

    else

         infile2>>file_line;

    

    outfile<<file_line<<endl;

}

int total_lines = count_lines(filename1) +

                              count_lines(filename2);

for(int i=0; i<total_lines; i++)

{

    if((i % 4) <=1)    

         infile1>>file_line;

    else

         infile2>>file_line;

    

    outfile<<file_line<<endl;

}

while(infile1>>file_line1 && infile>>file_line2)

{

   outfile<<file_line1<<endl;

   outfile<<file_line2<<endl;

}

Question 9)

Suppose two players are playing a modified version of the game Battleship on a 10 X 10 game board. In this modified version of the game, there is only ONE (1) battleship that takes up FIVE (5) spaces on the game board.


NOTE: In Battleship, ships can be placed only horizontally or vertically. They CANNOT be placed diagonally.

The current state of the game board for player 1 is shown below. Note the following:

  • An empty cell means player 1 has not tried to hit that cell on the game board yet.
  • A cell with an “*” means player 1 hit that cell, but they missed (i.e., player 2 did not have part of their ship on that cell)
  • A cell with an “X” means player 1 hit that cell and successfully hit part of player 2’s ship

*

*

*

*

*

*

*

*

*

*

*

*

*

*

X

*

*

*

*

Player 1’s game board is represented by a 10X10 2D array with the name player1. Based on the game board shown above, all of the following options below would be logical moves for player 1 to make on their next turn EXCEPT:

Group of answer choices

C-) player1[5][4]

D-) player1[6][3]

B-) player1[5][2]

A-) player1[4][3]

Question 10)

There is a close correspondence between the indexes in a 1D array and 2D array of the same size.

For example, suppose a program has the following two array declarations:

const int SIZE = 4, X=2, Y=2;

int arr1[SIZE];

int arr2[X][Y];

Both the 1D array and the 2D array have four indexes (subscripts).

For example, suppose the numbers 11, 22, 33, and 44 are being stored. The 1D array would look like this:

11

22

33

44

And the 2D array would look like this:

11

22

33

44

Assume the variable index is storing an index number from the 1D array. Which of the options below will correctly convert an index from the 1D array into the equivalent index in the 2D array?

Group of answer choices

A-) arr2[index/X][index/Y]

D-) arr2[index/X][index%Y]

B-) arr2[index%X][index%Y]

C-) arr2[index%X][index/Y]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q8} Correct option: c

int total_lines = count_lines(filename1) + count_lines(filename2);

for(int i=0; i<total_lines; i++){

if((i % 4) <=1)     infile1>>file_line;

else infile2>>file_line;

     outfile<<file_line<<endl;

}

Whenever the remainder is 0 or 1 serially words from file 1 is picked up into file_line and written to output file again when remainder on dividing i by 4 is 2 or 3 then words from file 2 is picked up and written to output file, So this way in the cycle upto the count of total_lines , every consecutive two lines from file 1 corresponding to remainder 0 and 1 is written to output first and then two lines from file 2 corresponding to file 2 is written to output file after that.

Q9} correct option : player1 [4] [3]

Any move which lies between the given ranges below representing possible positions of the ship is logical:

player1[4][3] is not a logical move because this move has already been done earlier and the battleship of player 2 is not over there as the [4][3] cell has " * " marked on it ,

Q 10}correct option: arr2[ index / X ] [ index % Y ]

index value starts from 0 : keeping that in mind. for an element in the 1 D array with a given index ,in order to compute its location on a 2D array , we must find how many elements from start of the 1D array can be placed in the first row of the 2D array this will give us the row number of the element in 2D array , this can be computed using ( index / X ) .
once we get the row number , we must see what is the offset left to place our element in the row which we found by index/X , this can be done by finding the remainder(offset) ie index % Y ,this is the column number

Add a comment
Know the answer?
Add Answer to:
Question 8) Suppose a program is supposed to merge two files in the following manner: write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1) Suppose a program has the following code: const int X = 2, Y =...

    Question 1) Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3},                                  {4, 5, 6}}; for(int i=0; i<X; i++) {      sum=0;      for(int j=0; j<Y; j++)         sum+=values[i][j];    cout<<sum<<endl; } What is this program getting the sum of? Group of answer choices D-) The columns of the 2D array C-) The rows of the 2D array A-) All of the elements of the 2D...

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • In Python, do a basic encryption of a text file in the following manner. The program...

    In Python, do a basic encryption of a text file in the following manner. The program encrypt.py will read in the following text file and rearrange the lines in the file randomly and save the rearranged lines of txt to another file called encrypted.txt. It will also save another file called key.txt that will contain the index of the lines that were rearranged in the encrypted file, so for example if the 4th line from the original file is now...

  • I am trying to modify this program so that it stores the employee's name in a...

    I am trying to modify this program so that it stores the employee's name in a c-string and can handle up to 100 employees (so it would mostly be a partially filled array), but I cannot get it to work. It works with two files that it is suppose to read input from. Then it prints output to an output file. Employee Info File (first file read, M/F should be ignored): 5    Christine Kim 30.00   2       1    F 15    Ray...

  • a) Hand-trace the following program and determine and write down what is the output of the...

    a) Hand-trace the following program and determine and write down what is the output of the code.                b) Run the code and compare the program output to your hand-traced result obtained from part (a). #include <iostream> #include <iomanip> using namespace std; void f(); int x = 5; int main() {         cout << "x = " << x << endl;         int x = 6;         cout << "x = " << x << endl;         {                int...

  • Imagine we are using a two-dimensional array as the basis for creating the game battleship. In...

    Imagine we are using a two-dimensional array as the basis for creating the game battleship. In the game of battleship a '~' character entry in the array represents ocean, a '#' character represents a place ion the ocean where part of a ship is present, and an 'H' character represents a place in the ocean where part of a ship is present and has been hit by a torpedo. Thus, a ship with all 'H' characters means the ship has...

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT