Jump to content

Hi everyone,

 

I had to create a new account, because I forgot my password, and LTT forgot password is taking sometime to work. Anyways, I come here to ask some help.

 

I'm doing a simple project for my Intro to Programming class. 

 

All I need fixed on my code is that when the linear search and binary search happen, it gives me the right position of the number on a Array List.

 

I will attach a file with it, if that doesn't work. I will edit and post here.

 

Thank you for all the help.

 

source code:

#include <iostream>#include <cmath>#include <fstream>#include <string>#include <cstdlib>#include <ctime> using namespace std;int value;int positionLinear;int positionBinary;char answer;  //Constant Numbersconst int SIZE = 200;  //Fuction Prototypesint searchList(const int[], int, int);int binarySeach(const int[], int, int);void selectionSort(int [], int);void checkAnswer();void createRandomNumbers();           // Main Fuctionint main(){       createRandomNumbers();       cout << "Please enter a random key from keyboard in between 1 and 200 " << endl;       cin >> value;       int searchList(const int list [], int SIZE, int value); // (random Array, SIZE, value)        int binarySearch(const int list[], int SIZE, int value); // (random Array, SIZE, value)        // Binary Search       if (positionBinary == -1)       {              cout << "The number couldnt be found on the Binary Search" << endl;       }       else       {              cout << "On the Binary Search the number is found at position " << positionBinary << endl;       }        // Linear Search       if (positionLinear == -1)              cout << "Could find the number on the Linear Search" << endl;       else       {              // Position has been found on the Linear Search.              cout << "On the Linear Search the number is found at position " << positionLinear << endl;       }        checkAnswer();                   system("pause");        return 0;}    int searchList(const int list[], int numElems, int value){        int index = 0;       int positionLinear = -1;       bool found = false;        while (index < numElems && !found)       {      if (list[index] == value)              {              found = true;              positionLinear = index;              }       index++;       }       return positionLinear;} int binarySearch(const int list[], int numElems, int value){       int first = 0,              last = numElems - 1,              middle,              positionBinary = -1;       bool found = false;        while (!found && first <= last)       {              middle = (first + last) / 2;              if (list[middle] == value)              {                     found = true;                     positionBinary = middle;              }              else if (list[middle] > value)              {                     last = middle - 1;              }              else                     first = middle + 1;        }       return positionBinary;} void checkAnswer(){       cout << "Would you like to try a new set of numbers? (Y/N)";       cin >> answer;        if (answer == 'Y' || answer == 'y')       {              int main();       }       else if (answer == 'N' || answer == 'n')       {              exit(0);       }       else              cout << "You have entered a invalid choice." << endl;              cout << "Please answer with a Y or N" << endl;              checkAnswer();              } void createRandomNumbers(){       int list[SIZE];       srand((unsigned)time(0));        for (int r = 0; r < SIZE; r++)       {              list[r] = (rand() % 200) + 1;               selectionSort(list, SIZE);              cout << list[r] << endl;       }        } void selectionSort(int list[], int SIZE){       int startScan, minIndex, minValue;        for (startScan = 0; startScan < (SIZE - 1); startScan++)       {              minIndex = startScan;              minValue = list[startScan];              for (int index = startScan + 1; index < SIZE; index++)              {                     if (list[index] < minValue)                     {                           minValue = list[index];                           minIndex = index;                     }               }              list[minIndex] = list[startScan];              list[startScan] = minValue;       }}

Project 7.pdf

Link to comment
https://linustechtips.com/topic/501222-help-on-completing-a-project/
Share on other sites

Link to post
Share on other sites

Hi everyone,

 

I had to create a new account, because I forgot my password, and LTT forgot password is taking sometime to work. Anyways, I come here to ask some help.

 

I'm doing a simple project for my Intro to Programming class. 

 

All I need fixed on my code is that when the linear search and binary search happen, it gives me the right position of the number on a Array List.

 

I will attach a file with it, if that doesn't work. I will edit and post here.

 

Thank you for all the help.

I don't know if a PDF can do stuff like execute stuff so I'm not going to download and open it for security reasons however I'd recommend you post the code in the thread.

Sergeant, United States Marine Corps

Network Administrator, Comptia A+, Security+, Cisco Certified Networking Associate

From a G3258 to dual Xeon E5-2670's

Link to post
Share on other sites

Just post it. I should have done both, just to be save next time.

please post code in the code tags http://linustechtips.com/main/forum-20/announcement-12-please-use-code-tags/

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×