How to find output in c++ programmes
Answers
Answered by
3
1. Find the output of following C++ program.
#include<iostream> #include<stdlib.h> using namespace std; int main() { float x=5.999; float *y,*z; y=&x; z=y; cout<<x<<","<<*(&x)<<","<<*y<<","<<*z<<"\n"; return 0; }
Output
5.999,5.999,5.999,5.999
Explanation
The reason for this is x gives the value stored in the variable x. *(&x) gives the data value stored in the address &x i.e., the data value of x. Since y points to x (..y=&x), *y gives the value of x. And because z has the same address as that of y, *z also gives the value of x i.e., 5.999.
2. Find the output of following C++ program.
#include<iostream> using namespace std; int main() { int track[]={10,20,30,40},*striker; striker=track; track[1]+=30; cout<<"Striker>"<<*striker<<endl; *striker-=10; striker++; cout<<"Next@"<<*striker<<endl; striker+=2; cout<<"Last@"<<*striker<<endl; cout<<"Reset To"<<track[0]<<endl; return 0; }
Output
Striker>10 Next@50 Last@40 Reset To0
Explanation
The array track contains 4 elements {10,20,30,40} and the pointer striker holds the base address of the array track i.e, address of track[0].
1)*striker holds the data value of track[0] i.e, 10. Decrement in *striker by 10 makes the track[0]=0.
2)Incrementing pointer striker gives the next location of track i.e.,1. Now *striker gives the data value of track[1].
3) Again by incrementing by 2 , striker reaches to the 4 address of the array track i.e, track[4].
4)At last print the value at track[0] ,which is 0 (see step 1).
#include<iostream> #include<stdlib.h> using namespace std; int main() { float x=5.999; float *y,*z; y=&x; z=y; cout<<x<<","<<*(&x)<<","<<*y<<","<<*z<<"\n"; return 0; }
Output
5.999,5.999,5.999,5.999
Explanation
The reason for this is x gives the value stored in the variable x. *(&x) gives the data value stored in the address &x i.e., the data value of x. Since y points to x (..y=&x), *y gives the value of x. And because z has the same address as that of y, *z also gives the value of x i.e., 5.999.
2. Find the output of following C++ program.
#include<iostream> using namespace std; int main() { int track[]={10,20,30,40},*striker; striker=track; track[1]+=30; cout<<"Striker>"<<*striker<<endl; *striker-=10; striker++; cout<<"Next@"<<*striker<<endl; striker+=2; cout<<"Last@"<<*striker<<endl; cout<<"Reset To"<<track[0]<<endl; return 0; }
Output
Striker>10 Next@50 Last@40 Reset To0
Explanation
The array track contains 4 elements {10,20,30,40} and the pointer striker holds the base address of the array track i.e, address of track[0].
1)*striker holds the data value of track[0] i.e, 10. Decrement in *striker by 10 makes the track[0]=0.
2)Incrementing pointer striker gives the next location of track i.e.,1. Now *striker gives the data value of track[1].
3) Again by incrementing by 2 , striker reaches to the 4 address of the array track i.e, track[4].
4)At last print the value at track[0] ,which is 0 (see step 1).
Answered by
0
Answer:
#include<iostream.h>
#include<string.h>
using namespace std;
int main()
{
string x= "check";
string y="India";
cout <<x.length{}<<" " <<y.size;
return 0;
}
Similar questions