Question

In: Computer Science

#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...

#include <iostream>
#include <string>
#include <array>
#include <vector>
using namespace std;
void f(int x, int &y);
int main(){
  char s1[] = "zoey";
  char s2[] = "zoey";
  string s3 = s1;
  string s4 = s2;
  cout << "1.  sizeof(s1) = " << sizeof(s1) << endl;
  if(s1 == s2) cout << "2.  s1 == s2 is true" << endl;
  else cout << "2.  s1 == s2 is false" << endl;
  if(s3 == s4) cout << "3.  s3 == s4 is true" << endl;
  else cout << "3.  s3 == s4 is false" << endl;
  array<double, 5> d = {7.5, 2.8, 9.5, 88.7, 1.7};
  vector<int> v;
  for(size_t i = 0; i < 4; i++){
    v.push_back((unsigned long) &d[i+1] - (unsigned long) &d[i]);
  }
  cout << "4.  The values stored in v are ";
  for(int value : v){
    cout << value << " ";
  }

cout << endl;

char * p1 = &s4[0];
*p1 = 'j';
*(&s4[3]) = 's';
cout<<"5. s4="<<s4<<endl;

s4 = s1;
string s5("zoey\0 is awesome"); cout<<"6. s5="<<s5<<endl;

int q[5];
int * p2 = q;
for(int i = 1; i < 6; i++) *(p2 + i - 1) = i * 5;
f(*(q + 1), *(q + 2));
cout << "7. What is the value of (q[1] + q[2])?" << endl;
cout << "8. What is the value of q?" << endl;
cout << "9. p2 = " << (unsigned long) p2 << ", q = " << (unsigned long) q << endl; cout<<"10.p2="<<p2<<",q="<<q <<endl;

2

return 0; }

void f(int x, int &y){
  x = -1;

y = -2; }

An output for the C++ (version 14) program above executed on nike.

1. sizeof(s1) = 5
2. s1 == s2 is false
3. s3 == s4 is true
4. Thevaluesstoredinvare8888
5. s4 = joes
6. s5 = zoey
7. What is the value of (q[1] + q[2])?
8. What is the value of q?
9. p2 = 140726181901504, q = 140726181901504 10. p2 = 0x7ffd5e1534c0, q = 0x7ffd5e1534c0

Create a file called lab04Answers.txt on nike, and answer the following 10 questions based on the C++ program and its output above.

  1. In the output for 1 above, why is sizeof(s1) = 5 even though s1 contains only four letters? What is character is being stored in the fifth byte of s1?

  2. In the output for 2 above, why does s1 == s2 result in false when both s1 and s2 contain the same character sequence? What values are being compared in s1 == s2 to yield a result of false?

  3. In the output for 3 above, why does s3 == s4 result in true? What values are being compared in s3 == s4? What types of strings are s3 and s4, and how are they different than the types of string used for s1 and s2?

  4. In the output for 4 above, explain why the values stored in v are all 8? Explain your answers in terms of how the memory addresses are being stored for d. Assume the starting address of d is 500, then what are the memory addresses for d[0], d[1], d[2], d[3], and d[4]?

  5. Why is joes in the output for 5 above? Explain how the first character in s4 was changed to ’j’. Explain how the last character in s4 was changed to ’s’.

  6. Why did the output for 6 above print s5 = zoey instead of s5 = zoey is awesome?

  7. As asked in the output for 7 above, what is the value of (q[1] + q[2])? Is it correct syntax to use *(q[1] + q[2]) to compute the value of (q[1] + q[1])? Explain why this syntax is correct or why it isn’t correct.

  8. As asked in the output for 8 above, what is the value of q? Explain what this value represents in terms of what is going on in the program’s memory at runtime.

  9. Consider the output for 9 and 10 above. Why do these memory addresses typically change each time this program is run on nike?

  10. Consider the output for 9 and 10 above. The memory addresses that are being display to standard output for 9 above are using which number system: binary, octal, base-10, or hexadecimal? The memory addresses that are being display to standard output for 10 above are using which number system: binary, octal, base-10, or hexadecimal?

