Write a method in C++ that: generates a stream of random integers between 0 and
ID: 3668785 • Letter: W
Question
Write a method in C++ that:
generates a stream of random integers between 0 and 80
for each random number, prints that many asterisks in a line
keeps generating random numbers until either a number divisible by 11 is generated or a total of 50 numbers has been generated
For example, suppose the numbers 7, 17, 21, 5 and 33 were generated. Then the output would be as follows:
Set up your main program to call this method 3 times. Before the method gets called, give a number of which call you're on, e.g. "Call 1:" and then the method's output. After each call, give a blank line.
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
using namespace std;
void display()
{
int arr[50];
int count;
int i=0;
int k=0;
int flag=0;
int temp=0;
do
{
flag=0;
temp=rand()%80;;
arr[i]=temp;
i++;
if(temp%11==0)
{
flag=1;
}
}while(flag!=1&&i<50);
string str="";
for(k=0;k<i-1;k++)
{
str="";
while(arr[k]--)
{
str=str.append("*");
}
cout<<str<<" ";
}
}
int main()
{
display();
display();
display();
return 0;
}
-------------------------------------
output
This is divisible by 11
***********************
******
*********************************************************
***********************************
This is divisible by 11
***************
**************************
************
*********
*************************************************************
******************************************
***************************
**************************************************
***********************************************************
***
******
************************************************************
This is divisible by 11
****************************************************
********************************************************
shekhar@shekhar-HP:~/Desktop/chegg/CS/cpp$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.