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