Answer to Question #21128 in C++ for werty

Question #21128
1. Write a program that will store 10 integer numbers into an array. Write a function that will search for a value from the array. The function will return the index if the value is in the llsit or return -1 if the value is not in the list. In the main() , show the location/index where the value is stored in the array else display a message “Value is not in the list”. Your function definition can be as the following :-
int Search( int list[], int target)
{
// list is the array of 10 integer numbers
//target is the value to search from the array
}
Sample Output :
Enter value to search : 28
28 is found in index 2.
Or
Enter value to search: 5
5 is not in the list.
2. Write another function that will sort the elements in list[] in ascending order. Show the content of the array after sorting. [ Note : Use selection sort ]
1
Expert's answer
2012-12-25T10:08:26-0500
#include <iostream>
#include <utility>

using namespace std;

int Search( int list[], int target)
{
for (int i = 0; i<10; i++) {
& if (list[i] == target) {
return i;
& }
}
return (-1);
}

void quickSort(int list[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = list[(left + right) / 2];

while (i <= j) {
& while (list[i] < pivot)
i++;
& while (list[j] > pivot)
j--;
& if (i <= j) {
tmp = list[i];
list[i] = list[j];
list[j] = tmp;
i++;
j--;
& }
};

if (left < j)
& quickSort(list, left, j);
if (i < right)
& quickSort(list, i, right);

}

int main(int argc, const char * argv[])
{
int list[10], x=0;

cin >> x;

for (int i = 0; i<10; ++i)
{
& cin >> list[i];
}

if (Search(list, x)+1 > 0 )
{
& cout << Search(list, 2)+1 << endl;
}
else
{
& cout << x <<" is not in the list "<<endl;
};

quickSort(list, 0, 10);

for (int i = 0; i<10; ++i)
{
& cout << list[i]<<"& ";
}


return 0;
}























Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS