Welcome back,
I am back with another program for all the readers. In today's program we are going to play with Array and write something using that.
Let's start,
You can copy the code from below :
// Function to generate a random number between min and max (inclusive)
int getRandomNumber(int min, int max) {
return rand() % (max - min + 1) + min;
}
int main() {
int min_interval, max_interval;
printf("Enter the minimum interval: ");
scanf("%d", &min_interval);
printf("Enter the maximum interval: ");
scanf("%d", &max_interval);
// Seed the random number generator
srand(time(NULL));
int random_number = getRandomNumber(min_interval, max_interval);
printf("Random number between %d and %d: %d\n", min_interval, max_interval, random_number);
int array_size = random_number + 1; // The size of the array will be one more than the random number
int random_array[array_size];
// Create the array one by one
for (int i = 0; i < array_size; i++) {
random_array[i] = i;
}
// Print the elements of the array
printf("Elements of the array:\n");
for (int i = 0; i < array_size; i++) {
printf("%d ", random_array[i]);
}
printf("\n");
return 0;
}
Let us understand this program,
We include the necessary header files:
stdio.h: For standard input/output operations like printf and scanf.
stdlib.h: For functions like rand and srand, which are used to generate random numbers.
time.h: For getting the current time to seed the random number generator.
We define a function getRandomNumber that takes in min and max as arguments and returns a random integer between min and max, inclusive.
The function uses the rand() function to generate a random number and then adjusts it to fall within the specified range.
In the main function:
We declare two integer variables min_interval and max_interval to store the user's input for the minimum and maximum intervals, respectively.
We prompt the user to enter the minimum and maximum intervals using printf and read their inputs using scanf.
We seed the random number generator using srand with the current time as the seed. Seeding is done only once in the program to ensure different random numbers are generated in each program execution.
We generate a random number random_number between the min_interval and max_interval (inclusive) using the getRandomNumber function.
We print the random number using printf.
We determine the size of the random_array as one more than the random_number, as mentioned in the problem.
We declare an array random_array of integers with a size of array_size.
We use a loop to create the array random_array one element at a time. The loop runs from 0 to random_number, and at each iteration, it assigns the value of i (which represents the current element) to the corresponding index in random_array.
After creating the array, we print its elements using another loop, showing the elements one by one.
The program will return 0 on successful execution of the program.
If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.
I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.
I am happy to call myself a trader and small programmer at the same time now.
Happy trading and keep learning what you love.