#include <iostream> using namespace std; int F(int x[], int c) { if (c < 3) retu
ID: 3841927 • Letter: #
Question
#include <iostream>
using namespace std;
int F(int x[], int c) {
if (c < 3) return 0;
return x[c - 1] + F(x, c - 1);
}
int G(int a, int &b) {
b = b - a;
a = b + a;
return a;
}
int main() {
int a = 4, b = 1;
int x[5] = {3, 1, 4, 1, 5};
string s = "Problem Number 2";
cout << x[2 + 2] + x[2] << endl; // line (a)
cout << s.substr(2, 3) << endl; // line (b)
cout << s.substr(s.find("b")) << endl; // line (c)
cout << G(b, a); cout << a << b << endl; // line (d)
cout << F(x, 5) << endl; // line (e)
return 0;
}
Can you please tell me the output of each line and explain to me why in simplest way? I am still a learner, Thank you very much!
Explanation / Answer
Hello answers are below respectively:
1. 9
a[2+2] is a [4] is 5 i..e not at 4th index as count starts from 0
a[2] is 4
5 +4 = 9 answer
2. obl
Substring from ("Problem Number 2") 2nd index with 3 character
starts from 0
P r o b l e m
0 1 2 3 4 5 6
i..e start from 2nd index which is 'o' alphabet and then 3 character from there
..i..e till 4th index as we need to include the 2nd index also as count begins from there
3. blem Number 2
this is string from the index where i find the first occurence of character 'b' which is 2nd index
print character from there onwards as we do not have any "ending index"
so it prints all charater strating from index 2nd
4. 431
Here lets break into 3 things
in function ( int a --> call by value) which means no change in the actual value when returns from function if something gets changed
in &b --> call by reference so , we are sending the actual address and hence the value of the variable changes when return from function if something gets changed
G(b,a) is the call of function and it matter what values is being passed and lets see the return value i..e a which goes through these operation , observe that values have been passed different
G(4,1) is called as below as all operation are inside this function only
==================================
b =4 ( pass by value , changes of b will be limited to function only)
a =1 (pass by reference ,so any change in a will be retained but actually ))
3 = 4-1 ( b = b - a)
4 = 3 +1
return 4
so which prints first "4"
=================================================
cout << a this is actaully changes as it's pass by referenc and it's 3
======================
cout << b this is actaully not changed anything as it's pass by valye so it's 1 ( as outissde the function call only
=============
5. 10
it's recurvie call and adding through 3rd index so
x[5] + x[4] +x[3]= 5 +4 +1 = 10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.