Question
please help urgent c++
Use the vector/array below for the following tasks: {25, 39, 12, 85, 55, 69, 40, 75} Task1 - [2 points] Put your name on the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

For the following program, Arrays are used to perform the following given tasks

Program:

#include <iostream>

using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main()
{
int array[]={25,39,12,85,55,69,40,75};
int grt40[8];
int count=0;
for(int i=0;i<8;i++)
{
if(array[i]>40)
{
grt40[count]=array[i];
count++;
}
}
cout<<"Output for Task 2"<<endl;
cout<<"Numbers greater than 40 are ";
for(int i=0;i<count;i++)
{
cout<<grt40[i]<<" ";
}
cout<<"\n\nOutput for Task 3";
cout<<endl<<"My vector is :"<<endl;
for(int i=0;i<8;i++)
{
cout<<array[i]<<" ";
}
cout<<endl<<"My sorted vector is : "<<endl;
int key, j;
for(int i = 1; i<8; i++)
{
key = array[i];//take value
j = i;
while(j > 0 && array[j-1]>key)
{
array[j] = array[j-1];
j--;
}
array[j] = key; //insert in right place
}
for(int i=0;i<8;i++)
{
cout<<array[i]<<" ";
}
cout<<"\n\nOutput for Task 4";
cout<<endl<<"Find 5";
int index1 = binarySearch (array, 0, 8-1, 5);
if(index1 == -1)
cout<<endl<<"5 cannot be found!";
else
cout<<endl<<"5 can be found at index : "<< index1;
  
cout<<endl<<"Find 75";
int index2 = binarySearch (array, 0, 8-1, 75);
if(index2 == -1)
cout<<endl<<"75 cannot be found!";
else
cout<<endl<<"75 can be found at index : "<< index2;
cout<<"\n\nOutput for Task 5";
cout<<endl<<"Five largest elements are : ";
for(int i=7;i>=7-5;i--)
{
cout<<array[i]<<" ";
}
return 0;
}

Output:

Output for Task 2 Numbers greater than 40 are 85 55 69 75 Output for Task 3 My vector is : 25 39 12 85 55 69 40 75 My sorted

Hope this helps!!

Add a comment
Know the answer?
Add Answer to:
please help urgent c++ Use the vector/array below for the following tasks: {25, 39, 12, 85,...
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++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Please use C++ Initialize the 2-D array of integers detailed below. { 25, 50,   75, 100,...

    Please use C++ Initialize the 2-D array of integers detailed below. { 25, 50,   75, 100, 125 }, { 50, 100, 150, 200, 250}, { 75, 150, 225, 300, 375}, { 100, 200, 300, 400, 500}, { 125, 250, 375, 500, 625 }, Traverse and output array elements for the 2D array above in reverse order. Starting with 625, 500, 375, 250, etc…. Directly Access and output 225 and 400 from the 2D array. DO NOT Traverse 2D Array

  • NEED C++ HELP. PLEASE FOLLOW DIRECTIONS CAREFULLY: Use a 1 dimensional array object to create a...

    NEED C++ HELP. PLEASE FOLLOW DIRECTIONS CAREFULLY: Use a 1 dimensional array object to create a 2 dimensional table object. Then modify to create a triangular table. Example: Objective -> create an array of dynamic objects of RowAray inside Table. See below for RowAray.h, Table.h, Then create a triangular table like the example above. Fill each cell with random 2 digit integers. The Example Table above has 8 columns of RowAray objects each filled with 6 rows of random 2...

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • 2. (10 pts. Please perform the following calculations via MATLAB a. Calculate e3!/pi and round the...

    2. (10 pts. Please perform the following calculations via MATLAB a. Calculate e3!/pi and round the answer to the nearest integer (all via MATLAB commands) b. Calculate cos(wt) for t (time) from 0 seconds to 10 seconds (with 1 second increments – created via colon operator) where w = 30°/seconds c. Please sort the values of the vector given below from smallest to largest and save it as another single column vector and G = [68, 83,61, 70, 75, 82,57,5,...

  • the following questions based on the titration curve below the following question 15 T 12 13...

    the following questions based on the titration curve below the following question 15 T 12 13 12 12 -04 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 volume of Na0H (1) (2pt) Read the second equivalence points from the plot? (2) (2pt) What is the pKaz of this acid? (2pt) Determine the

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