0% found this document useful (0 votes)
37 views6 pages

Assignment 6 Op1 Functions Passing Variable To Functions KhoaNV15

The document contains an assignment on C++ functions and passing variables to functions. It includes 5 questions/problems related to C++ functions and references: 1. A problem asking to pass pointers by reference to a swap function to correctly swap variable values. 2. A program to compare the lengths of two input strings and output the longer string. 3. Determining the output value of variable v when calling functions that pass by reference and value and increment the variable. 4. Identifying which statement about references vs pointers is incorrect. 5. Identifying what is wrong about the statement that a reference can be reassigned to refer to a different object.

Uploaded by

Minh Thuận
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Assignment 6 Op1 Functions Passing Variable To Functions KhoaNV15

The document contains an assignment on C++ functions and passing variables to functions. It includes 5 questions/problems related to C++ functions and references: 1. A problem asking to pass pointers by reference to a swap function to correctly swap variable values. 2. A program to compare the lengths of two input strings and output the longer string. 3. Determining the output value of variable v when calling functions that pass by reference and value and increment the variable. 4. Identifying which statement about references vs pointers is incorrect. 5. Identifying what is wrong about the statement that a reference can be reassigned to refer to a different object.

Uploaded by

Minh Thuận
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CODE: CPP.Assignment06.

Opt1
Assignment topic : CPP Functions, Passing FRESHER
variable to functions ACADEMY
Assignment duration : 180 minutes

Bài 1. Below example tries to swap value of evenNumber and oddNumber but it does not works.
Please correct it by trying passing reference of pointer to swap function.

void swap(int evenNumber, int oddNumber)


{
int temp = evenNumber;
evenNumber = oddNumber;
oddNumber = temp;
}
int main(int argc, char *argv[])
{
int evenNumber = 2;
int oddNumber = 3;

cout << "evenNumber: " << evenNumber << " ,oddNumber : " << oddNumber << endl;
swap(evenNumber, oddNumber);
cout << "evenNumber: " << evenNumber << " ,oddNumber : " << oddNumber<< endl;

return 0;
}

Result:
evenNumber: 2 ,oddNumber : 3
evenNumber: 2 ,oddNumber : 3

#include <iostream>
using namespace std;

void swap(int &evenNumber, int &oddNumber)


{
int temp = evenNumber;
evenNumber = oddNumber;
oddNumber = temp;
}
int main(int argc, char *argv[])
{
int evenNumber = 2;
int oddNumber = 3;

cout << "evenNumber: " << evenNumber << " ,oddNumber : " << oddNumber << endl;
swap(evenNumber, oddNumber);
cout << "evenNumber: " << evenNumber << " ,oddNumber : " << oddNumber<< endl;

return 0;
}

Bài 2. Write a C++ program that compare length of 2 input strings and return the string that has longer
length.
User will input 2 strings step by step, then program will output the string that is longer.
For example: string1 = “abc”, string2 = “qwer”. Output should be string2 = “qwer”.
#include <iostream>
#include <string>
using namespace std;

main()
{ int x;
string string1,string2;
cout<<"nhap vao do dai xau 1 va 2 "<<endl;
getline( cin,string1);
fflush(stdin);
getline(cin,string2);
if (string1.size()==string2.size())
{
cout<<"2 chuoi bang nhau";
}

else if (string1.size()<string2.size())
{
cout<<string2;
}
else cout<<string1;
}
Bài 3. What is the output when trying to print the value of v.

void incr1(int& x){ // increase 1


x++;
}
int incr2(int x){ // increase 1
return x++;
}
int v = 2;
incr1(v);
v = incr2(v);
A. 2
B. 3
C. 4
D. 5
Biến v được tăng khi gọi hàm incr1.

#include <iostream>
using namespace std;
void incr1(int& x)
{ // increase 1
x++;
}
int incr2(int x)
{ // increase 1
return x++;
}
main()
{
int v = 2;
incr1(v);
v = incr2(v);
cout<<v;
}

Bài 4. Which sentence below is incorrect. Explain?


A. Use reference when needed, and pointer when you have to.
B. If you want to change the object passed, call by reference and use a pointer.
C. Reference must refer to an object, not a dereferenced null pointer.
D. Pointers are usually preferred over references.

Vì 3 phần ABC đúng còn phần D theo cá nhân mỗi người nên có thể sai.

Bài 5. What is wrong about reference? Explain?


A. To get a pointer to the object denoted by a reference rr, use like &rr.
B. Reference could be see as a const pointer.
C. You can reseat a reference to make it refer to a different object.
D. Value of reference cannot be changed after initialation. It always refers to the object it was
initialized to denote.

Em tra GG thấy đáp án C đúng ạ.

You might also like