Thursday, October 9, 2014

Find a Number in C++

In this article I would like to share with you a simple program that I wrote using C++ as my programming language to perform linear search or sequential search in computer science data structure. I called this program find a number. What this program will do first is to ask the user to enter five integer numbers after that our program will ask the user what number to be search in a given list. If the number can be found in the list our program will tell the user which location the value is located. If the number is not in the list our program will tell the user the value to be search cannot be found in the list.

I hope you will find my program useful in you programming assignments and projects using C++ as your primary programming language. If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com

Thank you very much.


Sample Output of Our Program

Program Listing

#include <iostream>
#include <stdlib.h>

using namespace std;

main() {

    int list=0,numbers[5];
    int search=0,dummy=0,location=0;
    int *mypointer;

     cout << "\n\t\t\tFIND A NUMBER 1.0";
     cout << "\n\n";

      mypointer = numbers;

    for (list=0; list < 5; list++) {
        cout << "Enter Value No. " << list+1 <<  " :" ;
        cin >> list[mypointer];
    }

   cout << "\n\n";
   cout << "Enter Number To Search : ";
   cin >> search;

   dummy = search;
     for (list=0; list < 5; list++) {
         if (search == list[mypointer]) {
            search = 1; // true value if found in the list
            location = list+1; // Determine the exact location of the number
     }
}
  if (search == 1){
       cout << "\n\n";
      cout <<"The number " << dummy <<
            " is found in the list";
      cout << "\n\n";
      cout << "The Exact Location in list is " <<
            location <<".";
  }
  else {
       cout << "\n\n";
      cout <<"The number " << dummy<<
            " Cannot be found in the list";
  }
  cout << "\n\n";
 system("PAUSE");
}


No comments:

Post a Comment