Exercise #1: Simple Usage of Arrays and Random Numbers Using a repetition loop (
ID: 3568701 • Letter: E
Question
Exercise #1: Simple Usage of Arrays and Random Numbers Using a repetition loop (for, while or do -while, the choice is yours), write a program that generates n random numbers in the range 0 to m inclusive. ? Asks the user to input two positive integers n and m. ? Asks the user to input a number (say value) between 0 and m. ? Write a program that generates the n numbers as required. ? The program stores the generated numbers in an array A[] (make the size of the array 100). ? The program then searches the array to find how many times value is found in the array A. Exercise #2: Simple Usage of Arrays, Random Numbers and User Defined Functions Write a program that generates 10 sentences each of which has the following syntax: A noun followed by the word likes followed by a fruit name Use the following noun and fruit names: boy, elephant, girl, dog, water, car, umbrella, chair Apple, Apricot, Avocado, Banana, Blueberry, Boysenberry, Cherry, GrapeExplanation / Answer
#include <iostream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
#include <string>
using namespace std;
int main ()
{
int xRan, n,m;
cout << "enter n amd m values" << endl;
cin << n << m;
int randaom_array[100];
int value;
cout << "enter value between 0 and " << m << endl;
srand( time(0)); // This will ensure a really randomized number by help of time.
for(int i = 0; i < n ; i++)
{
xRan=rand()%m+1; // Randomizing the number between 1-m.
random_array[i] = xRan;
}
int count_value = 0;
for(int i = 0; i < n ; i++)
if(value == array[i])
count_value++;
cout << "number of times " << " generated is : " << count_value << endl;
return 0;
}
exercise 2:
int main()
{
string noun[] = {"boy", "elephant", "girl", "dog", "water", "car" , "umbrella" , "chair"};
string fruit[] = { "apple","apricot","avocado","banana","blueberry","bousenberry", "cherry", "grape"};
srand( time(0)); // This will ensure a really randomized number by help of time.
for(int i = 0 ; i < 10 ; i++)
{
xRan = rand()%8 ; // Randomizing between the number 0-8
cout << noun[xRAn] << " likes " << fruit[xRAn] << endl;
}
return 0;
{
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.