CIS-251 “C++ Programming” Program #8 (Searching and Sorting Arrays) 1) Using Visual Studio or your selected IDE, create

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

CIS-251 “C++ Programming” Program #8 (Searching and Sorting Arrays) 1) Using Visual Studio or your selected IDE, create

Post by answerhappygod »

CIS-251 “C++ Programming” Program #8 (Searching and SortingArrays)
1) Using Visual Studio or your selected IDE, create a C++program solution for challenge described in Step #3 below.
2) You must use good programming style as shown in the examplesin the textbook to include ample comments in your source code. Youmust include helpful information in a header block such as theprogram description, the source of your program, your name, and thedate.
3) Write a C++ program that performs specific searching andsorting exercises of an array of integers. This program has sixrequired outputs, and each must be numbered. Start by initializingan 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. Then display the unsorted values. This is requiredoutput #1 of 6 for this program. Using the linear, or sequential,search of the unsorted array, determine and report the 1-relative(i.e. 1st, 2nd, 3rd, 4th, etc.) positions of the following numbersin the array (or -1 if not found), and the number of searchesrequired to locate each of the following numbers: 25, 30, 50, 75,and 92. This is required output #2 of 6. Then display the totalnumber of searches for all five numbers. If a number wasn’t found,then add in the total elements in the array for that search attemptsince they obviously were all looked at. This is required output #3of 6. Then sort the numbers using any algorithm of your choice(e.g. bubble sort, selection sort, etc.) and then display thesorted array. This is required output #4 of 6. Next, using a binarysearch of the sorted array, determine and report the 1-relativepositions of the following numbers in the array (or -1 if notfound), and the number of searches required to locate each of thesame numbers as before: 25, 30, 50, 75, and 92. This is requiredoutput #5 of 6. Finally, display the total number of searches forall five numbers. This is required output #6 of 6. Your programmust only execute a single time to produce all six results.
CAN ANYBODY HELP ME WITH THIS HOMEWORK? I have no idea what todo. WHATS THE FULL CODE LOOK LIKE? I"M HAVINGTROUBLE>>>
Expert Answer
Anonymous answered this95 answers
Let me, first of all, explain the question and what it says.
(2). You can use your book to understand the coding format whichis mentioned here as well that include as many comments as well sothat others can understand your code, then write your name, date onwhich day you wrote this program and source means from where didyou write this program.
For example, The header can be this way which you can writeabove all the code:
/* C++ programming: Concept-> searching and sorting.
Name YourName.
Date: The given Date on which day you wrote this program.
Source: Own.
*/
The above comment is called a multiline comment and this symbolyou can use to write a multiline comment./* */
In between /* */ This you can write as many comments as much asyou want.
There is another way to do a comment in c++ by using a doubleslash that is using this symbol // which is also called asingle-line comment.
That means in the same line comment will be interpreted as acomment by the compiler but if the comment goes below that line thecompiler will think of this as code.
And hence you can use single line comment when the comment isshort for example:
//This is an explanation of different comment symbols used inc++
And you can use multiline comments when you want to give a longdescription of the code.
For example:/* This is an explanation of multiline comment. Amultiline comment is used when we have to describe a lot about theprogram and the comment is long.
It is a good practice to use the multiline comment for a headeras well.*/
Perfect now hope you have understood what the question initiallyasked and hence kindly refer to your textbook as well for theformatting of the code. Because you wanted any help and therefore Iwas eager to help in this context.
Now let me make you understand the third party.
(3).First of all you have to create an array and store thesenumbers:
23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88,8,97, 25, 50, 75, 61, and 49.
Let me tell you this in a step-by-step manner.
Step1:
int arrayOfNumbers[]={23,17,5,90,12,44,38,84,77,3,66,55,1,19,37,88,8,97,25,50,75,61,49};
Step2: Display the elememts of thearrayOfNumbers.
size_t sizeOfTheArrayOfNumbers =sizeof(arrayOfNumbers)/sizeof(arrayOfNumbers[0]);
// loops to each element of the arrayOfNumbers and prints eachelement with space.
for (size_t i = 0; i < sizeOfTheArrayOfNumbers; i++) {
cout << arrayOfNumbers << ' ';
}
Step3:Find the index of: 25, 30,50,75, and 92in arrayOfNumbers. That means searching these numbers in thearrayOfNumbers.
If the numbers are found then display the index of that numberotherwise display -1.
Also, display how many searches are required to search abovementioned numbers.
Because you know that in an array the number stored will have anindex and hence if that number is available in the array then youcan return that index.
Use linear search to search the number from arrayOfNumbers.
Let me tell you that linear search is a very simple searchingalgorithm where you search the element sequentially for example youstart the 0th index and if the element is not found here then yousearch at the 1st index and then at the second index and if thesearching element you found then you return the index where theelement was found.
For example: if we have 5 numbers let us 50 70 90 100 250 350and you want to search 70 then you will start from 50 and then youwill compare 50 and 70 and since they are not equal then you willgo to the next number that is 70 and you will compare 70 with 70and since they are equal and hence 70 is available in the givenarray of numbers.
And you can return the index of that number in this case if youassume 0-based indexing then 70 is found at index 1.
Otherwise, return -1.
Let me show the code for our given numbers to search:
These numbers you have to search: 25, 30,50,75, and92. in this array of numbers-> 23, 17, 5, 90, 12,44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61,and 49.
Here is the code:
int searchAnumberInArrayOfNumbers(int
return i;// for finding a number of searches when you do step 4.Look at step 4.
}
}
else{
return -1;
}
Step4:Print the total number of searches forall five numbers.
If a number was not found in the arrayOfNumbers then print t thenumber of searches needed equal to the length of thearrayOfNumbers.
For example, you are searching for 92 in the arrayOfNumbers asince 92 is not available in arrayOfNumbers and since 23 elementsare there in arrayOfNumbers and hence display 23 searches needed totell that 92 is not available in the arrayOfNumbers.
Here is the code:
int getIndex1=searchAnumberInArrayOfNumbers(arrayOfNumbers,25);
intgetIndex2=searchAnumberInArrayOfNumbers(arrayOfNumbers,30);
intgetIndex3=searchAnumberInArrayOfNumbers(arrayOfNumbers,50);
intgetIndex4=searchAnumberInArrayOfNumbers(arrayOfNumbers,75);
intgetIndex5=searchAnumberInArrayOfNumbers(arrayOfNumbers,92);
if(getIndex1!=-1){
cout<<"the number of searches required for 25is:"<<getIndex+1;
}
else{
cout<<" The given number is not available inarrayOfNumbers and total searches done are:"<<23;
}
//And in the same way you can write for all other numbers.
Step5:Sort the arrayOfNumbers using any sortingalgorithms of your choice.
And then print sorted arrayOfNumbers.
Here is the code:
Step6: Using binary search algorithmsearch these numbers 25, 30, 50, 75, and92 in 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55,1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49.
And return the index of the number if the searching number isfound and return -1 if the number is not found.
Also, return the number of searches required to search eachnumber.
For example, the element found at the 5th index is then print5th index.
And display total seraches=6.
Binary search is more efficient than linear search however letme tell you that the elements in the array must be sorted.
And let me explain to you the idea of binary search:
Step1: You find the middle index of the array.
Step2: Let us say you are searching for a number n in an arraythen compare that number with the number present at the middleindex and if both are equal then return the middle index.
arrayOfNumbers[],int number){
for(int i=0;i<sizeOfTheArray;i++){
Situation 2: The searching element is smaller than the elementpresent at the middle index.
And kindly note that here we are talking about the solution whenthe array is sorted in ascending order.
For situation 1 search the element towards the left side of themiddle index.
That is from the start index and till it's less than the middleindex.
And for situation two search the element towards the right sideof the middle index that is from the middle index till the lastelement of the sorted array.
And since you skip one side totally while searching andtherefore it's the most efficient searching algorithm.
The best case can be when the element you were searching forfound a middle index itself.
Otherwise either you search on the left side or right side andyou, therefore, skip one half entirely and therefore the algorithmdoes less operation and you less operation means moreefficiency.
And hence the time complexity of this algorithm is O(logn).
Step 7: Print the total number of searchesneeded for all five numbers using binary search.
Thanks, and please comment if you have further questions.
if(arrayOfNumbers==number){//If the element is found at theindex i then you can return that index.
WHAT IS THE FULL CODES LOOK LIKE? I CAN'T SEEM TO GET THE ORDERSRIGHT, PLEASE POST THE FULL CODES.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply