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 function called randFill that fills the entries of an array with random integers in...
Write a function called randFill that fills the entries of an array with random integers in the range from 10 to 99 (inclusive). (You should use a standard Java method to generate the values. Your solution should use no more than 6 lines of code.) For example, a program that uses the function randFill follows. public class P4 { public static void main(String args[]) { int x[]; x = randFill(5); for (int i = 0; i < 5; i++) System.out.print(x[i]...
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. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
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   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT