Question

Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

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";

}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

C:\Users\NIROSHINI\Documents\CPPFiles\ElementGreaterlndex\bin\Debug\ElementGreaterlndex.exe Enter array size: 10 Original arr

//for any clarification please do comments. if you found this solution useful, please give me thumbs up

Add a comment
Know the answer?
Add Answer to:
Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (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...

    (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....

  • 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...

    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...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    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....

  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

  • //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

    //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;        int size;        int capacity;               void resize(){            capacity*=2;            T * newArr = new T[capacity];            for(int i=0;i<size;i++){                newArr[i] = arr[i];            }            delete(arr);            arr = newArr;                   }           public:       ...

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    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...

  • C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with...

    C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with the equation std=sqrt(sum((xi-mean)2/(n-1))) Sum the square of difference of each value with the mean. Divide that sum by n-1. Take the square root and you have the standard deviation. //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Srand #include <ctime> //Time to set random number seed #include <cmath> //Math Library #include <iomanip> //Format Library using namespace std; //User Libraries //Global Constants, no Global Variables...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* 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...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT