Hey, I need to do a program that displays a menu with 5 options that works with search and sorting of arrays of unordered decimal numbers:

1)Read file
2)Linear Search
3)Binary Search
4)Bubble Sort
5)Selection Sort

each option has to display the result.

with
the option 1 has to ask the user the name of the file and display the values, I already did that, but I'm having trouble using the rest of the functions with the same file because apparently is not receiving any information from the file. When i use them the result is blank. Please I need help! thanks in advance

Recommended Answers

All 5 Replies

Hmm...it would be nice if it is possible to have a look at the code you have written. :]

Hmm...it would be nice if it is possible to have a look at the code you have written. :]

well I have this.. :$

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

using namespace std;
//prototipos
int ReadFile(string);
int BinarySearch(int [], int, int);
int LinearSearch(int[], int, int);
int BubbleSort(int [],int);
void SelectionSort(int [], int);
int DisplayMenu();
int GetInt(string);
bool VerifyFile(string);

int main(){ 
    int size = 0;
    int option;
    int key; 
    int array[100];
    int intReturn; 
    bool order = false;
    string line, FileName;
    int posicion;
    
  
while (option >= 1 || option <= 6){


cout<<"This program bla bla..."<<endl;


   option = DisplayMenu();
    
   
      if(option == 1){
  ReadFile(FileName); 
  }
  else if(option == 2){
   cout<<"Type the value you are looking for"<<endl;
   cin>>key;
   cout<<"This value is located in the position:"<<posicion<<endl;
   LinearSearch(array,size,key);
  }
  else if(option == 3){
    cout<<"Type the value you are looking for"<<endl;
    cin>>key;
    BinarySearch(array,size, key);
  }
  else if(option == 4){
   BubbleSort(array,size); 
    
  }
  else if(option == 5){
  SelectionSort(array,size);
  
    
  }
  else if(option == 6){
       return 0;
  }


}



return 0;    
}


//functions

int DisplayMenu(){
     
   int option;    
   cout <<"\tMenu" <<endl;
   cout <<"\t 1) Read File" << endl;
   cout <<"\t 2) Linear Search" <<endl;
   cout <<"\t 3) Binary Search" <<endl;
   cout <<"\t 4) Bubble Sort" <<endl;
   cout <<"\t 5) Seleccion Sort" <<endl;
   cout <<"\t 6) Exit" <<endl;


do{

   cout << "Choose an option from the menu " << endl;
   cin >> option;
   
 while ((option < 1) || (option > 6)){
     cout << "ERROR: Options must be from 1 to 6: " << endl;
     cin >> option;
                 }
}
 while ((option < 1) || (option > 6));{
     return option;
}

}


int BinarySearch(int array[], int size, int key){
    bool found = 0;
    int posicion = -1;
    int middle;
    int first = 0;
    int last = size -1;
    bool order;
    
    do{ 
        while(!found && first <=last){
                 middle = (first + last)/2;
                 if (array[middle]==key){
                     found = 1;
                     posicion = middle;
}
                 else if (key < array[middle])
                     last = middle - 1;
                 else
                     first = middle + 1;
                     
                     
}
                   return posicion;
}  while(order);

if (order == false){
          cout<<"You have to sort the array before using this option"<<endl;


}
}

int LinearSearch(int array[], int size, int key) {
    int posicion = -1;
    for(int i = 0; i<size; i++){
          if(array[i] == key)
             posicion = i; 
               break;
}
    return posicion;
}


int BubbleSort(int array[],int size){
        
          
     bool swap; 
     int temp;
     bool order = false; 
     
     do
     {
         swap = false;
         for(int cont = 0; cont < (size - 1); cont++) 
         {
         
          if (array[cont] > array[cont + 1])
          {
               temp = array[cont];
               array[cont] = array[cont + 1];
               array[cont + 1] = temp;
               swap = true;
              }
               }
               }
     while (swap); 
               cout<<"This is the array in ascending order"<<endl;
               for(int cont = 0; cont < size ; cont++) 
               cout << "[ " << array[ cont ] << " ]";

      cout << endl;
      order = true;        
               }
      
      
      
                  
void SelectionSort(int array[], int size){
       int scan, minI, minV;
       bool order = false;
       
       for (scan = 0; scan < (size - 1); scan++)
       {
           minI = scan;
           minV = array[scan];
           for(int j = scan + 1; j < size; j++)
           {
              if (array[j] < minV)
              {
                minV = array[j];
                minI = j;
}
}
                array[minI] = array[scan];
                array[scan] = minV;
}
cout<<"This is the array in ascending order"<<endl;
               for(int cont = 0; cont < size ; cont++) 
               cout << "[ " << array[ cont ] << " ]";

      cout << endl;
      order = false;
} 


//to convert string to int

int GetInt(string line){
    int intReturn;
    
    intReturn = atoi(line.c_str());
    
    return(intReturn);
}
  
  
//verify file function
bool VerifyFile(string FileName)
{

    fstream infile;
    
         
    infile.open(FileName.c_str(),fstream::in); 
    
    
    if (infile.good())
{
        infile.close();
        return true ;
}
    return false;
}   

int ReadFile(string FileName)
{
    string line;
    fstream infile;  
    int array[100];
    int size = 0; 
    int intReturn;
   // string FileName;  
    
    cout<<"Insert the name of the file:"<<endl;
    cin >>FileName;
    
    if (VerifyFile(FileName)==true)
{
            
        infile.open(FileName.c_str(),fstream::in);
    
        cout<<"This is the content of the file:"<<endl;
        
      while(!infile.eof()){
         getline(infile,line);
         cout<< line << endl;
         GetInt(line);
         array[size] = intReturn;
         size++;
        }  
infile.close();
return 0;
}     
         

    else
{
         cout<<"The file does not exists."<<endl;
         return 1;
}
}

From the code it looks like you are reading from file only once. And when you get the data in an array , you are trying to use the same array for manipulation.. am i right?

From the code it looks like you are reading from file only once. And when you get the data in an array , you are trying to use the same array for manipulation.. am i right?

Yes, but do I have to open the file in each function?? I'm really lost at that part

Yes you have to, i think thats the problem, suppose your line search function takes in values but what is it searching? to me it looks like it is searching nothing, if you have to search through a file you open a file at the start of the function and close it at the end of the function :]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.