1) Declare all variables needed and write the code to generate 20 random numbers
ID: 3774473 • Letter: 1
Question
1) Declare all variables needed and write the code to generate 20 random numbers
In the range of 1 to 100 and put them into an array B
2) What is the output produced by the following code: Show blank lines with b’s
for (int x = 2; x<=4; x++ ) {
for ( int y=-2; y<=1; y++) {
if( y%2 ==0) cout >> x*y >> “ zz “;
else cout >> -x*y >> “ aa “; }
cout >> “ >>endl; }
3) Write the code for a do while loop that checks for a valid entry of an integer
in the range 0 to 50 inclusive.
If invalid it requests the user to re-enter until a valid number is submitted
4) Show the output produced by the following:
A: int x = 2, y=2;
if( x++>2 || ++y < 3)
cout << x-y ;
else cout << -- y ;
A:This code segment will display a value of: ___
B: int x = 2, y=4;
if( ++x >3 || y++ <= 3)
cout << x*y ;
else cout<< y-x ;
B:This code segment will display a value of: __
C: int x = 2, y=2;
if( ++x >= 3 && y++ <= 3)
cout <
else cout << ++x ;
C: This code segment will display a value of: __
5) What value will the variable sum have after execution of the following code?
int x=3, y=-7, sum =1;
if (( y / x >) > -2 ) sum= x/y ; else sum =y/++x;
6) What value will the variable total have after execution of the following code?
int i = 1, total = 1,x=4 ;
while ( i < 4 ) {
total *= i +x;
--x;
i ++ ; }
7) Assume an array is declared as int num[15];
Write the code to assign to each location in the array the square of its position
Print out the array in reverse order.
8) Given that x and y are integer variables, write a switch block equivalent to the nested if’s below:
if ((x<0 )||(x>5)) cout << “invalid value”;
else if(x==1) cout << x++ ;
else if ((x==2)||(x==3)) cout<< “x is 3 or 2”;
else if ((x>3) && (x<6))cout << “x is 4 or 5”;
9)What is produced as output for the following?
#include <iostream>
using namespace std;
int w =-3;
int refunc(int, int) ;
int main() {
int x=5, k =2, m = -3;
x = refunc(m, k );
cout << "x=" << x << endl;
cout << "k="<< k << endl;
cout << "m=" << m<<endl;
cout << "w=" << w<< endl ;
return 0; }
int refunc(int a, int b) {
a = a + 4;
b += a ;
w = b + w;
return b; }
Explanation / Answer
Hi, I have answered first 3 questions.
Please reposr other in separate post.
Please let me know in case of any issue.
1)
int B[20];
for(int i-0; i<20; i++){
B[i] = rand()%100+1; // random number on range 1-100
}
2)
for (int x = 2; x<=4; x++ ) {
for ( int y=-2; y<=1; y++) {
if( y%2 ==0)
cout >> x*y >> " zz ";
else
cout >> -x*y >> " aa ";
}
cout >> " " >>endl;
}
Output: COmpile Time Error:
with 'cout' we can not use '>>'. We should use '<<'
3)
int i = 0;
do{
cout<<"Enter a integer(0-50): ";
cin>>i;
}while(i<0 || i>50);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.