Why this C++ code output is
1
1
num is 3
Can u explain to me? Thank u
code:
#include using namespace std; void xFunction(int num) { do { if (num % 2!= 0) cout << num << " "; num--; } while (num >=1); cout << endl; } int main() { int num = 1; while (num <= 2) { xFunction(num); ++num; } cout << "num is " << num; return 0; }
first main function exicute with value of num=1
then while loop exicute for num=1 means while(1<=2) so condition
is true and loop exicuted, after exicution of loop function will be
call with value num=1
after function calling function defination will be exicuted then
exicute do while loop which is inside of function after first
iteration of do while loop exicute if satatement if(1%2!=0)
,statement is true so print 1 and value of num decrement with 1 so
new new value of num will be 0
then check do while condition while(0>=1) ,condition is false so
loop will be terminate and print new line and function terminate
and compiler will goes to main and exicute other satement of main
then exicute
++num and value of num will be 2 because num is a local variable so
value of num no change in calling function, again while loop
exicute for num=2 means while(2<=2) so condition is true and
again function will be call with value num=2
after function calling function defination will be exicuted then
exicute do while loop which is inside of function after first
iteration of do while loop exicute if satatement if(2%2!=0)
,statement is false so direct go to num-- and value of num will be
decrement with 1 so new new value of num will be 1
then check do while condition while(1>=1) ,condition is true so
again exicute loop then exicute if satatement if(1%2!=0) ,statement
is true so print 1 and value of num decrement with 1 so new new
value of num will be 0 then check do while condition while(0>=1)
,condition is false so loop will be terminate and print new line
and function terminate and compiler will goes to main and exicute
other satement of main then exicute
++num and value of num will be 3 because num is a local variable so
num variable no change in calling function again while loop exicute
for num=3 means while(3<=2) so condition is false so loop will
be terminate and print 3
so output :
1
1
3
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }
C++ 1. What is the final output? 2. Why is 2 the ouput for (int)*c? Please explain what happens during the line c = c + *p +1; #include <iostream> using namespace std; int main() { int arr[5] = { 5,2,3,1,4 }; int* p = arr; cout << (int)*p << endl; unsigned char* c = (unsigned char*)arr; p = p + 2; c = c + *p + 1; cout << (int)*p...
Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
C++ Type in the above program . Before running the program, change each of the question marks to the numbers 1 through 5 indicating the order in which that line will be executed. Run the program and check your numbers. Fix and rerun if necessary. Submit the program and the output. #include <iostream> using namespace std; //prototypes void DemonstrateProc1(); void DemonstrateProc2(int num); int main() { cout << "?. Execution starts with main. " << endl; DemonstrateProc1(); cout << "?....
Id: 40100885 in c++ output text please thanks! what is the output values printed by the following code? You need to explain step by step how each printed value is calculated. #include <iostream> using namespace std; int m = 0; void SampleMethod (int); int SampleMethod(); void increase(); int main() { int j = 9; SampleMethod(j); cout << j<<endl; return 0; w Y == void SampleMethod(int i) { if (j%2 1) cout << SampleMethod() <<endl; else cout << j << "...
c++ only. Please read directions. I'm looking to have each line re-written in a way that shows we're using the walk through method that the pointer is shifting by each value to solve this. Example far below shows what not to do. Rewrite this program and remove the variable declared at line A below. Make your revised program generate the exact same output as the original without using the variable declared at line A. #include <iostream> using namespace std; int...
This is a c++ code /** Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...