Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main.
Here is an example run:
Enter array size: 10 //your code MUST work for any other size > 0
Original array:
arr[0]= 81
arr[1]= 94
arr[2]= 90
arr[3]= 1
arr[4]= 77
arr[5]= 5
arr[6]= 49
arr[7]= 99
arr[8]= 1
arr[9]= 52
Indices of values > 50:
arr[0]= 0
arr[1]= 1
arr[2]= 2
arr[3]= 4
arr[4]= 7
arr[5]= 9
Press any key to continue . . .
Copy this code and declare and implement the returnIndexOfelementsGreaterThan(…) function.
This code does not compile as the function is not declared, so you need to declare it and implement it.
#include <iostream>
#include <ctime>
using namespace std;
void printArr(int* arr_in, int size);
int main()
{
int size;
cout << "Enter array size: ";
cin >> size;
if (size<0) {
cout << "Size should be positive!\n";
exit(0);
}
int* arr_in = new int[size];
srand(time(0));
for (int i = 0; i<size; i++) {
arr_in[i] = rand() % 101;
}
cout << "Original array: \n";
printArr(arr_in, size);
int sizeOfnewArray = 0;
int * indices = returnIndexOfelementsGreaterThan(arr_in, size, sizeOfnewArray);
cout << "Indices of values > 50:\n";
printArr(indices, sizeOfnewArray);
delete[] arr_in;
delete[] indices;
return 0;
}
void printArr(int* arr_in, int size){
cout << "\n";
for (int i = 0; i<size; i++)
cout << "arr[" << i << "]= " << arr_in[i] << "\n";
cout << "\n";
}
cpp code:
#include <iostream>
#include <ctime>
using namespace std;
//function declaration
void printArr(int * arr_in, int size);
int* returnIndexOfelementsGreaterThan(int *arr_in,int size,int
&sizeOfnewArray);
int main()
{
int size;
//get the size of array
cout<<"Enter array size: ";
cin>>size;
//if negative, print error msg
if(size<0){
cout<<"Size should be positive!\n";
exit(0);
}
//allocate memory for array
int * arr_in=new int[size];
srand(time(0));
//assign random values
for(int i=0;i<size;i++){
arr_in[i]=rand()%101;
}
//call print function, print the original array
cout<<"Original array: \n";
printArr(arr_in,size);
//initialize new array to 0
int sizeOfnewArray=0;
//call function
int *indices
=returnIndexOfelementsGreaterThan(arr_in,size,sizeOfnewArray);
cout<<"Indices of values > 50:\n";
//print array greater than 50
printArr(indices,sizeOfnewArray);
//deallocate memory
delete[] arr_in;
cout << endl;
return 0;
}
//print the array function
void printArr(int* arr_in, int size){
cout<<"\n";
//all elements in array
for(int i=0;i<size;i++)
cout<<"arr["<<i<<"]=
"<<arr_in[i]<<"\n";
cout<<"\n";
}
//function that return array of index ,elements greater than
50
int* returnIndexOfelementsGreaterThan(int* arr_in, int size, int
&sizeOfnewArray)
{
//new arr
int * arr=new int[size];
//go through all elements in array
for(int i=0;i<size;i++)
{
//if elements greater than 50
if(arr_in[i]>50)
{
//add indices to new array
arr[sizeOfnewArray]=i;
//increase the size
sizeOfnewArray++;
}
}
//return new array
return arr;
}
output:
//for any clarification please do comments. if you found this solution useful, please give me thumbs up
Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...
/* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...
My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...
Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...
9. The purpose of the following program is to generate 10 random integers, store them in an array, and then store in the freq frequency array, the number of occurrences of each number from 0 to 9. Can you find any errors in the program? If yes, correct them. #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 int main(void) int i, num, arr[SIZE], freq[SIZE]; srand (time (NULL)); arr[1] rand(); SIZE; for(i i 〈 i++) num arr[i]; freq[num]++; printf("InNumber occurrences...
Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() { const int n = 50; int *arr,...
How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...