Generate an array of random numbers in C++
1)The size of the array is defined by the user
2) The application is to take every odd number and find the mean.
Program has to use structured programing and pointers
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(NULL));
int n;
cout<<"Enter size: ";
cin>>n;
int* arr=new int[n];
for(int i=0;i<n;i++)
{
*(arr+i)=rand()%100+1;
}
double avg=0;
for(int i=0;i<n;i++)
{
if(*(arr+i)%2==1)
avg+=*(arr+i);
}
avg=avg/(double)n;
cout<<"Mean of odd numbers in array are:
"<<avg<<endl;
return 0;
}

Kindly revert for any queries
Thanks.
#include <iostream>
#include <vector>
#include <cstdlib> // For rand(), srand()
#include <ctime> // For time()
// Function to generate an array of random numbers
int* generateRandomArray(int size) {
int* arr = new int[size]; // Allocate memory for the array
// Seed the random number generator
srand(static_cast<unsigned int>(time(0)));
for (int i = 0; i < size; ++i) {
arr[i] = rand() % 100; // Generate random numbers between 0 and 99
}
return arr;
}
// Function to calculate the mean of odd numbers in an array
double calculateMeanOfOddNumbers(int* arr, int size) {
std::vector<int> oddNumbers; // Store odd numbers found
for (int i = 0; i < size; ++i) {
if (*(arr + i) % 2 != 0) { // Check if the number is odd using pointer arithmetic
oddNumbers.push_back(*(arr + i));
}
}
if (oddNumbers.empty()) {
return 0.0; // Return 0 if no odd numbers are found
}
double sum = 0.0;
for (int num : oddNumbers) {
sum += num;
}
return sum / oddNumbers.size();
}
int main() {
int size;
// Get the size of the array from the user
std::cout << "Enter the size of the array: ";
std::cin >> size;
// Generate the random array
int* randomArray = generateRandomArray(size);
// Display the generated array
std::cout << "Generated Array: ";
for (int i = 0; i < size; ++i) {
std::cout << *(randomArray + i) << " "; // Display using pointer arithmetic
}
std::cout << std::endl;
// Calculate and display the mean of odd numbers
double meanOfOdd = calculateMeanOfOddNumbers(randomArray, size);
std::cout << "Mean of Odd Numbers: " << meanOfOdd << std::endl;
// Deallocate the dynamically allocated memory
delete[] randomArray;
return 0;
}
C++#include <iostream>#include <vector>#include <cstdlib> // For rand(), srand()#include <ctime> // For time()// Function to generate an array of random numbersint* generateRandomArray(int size) { int* arr = new int[size]; // Allocate memory for the array
// Seed the random number generator
srand(static_cast<unsigned int>(time(0)));
for (int i = 0; i < size; ++i) {
arr[i] = rand() % 100; // Generate random numbers between 0 and 99
} return arr;
}// Function to calculate the mean of odd numbers in an arraydouble calculateMeanOfOddNumbers(int* arr, int size) { std::vector<int> oddNumbers; // Store odd numbers found
for (int i = 0; i < size; ++i) { if (*(arr + i) % 2 != 0) { // Check if the number is odd using pointer arithmetic
oddNumbers.push_back(*(arr + i));
}
} if (oddNumbers.empty()) { return 0.0; // Return 0 if no odd numbers are found
} double sum = 0.0; for (int num : oddNumbers) {
sum += num;
} return sum / oddNumbers.size();
}int main() { int size; // Get the size of the array from the user
std::cout << "Enter the size of the array: "; std::cin >> size; // Generate the random array
int* randomArray = generateRandomArray(size); // Display the generated array
std::cout << "Generated Array: "; for (int i = 0; i < size; ++i) { std::cout << *(randomArray + i) << " "; // Display using pointer arithmetic
} std::cout << std::endl; // Calculate and display the mean of odd numbers
double meanOfOdd = calculateMeanOfOddNumbers(randomArray, size); std::cout << "Mean of Odd Numbers: " << meanOfOdd << std::endl; // Deallocate the dynamically allocated memory
delete[] randomArray; return 0;
}
Explanation:
generateRandomArray(int size):
Takes the desired array size as input.
Dynamically allocates memory for the array using new int[size].
Seeds the random number generator using srand(time(0)) to ensure different random numbers each time the program runs.
Generates random numbers between 0 and 99 (you can change the range by modifying rand() % 100) and stores them in the array.
Returns a pointer to the generated array.
calculateMeanOfOddNumbers(int* arr, int size):
Takes the array pointer and size as input.
Creates a std::vector<int> to store the odd numbers found in the array.
Iterates through the array using pointer arithmetic (*(arr + i)) to access elements.
Checks if each element is odd using the modulo operator (% 2 != 0).
If an element is odd, it's added to the oddNumbers vector.
Calculates the sum of the odd numbers and divides by the count to find the mean.
Returns the mean (or 0.0 if no odd numbers are found).
main():
Gets the array size from the user.
Calls generateRandomArray() to create the array.
Displays the generated array using pointer arithmetic.
Calls calculateMeanOfOddNumbers() to get the mean of odd numbers.
Displays the calculated mean.
Crucially, deallocates the memory allocated for the array using delete[] randomArray to prevent memory leaks.
Key Concepts:
Pointers: The code uses pointers (int*) to work with arrays and access elements using pointer arithmetic.
Dynamic Memory Allocation: new and delete[] are used to allocate and deallocate memory dynamically, allowing the array size to be determined at runtime.
Structured Programming: The code is organized into functions (generateRandomArray, calculateMeanOfOddNumbers) for better readability and maintainability.
Vectors (Optional but Recommended): Using std::vector to store the odd numbers makes the code more flexible and easier to manage, as you don't need to know the exact number of odd numbers in advance.
Random Number Generation: srand() and rand() are used to generate random numbers.
Generate an array of random numbers in C++ 1)The size of the array is defined by...
1. Write a C code that do the following: Generate array of random integer numbers of size 25 Fill the array with random integers ranging from [1-100] Find the maximum number of the array and its location Find the minimum number of the array and its location Search for a number in the array Print the array Flip the array Sort this array Quit the program when the user choose to exit
cout<<"blah<<endl; no std:cout C++ pointers Generate 25 integer random numbers between 0-100 and place them in a file, each number on a single line.. Dont forget to close the file once the numbers are generated. Then read the file and create a dynamic array that holds these numbers. The size of the array is the numbers read.
Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...
Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5 digits. Of those numbers give me the following information: - Mean , Mode, Median, Range. - Five times, ask the user for a number within the range (from above) and record the time to find the number in the range. (Loop count) 2. Using a any sort algorithm, build sorted 2D array of random numbers (1 TO 5 digits ) with the x direction...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
in c
Q3) Write a program to generate three random numbers between 1 and 9 and display them. The user has to enter their sum as soon as possible. These two steps will be repeated continuously. An alarm will be triggered every 5 seconds to display the numb answers so far. If the user did not give a correct answer in 10 seconds, the timer will jump to the main to give an error message "Too slow response!" and terminate...
Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...
Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...
Q1. Write a program in Java a. Create 2 separate arrays, of user defined values, of same size b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....
Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum. It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...