Question

In: Computer Science

Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...

  • Creates a 100-element array, either statically or dynamically
  • Fills the array with random integers between 1 and 100 inclusive
  • Then, creates two more 100-element arrays, one holding odd values and the other holding even values.
  • Prints both of the new arrays to the console.

In C++. Thank you!

Solutions

Expert Solution

C++ CODE:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
    int arr[100];//Create 100 elements array
    srand(time(NULL));//Seeds for rand()
    //Fill random numbers in arr
    for(int i = 0; i < 100; i++)
        arr[i] = rand() % 100 + 1;//Fill with random numbers between 1 and 100

    int oddArr[100], evenArr[100];//Create two 100-elements array
    int oddArrSize = 0, evenArrSize = 0;

    for(int i = 0; i < 100; i++)
        if(arr[i] % 2 == 0)//If element is even
            evenArr[evenArrSize++] = arr[i];
        else//Otherwise element is odd
            oddArr[oddArrSize++] = arr[i];

    //Print both odd and even arrays
    cout << "Odd array: ";
    for(int i = 0; i < oddArrSize; i++)
        cout << oddArr[i] << " ";
    cout << endl << endl;

    cout << "Even array: ";
    for(int i = 0; i < evenArrSize; i++)
        cout << evenArr[i] << " ";
    cout << endl << endl;

    return 0;
}

SAMPLE OUTPUT:


Related Solutions

Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }.  For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this assignment,...
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13    count: 5    pid: 13342 $> query: 22   ...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT