Question

Vectors. This lab is to be done using Unix. To familiarize yourself with Unix, read this...

  1. Vectors. This lab is to be done using Unix. To familiarize yourself with Unix, read this tutorial.

    To prove that you used Unix tools, as part of your project submission, you need to submit a typescript file with the record of at least one successful compilation of the second project programs below (the execution of the compiler on your source code file). Create a project titled Lab12_Vectors. Implement the dynamically expanding and contracting container functionality described in previous labs using vectors. In the main() function, you may declare the vector to hold the integers input. Specifically, your program has to support this dialog:

    enter operation [a/r/q] and number: a 5.0
    your numbers: 5
    enter operation [a/r/q] and number: a 8.0
    your numbers: 5 8
    enter operation [a/r/q] and number: a 8.0
    your numbers: 5 8
    enter operation [a/r/q] and number: r 8.0
    your numbers: 5
    enter operation [a/r/q] and number: a 3.3
    your numbers: 5 3
    enter operation [a/r/q] and number: a 12.2
    your numbers: 5 3.3 12.2
    enter operation [a/r/q] and number: q
    
    Hint: use iterators for number removal. Use find() algorithm to check if the number of present in the vector. Refer to this code for example usage.

    Code

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using std::cin; using std::cout; using std:: endl; 
    using std::vector;
    
    int main(){
      vector<int> v;
    
      // inputing the numbers
      cout << "Enter numbers, 0 to quit: ";
      int num;
      cin >> num;
      while(num!=0){
        vector<int>::iterator ip=v.end();
        v.insert(ip,num);
        cin >> num;
      }
    
      cout << "Enter number to remove: ";
      int numToRemove;
      cin >> numToRemove;
    
      // using find() to locate number 
      vector<int>::iterator it;
      it = find(v.begin(), v.end(), numToRemove);
    
      if (it != v.end()){ // if found, erase
         cout << "found number at location " << it - v.begin() << endl;
         v.erase(it);
      }
    
      sort(v.begin(),v.end()); // sorting elements
    
      // printing the sorted numbers
      cout << "Your numbers sorted: ";
      for(vector<int>::iterator ip=v.begin();
          ip != v.end(); ++ip)
        cout << *ip << " ";
      cout << endl;
    }
  2. Multidimensional arrays. Create a project Lab12_MultArrays. Implement a program that keeps track of shots fired in a battleship game. Your program should ask the user if she wants to take another shot, ask for shot coordinates and then print the locations of all shots fired so far in a grid. Here is an example dialog:
    Another shot? [y/n] y
    Location?  a 1
    All fired shots
      a b c d e f
    1 *       *
    2     *
    3
    4 * *
    5         
    6           *
    Another shot? [y/n] y
    ...
    
    You do not have to keep track if multiple shots were fired in the same location, i.e. you should show a single star to indicate that the shot was fired there. You do not have to join this program with previously developed battleship program.

    You should store the shots in a two-dimensional array of booleans. You should use the following code as a starting point.

    Code

    // size of the ocean
    const int oceanLength = 6;
    const int oceanWidth = 6;
    
    int main(){
         bool shots[oceanLength][oceanWidth];
         // place your code here
    }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

// C++ program to implement operations on vector
#include <iostream>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
vector<int> v;
int num;
char ch;
do{
   cout<<"enter operation[a/r/q] and integer : ";
cin>>ch;
if(ch == 'q' || ch == 'Q')
break;
cin>>num;
switch(ch)
{
case 'a':
case 'A':
v.push_back(num);
break;
case 'r':
case 'R':
{ vector<int>::iterator it;
it = find(v.begin(),v.end(),num);
if(it != v.end())
v.erase(it);
break;
   }
default:
cout<<"Wrong choice"<<endl;

}

cout<<"your numbers : ";
for(vector<int>::iterator ip = v.begin(); ip != v.end(); ++ip)
cout<<*ip<<" ";
cout<<endl;
}while(ch != 'q' && ch != 'Q');

return 0;  
}

//end of program

Output:

2.

// C++ program to implement operations on 2d array
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

const int oceanLength = 6;
const int oceanWidth = 6;

int main()
{
bool shots[oceanLength][oceanWidth];
for(int i=0;i<oceanLength;i++)
for(int j=0;j<oceanWidth;j++)
shots[i][j] = false;
  
int col;
char row, choice;
do{
cout<<"Location? ";
cin>>row>>col;
int row_num = (int)(row - 'a') ;
if(row_num >= 0 && row_num <oceanLength && col >0 && col < oceanWidth)
shots[row_num][col-1] = true;
cout<<"All fired shots : "<<endl;
cout<<" ";  
for(int i=0;i<oceanLength;i++)
cout<<(char)((int)'a' + i)<<" ";
for(int i=0;i<oceanLength;i++)
{
cout<<"\n"<<(i+1)<<" ";
for(int j=0;j<oceanWidth;j++)
{
if(shots[i][j])
cout<<"*"<<" ";
else
cout<<" ";
}
}

cout<<"\nAnother shot? [y/n] " ;
cin>>choice;
}while(choice == 'y' || choice == 'Y');
return 0;  
}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Vectors. This lab is to be done using Unix. To familiarize yourself with Unix, read this...
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
  • Consider this code below. If this is correct, explain how it works. If it is not...

    Consider this code below. If this is correct, explain how it works. If it is not correct, state the problem and provide a fix. vector<int> v = {10, 20, 10, 20, 30, 10}; cout << "Enter number to remove from vector"; int toRemove; cin >> toRemove; for(auto it = v.begin(); it != v.end(); ++it) if(*it == toRemove) v.erase(it);

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • What does this error message mean? "expected primary expression before int" Also, how do i get...

    What does this error message mean? "expected primary expression before int" Also, how do i get the program to repeat the numbers that were entered by the user back to me? this is my code below: // This program accepts a series of positive numbers // This code was last modified on 2/16/2020 #include <iostream> #include <iomanip> #include <cmath> using namespace std; void welcome(); int entrySubmission(int numbers); void acceptsNumbers(int); void goodbye(); int main() { welcome(); entrySubmission(int numbers); acceptsNumbers(int); goodbye(); return...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • C++ Programming Code Do not change anything in the supplied code below that will be the...

    C++ Programming Code Do not change anything in the supplied code below that will be the Ch16_Ex5_MainProgram.cpp except to add documentation and your name. Please use the file names listed below since your file will have the following components: Ch16_Ex5_MainProgram.cpp - given file //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999 #include <iostream> #include "unorderedLinkedList.h" using namespace std; int main() { unorderedLinkedList<int> list, subList; int num;                                                                                                                                    cout << "Enter...

  • Bly language program that c l orresponds to the following Cr+ program.Include the memory addr Wri...

    bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include <iostream> using namespace std; int num; int main ( cout << "Enter a number:" cin >> num ; num = num * 4; cout << "num 4-<< num << endl; return 0 bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include using namespace std; int num; int main (...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

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