Solutions

Expert Solution

  1. In the output for 1 above, why is sizeof(s1) = 5 even though s1 contains only four letters? What is character is being stored in the fifth byte of s1?

      Ans) Because s1 declared as char array.A char array end with ‘\0’ terminating element.

  1. In the output for 2 above, why does s1 == s2 result in false when both s1 and s2 contain the same character sequence? What values are being compared in s1 == s2 to yield a result of false?

        Ans) s1 contains in 1 memory location and s2 in other , so ‘==’ only compare the locations and output false;If you using strcmp which return true;

  1. In the output for 3 above, why does s3 == s4 result in true? What values are being compared in s3 == s4? What types of strings are s3 and s4, and how are they different than the types of string used for s1 and s2?

Ans) Because it matching each characters of the s3 and s4, both are same , so return true.

  1. In the output for 4 above, explain why the values stored in v are all 8? Explain your answers in terms of how the memory addresses are being stored for d. Assume the starting address of d is 500, then what are the memory addresses for d[0], d[1], d[2], d[3], and d[4]?

Ans)Here vector ‘v’ push values of the address differences of the array elements stored . It is taken as unsigned long, so 8-bit address for each element stored .Difference between elements stored address always become 8. So we get output 8888.

     d[0]=500 d[1]=508 d[2]=516 d[3]=524 d[4]=232

     v.push(508-500)=8;

  1. Why is joes in the output for 5 above? Explain how the first character in s4 was changed to ’j’. Explain how the last character in s4 was changed to ’s’.

Ans) Here we assign a pointer P1 , which pointing to the address of s4. So p1 point to the starting address of s4,her that address hols ‘z’ but it changed into ‘J’ by using p1=’J’ and p1(&s4[3] which points to 4th letter of s4 and change into ‘s’ , so output is ‘Joes’

  1. Why did the output for 6 above print s5 = zoey instead of s5 = zoey is awesome?

Ans) String is always end with null terminating character, here after ‘zoey’ using null terminating character ’\0’.So s5 take string before ‘\0’.We get output ‘zoey’ and not assign after ‘\0’ characters.

  1. As asked in the output for 7 above, what is the value of (q[1] + q[2])? Is it correct syntax to use *(q[1] + q[2]) to compute the value of (q[1] + q[1])? Explain why this syntax is correct or why it isn’t correct.

Ans) Value should be 8. For loop assign each q array vaues 5,10,15,20,25.

After that call function f, which take q[1] and q[2] as parameters, but q[2] as reference parameter. So any change inside function will reflect it’s value .

After function execution it change q[2] from 15 to -2. So q[1]+q[2]=10+-2=8

“*(q[1] + q[2])” using instead of “(q[1] + q[2])” is wrong format, we can use *(q+1)+*(q+2)” that will generate same result. Because pointer pointing starting address of the array, so increment 1 in pointer which goes to next address, that is next element stored.

  1. As asked in the output for 8 above, what is the value of q? Explain what this value represents in terms of what is going on in the program’s memory at runtime.

Ans) value of q is the starting address of the array q,here it is 00D7F644. It will change each time execute, because of allocation of array in the memeory

  1. Consider the output for 9 and 10 above. Why do these memory addresses typically change each time this program is run on nike?

Ans) . It will change each time execute, because of allocation of array in the memeory. Both points to the start address of the array, so change in allocation change the result in each time.

  1. Consider the output for 9 and 10 above. The memory addresses that are being display to standard output for 9 above are using which number system: binary, octal, base-10, or hexadecimal? The memory addresses that are being display to standard output for 10 above are using which number system: binary, octal, base-10, or hexadecimal?

Ans) Both are displaying in Hexadecimal format. In hex representation contains 0-9 and A-F.


Related Solutions

#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#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 =...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
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...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT