In: Computer Science
#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.
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?
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?
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?
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]?
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’.
Why did the output for 6 above print s5 = zoey instead of s5 = zoey is awesome?
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.
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.
Consider the output for 9 and 10 above. Why do these memory addresses typically change each time this program is run on nike?
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) Because s1 declared as char array.A char array end with ‘\0’ terminating element.
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;
Ans) Because it matching each characters of the s3 and s4, both are same , so return true.
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;
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’
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.
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.
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
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.
Ans) Both are displaying in Hexadecimal format. In hex representation contains 0-9 and A-F.