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
Can you help me explain how fork, getpid(), cout work in this code? I got a quiz today which asked me how many times cout and fork() is executed. #include <iostream> #include <unistd.h> using namespace std; int main ( int argc, char *argv [] ) { int pid; cout << getpid()<<endl; pid = fork(); if (pid == 0) { cout << getpid() <<endl; fork(); cout << getpid()<<endl; fork(); cout << getpid()<<endl; } return 0; }
Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack { int s[MAX]; // an array of integers int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...
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;...
1. What would be the output of the following lines of code: int num = 2; switch(num){ case 1: cout << "1"; case 2: cout << "2"; case 3: cout << "3"; case 4: cout << "4"; } 2. What would be the output of the following code: char op = '*'; switch(op){ case '+': cout << "Addition"; break; case '-': cout << "Subtraction"; break; case '/': cout << "Division"; break; default: cout << "Invalid"; } 3. What would be...
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 << "?....