Write a C++ program in microsoft visual studio 2010 that enables a user to sort
ID: 3654893 • Letter: W
Question
Write a C++ program in microsoft visual studio 2010 that enables a user to sort an array of 10 integers using either a selection sort or bubble sort. Also to search for a particular item in the 10 integer array using either a linear search or binary search. I've written the code as follows: // Abdullah_sorting.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int opselect; cout << "Select a Menu Option Below: "; cout << " Sort Elements - Press 1 "; cout << " Search Elements - Press 2 "; cin >> opselect; switch (opselect) { case 1: char s, b, selection; cout << " Select sort method: "; cout << " Selection Sort - Enter the letter s "; cout << " Bubble Sort - Enter the letter b "; cin >> selection; if (selection == s) { int selectionSort(int [], int); const int NUMEL = 10; int nums[NUMEL] = {5, 98, 10, 67,32,45,73,22,99,101}; int i, moves; moves = selectionSort(nums, NUMEL); cout << " The sorted list, in ascending order, is: "; for (i = 0; i < NUMEL; i++) cout << " " << nums[i]; cout << ' ' << moves << " moves were made to sort this list "; return 0; } else if (selection != s) { cout << " Invalid Selection!"; } int selectionSort(int num[], int numel) { int i, j, min, minidx, temp, moves = 0; for (i = 0; i < (numel - 1); i++) { min = num[i]; minidx = i; for (j = i; j < numel; j++) { if (num[j] < min) { min = num[j]; minidx = j; } } if (min < num[i]) { temp = num[i]; num[i] = min; num[minidx] = temp; moves++; } } return moves; if (selection == b) { int bubbleSort(int [], int) { const int NUMEL = 10; int nums[NUMEL] = {5, 98, 10, 67, 32, 45, 73, 22, 99, 101}; int i, moves; moves = bubbleSort(nums, NUMEL); cout << "The sorted list, in ascending order, is: "; for (i=0; i < NUMEL; i++) cout << " " << nums[i]; cout << ' ' << moves << " moves were made to sort this list "; return 0; } else if (selection != b) { cout << " Invalid Selection!"; } int bubbleSort(int num[], int numel) { int i, j, temp, moves = 0; for (i = 0; i < (numel - 1); i++) { for(j = 1; j < numel; j++) { if (num[j] < num [j-1]) { temp = num[j]; num[j] = num [j-1]; num [j-1] = temp; moves++; } } } return moves; } } case 2: int item, location; char l, b, selection; cout << " Enter the item you are searching for: "; cin >> item; cout << " Select a search method: "; cout << " Linear Search - Enter the letter l "; cout << " Binary Search - Enter the letter b "; cout << " Enter selection: "; cin >> selection; if (selection == l) { int linearSearch(int[], int, int); int main() { const int NUMEL = 10; int nums[NUMEL]= {5, 10, 22, 32, 45, 67, 73, 98, 99, 101}; location = linearSearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location "<< location << endl; else cout << "The item was not found in the list "; return 0; } else if (selection != l){ cout << " Invalid Selection! "; } int linearSearch(int list[], int size, int key) { int i; for (i=0; i < size; i++) { if(list[i] == key) return i; } return -1; } } if(selection == b) { int binarySearch(int[], int, int); int main () { const int NUMEL = 10; int nums[NUMEL] = {5, 10, 22, 32, 45, 67, 73, 98, 99, 101}; location = binarySearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location " << location << endl; else cout << "The item was not found in the list "; return 0; } else if (selection != b) { cout << " Invalid Selection! "; } int binarySearch(int list[], int size, int key) { int left, right, midpt; left = 0; right = size -1; while (left <= right) { midpt = (int) ((left + right) / 2); if (key == list[midpt]) { return midpt; } else if (key == list[midpt]) left = midpt +1; else right = midpt -1; } return -1; } default: if (opselect >= 3) cout << " Invalid Selection! "; } cin.ignore(); system("pause"); return 0; } And I keep getting this error 1075 code about my braces, but I've looked over it countless of times and can't find it! Then it's saying my local definitions are illegal, but I'm not sure how to correct these errors! I've only had a few weeks total time of class so I'm still learning, therefore any help is appreciated! I'll rate and thanks!!Explanation / Answer
i'd removed all the error from your code......you may also download the code from http://www.2shared.com/file/JQoaOZP5/Abdullah_sorting.html // #include "stdafx.h" #include#include#include#include int main(int argc, char *argv[]) { int opselect; int item,location; coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.