Question

In: Computer Science

Can someone solve these two questions for an intro c++ course? 1. #include <iostream> using namespace...

Can someone solve these two questions for an intro c++ course?

1.

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{

    char twostrings[] = "Hello! CS103L!";
    char* p1;
    char* p2;
    //fill in the code required to print out Hello! and CS103L! on different lines
    //Hint: modify one character in the original string and set p1 and p2 correctly
    //Do not make any copies or new strings or arrays. You only need to modify the original array.
    
    //
    cout << p1 << endl;
    cout << p2 << endl;
}

2.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    string column1[5];
    string column2[5]
    //use the getline() function (not cin.getline()) to read in 5 lines from cin into column1
    
    //now read 5 lines from cin into column2
        
    //Now write a for loop that will print out five lines with the two columns next to each other separated by a tab character
    //try using string concatenation instead of multiple << operators 

}

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{

char twostrings[] = "Hello! CS103L!";
char* p1;
char* p2;
//fill in the code required to print out Hello! and CS103L! on different lines
//Hint: modify one character in the original string and set p1 and p2 correctly
//Do not make any copies or new strings or arrays. You only need to modify the original array.
  
//modifying one character
twostrings[6]='\0';
//setting pointers
p1 = twostrings;
p2 = twostrings+7;
  
cout << p1 << endl;
cout << p2 << endl;
}

output:

Hello!
CS103L!


Process exited normally.
Press any key to continue . . .

2)

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
string column1[5];
string column2[5];
//use the getline() function (not cin.getline()) to read in 5 lines from cin into column1
for(int i=0;i<5;i++)
   getline(cin,column1[i]);
  
//now read 5 lines from cin into column2
for(int i=0;i<5;i++)
   getline(cin,column2[i]);
  
//Now write a for loop that will print out five lines with the two columns next to each other separated by a tab character
//try using string concatenation instead of multiple << operators

   for(int i=0;i<5;i++)
   cout<<column1[i]+"\t"+column2[i]<<endl;
return 0;
}


Related Solutions

Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace...
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace std; int num; char letter; int main() {    cin >> num;    cin >> letter;    cout << "You inputted " << num << endl;    cout << "Option " << letter << endl;    if (letter == '*')       cout << "Multiplied by 2 " << num*2 << endl;    return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT