Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Goals: Practicing functions Create a program in Visual Studios C++ that utilizes

ID: 3674419 • Letter: G

Question

Goals: Practicing functions

Create a program in Visual Studios C++ that utilizes a function to generate a “random” number for simulating rolling a die for a game. The functions should accept the number of sides for the die as an input parameter and return a generated number between 1 and the number of sides (inclusive) to the function call as output. Make sure your program does not generate the same “random” numbers in the same order each time you run the program.

The main function should call the function at least 5 times using differing number of sides, store the returned “random” numbers in variables, and output the “random” numbers. The function calls may use values that the user was prompted to enter (then you have to write the prompts and store the user’s input) or may be values that you as the programmer choose and hardcode.

Do not use the same identifiers in main and in your function. Also put your function definition after main.

Explanation / Answer

//pre-processor
#include<iostream>
#include<stdlib.h>
using namespace std ;

//function prototype to get random number
int getRandom(int side);
//the main function
int main() {
   
   int numberOfSide;
   for(int i=1; i<=5; i++){
       cout<<"Enter number of side of dice: ";
       cin>>numberOfSide;
       
       cout<<"Rolled Value: "<<getRandom(numberOfSide)<<endl;
   }
   return 0 ;
}

// function definition
int getRandom(int side){
   //srand(time(NULL));
   int random = 1 + rand()%side; // random number [1-side]
   return random;
   }

/*

Output:

Enter number of side of dice: 9
Rolled Value: 7
Enter number of side of dice: 5
Rolled Value: 3
Enter number of side of dice: 2
Rolled Value: 1
Enter number of side of dice: 4
Rolled Value: 1
Enter number of side of dice: 7
Rolled Value: 7

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote