I need a short program to perform a few functions surrounding random number gene
ID: 3543232 • Letter: I
Question
I need a short program to perform a few functions surrounding random number generation. THE PROGRAM MUST BE IN C.
For the prelab implement following functions.
int generateNumber(void): This function generates a number between 0-9 and returns that number.
Use the rand() function and the above example to generate number between 0-9.
int cube (int ): This function takes a integer number and returns the cube of the number. The integer
number send to this function comes from the generateNumber function.
float division (int , int): This function takes two integer numbers and the returns the division result of
the two number. Before dividing the numbers you need to find out which number is bigger and then
divide the bigger number by the smaller number and return the result. Similar to the function above inputs
to this function are random numbers generated by the generateNumber function. If one of the number is
zero then return 0.Function should correctly handles all the cases, handling zero as input and dividing
bigger number by smaller number, correctly.
int main(): Put the following line in the main() before calling generateNumber function.
srand(time(NULL));
The purpose of the above line is to put seed for the random number generator function so that every time
the program is run a new random number is generated. Part of the code for the main is shown below.
Call the generateNumber function to generate random numbers. Use these numbers as inputs for functions
cube and division. Include the stdlib.h and time.h header file to use the rand, srand and time functions.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int generatenumber()
{
int x;
x = rand()%10;
return x;
}
int cube(int x)
{
return x*x*x;
}
float division(int x, int y)
{
float z;
if(x!=0&&y!=0)
{
if(x>y)
{
z=x/y;
}
else
{
z=y/x;
}
return z;
}
else
return 0;
}
int main(void) {
srand(time(NULL));
int x,y,a,b;
float z;
x=generatenumber();
y=generatenumber();
a = cube(x);
b = cube(y);
z = division(x,y);
printf("Cube of %d is %d ",x,a);
printf("Cube of %d is %d ",y,b);
printf("Div(%d,%d) is %.2f ",x,y,z);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.