?
Solved

Code Check on hw_6.cpp

Posted on 2005-02-24
2
Medium Priority
?
386 Views
Last Modified: 2008-03-17
I have problems compiling this program this is my code can you please help me find the errors


#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>


void hitEnter();
void drawMenu();
void fillRandom();
void display( const int [] );
void bubbleSort( int[], int );
int linearSearch( const int [], int, int );
int binarySearch( const int [], int, int, int, int );

int main ()
{
       
    int arraySize[50] = 0;
    char choice;  
   
    bubbleSort(arraySize, 50);
   
    int entry = 0;
    int Sta;
    Sta = binarySearch(arraySize, 0, 50 - 1, entry);
   
   
    drawMenu();
    cin.get(choice);
   
    do {
       
        switch (choice) {
           
        case '1':   cout << "Array filled with the numbers between 1-30 " << endl;
            cin >> entry;
            fillRandom(arraySize, entry);
            display(arraySize, entry);
            break;
           
        case '2':   cout << "array displayed on screen" << endl;
            display (arraySize, entry);
            break;
           
        case '3':   cout << "Sort the array in descending order" << endl;
            bubbleSort(arraySize, entry);
            display(arraySize, entry);
            break;
           
           
        case '4':   cout << "search for an element in either the sorted or the unsorted array " << endl;
            cin >> entry;
            linearSearch(arraySize, 0,entry-1, entry);
          if(Sta == -1)
            cout << "Not found!!" << endl;
          else
            cout << " Number " << entry << " found array " << Sta << endl;
          
            display(arraySize, entry);
            break;            
           
        case '5':   cout << "search for an element in a sorted array " << endl;
            cin >> Num;
            binarySearch(arraySize, 0, entry-1, entry);
          if(Sta == -1)
            cout << "Not found" << endl;
          else
            cout << "Number " << entry << " found in array " << Sta << endl;
          
            display(arraySize, entry);
            break;
           
        case 'q':   cout <<  "\nExiting program goodbye.\n";
            break;
        default:    break;
           
        }  
        hitEnter();
        drawMenu();
        cin.get(choice);
       
    } while ( choice != 'q' );
   
    return 0;
   
} // end main

void drawMenu() {
   
    cout << "\033[2J";  
    cout << "\t\tHomework 6";
    cout << "\n\n\n[1]fillRandom\n[2]Display\n[3]Sort\n[4]Search\n[5]binarySearch\n[q]Quit\n\nEnter Choice: ";
   
}

void hitEnter(){
    char ch;
    do                        
    cin.get(ch);      
    while  ( ch == ' ');  
   
   
    cout << "\n\nHit Enter to continue....";
    cin.get(ch);        
}

void bubbleSort(arraySize, int counter){
   
    for(int i = 0; i < counter - 1; i++) {
      for(int j = 0; j < counter - 1; j++) {
           
            if(arraySize[j] < arraySize[j+1]) {
               
                int temp      = arraySize[j];
                arraySize[j]   = arraySize[j+1];
                arraySize[j+1] = temp;
 }
 }
 }
   
}

int binarySearch(arraySize, int low, int high, int size) {
   
    int high, low, middle, searchKey;
   
    while (low <= high) {
       
        middle = (low + high) / 2;
       searchKey = arraySize[middle] - size;
       
        if (searchKey == 0)        
      
                 return middle;
        else if (searchKey < 0)
      
            low = middle + 1;
        else
            high = middle - 1;
    }
   
    return -1;  
   
}


void fillRandom(arraySize, int Counter) {
   
    int c;
   
    srand((unsigned) time(NULL));
   
    arraySize[0] = rand() % 30;
    for (c = 0; c < Counter; c++)
        arraySize[c] = rand() % Counter;
    return;
   
}

void display(arraySize, int Counter) {
   
    int c;
   
    for (c = 0; c < Counter; c++) {
       
        cout << setw(5) << arraySize[c];
        if (c % 10 == 9)
            cout << endl;
       
    }
   
    cout << endl;
    return;
   
}

int linearSearch(arraySize[], int low, int high, int size) {
   
    int i=0;
    int b=0;
    int a, high;
   
    while((a <= high) && (b==0)){
       
        if (size == arraySize[i])
           b=1;
        else
            i++;
    }
   
    if(b==1)
        a = i;
    else
        a = -1;
   
    return a;
   
}

0
Comment
Question by:Yesenia1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13393751
To be honest - the above is a mess. The following should compile:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

void hitEnter();
void drawMenu();
void fillRandom(int array[], int Counter);
void display( const int [], int count );
void bubbleSort( int[], int );
int linearSearch(int arraySize[], int low, int high, int size);
int binarySearch( const int [], int, int, int);

int main ()
{
       
   int arraySize[50];
   char choice;  
   
    bubbleSort(arraySize, 50);
   
   int entry = 0;
   int Sta;
   Sta = binarySearch(arraySize, 0, 50 - 1, entry);
   
   
   drawMenu();
   cin.get(choice);
   
   do {
       
       switch (choice) {
           
       case '1':   cout << "Array filled with the numbers between 1-30 " << endl;
            cin >> entry;
           fillRandom(arraySize, entry);
           display(arraySize, entry);
           break;
           
       case '2':   cout << "array displayed on screen" << endl;
            display (arraySize, entry);
           break;
           
       case '3':   cout << "Sort the array in descending order" << endl;
            bubbleSort(arraySize, entry);
           display(arraySize, entry);
           break;
           
           
       case '4':   cout << "search for an element in either the sorted or the unsorted array " << endl;
           cin >> entry;
           linearSearch(arraySize, 0,entry-1, entry);
        if(Sta == -1)
          cout << "Not found!!" << endl;
        else
          cout << " Number " << entry << " found array " << Sta << endl;
       
            display(arraySize, entry);
           break;            
           
       case '5':   cout << "search for an element in a sorted array " << endl;
           binarySearch(arraySize, 0, entry-1, entry);
        if(Sta == -1)
          cout << "Not found" << endl;
        else
          cout << "Number " << entry << " found in array " << Sta << endl;
       
            display(arraySize, entry);
           break;
           
       case 'q':   cout <<  "\nExiting program goodbye.\n";
           break;
       default:    break;
           
        }  
        hitEnter();
       drawMenu();
       cin.get(choice);
       
   } while ( choice != 'q' );
   
   return 0;
   
} // end main

void drawMenu() {
   
   cout << "\033[2J";  
    cout << "\t\tHomework 6";
   cout << "\n\n\n[1]fillRandom\n[2]Display\n[3]Sort\n[4]Search\n[5]binarySearch\n[q]Quit\n\nEnter Choice: ";
   
}

void hitEnter(){
   char ch;
   do                        
   cin.get(ch);      
   while  ( ch == ' ');  
   
   
   cout << "\n\nHit Enter to continue....";
   cin.get(ch);        
}

void bubbleSort(int arraySize[], int counter){
   
   for(int i = 0; i < counter - 1; i++) {
     for(int j = 0; j < counter - 1; j++) {
           
           if(arraySize[j] < arraySize[j+1]) {
               
               int temp      = arraySize[j];
               arraySize[j]   = arraySize[j+1];
               arraySize[j+1] = temp;
}
}
}
   
}

int binarySearch(const int arraySize[], int low, int high, int size) {
   
   int middle, searchKey;
   
   while (low <= high) {
       
       middle = (low + high) / 2;
      searchKey = arraySize[middle] - size;
       
       if (searchKey == 0)        
     
               return middle;
       else if (searchKey < 0)
   
            low = middle + 1;
       else
           high = middle - 1;
   }
   
   return -1;  
   
}


void fillRandom(int arraySize[], int Counter) {
   
   int c;
   
   srand((unsigned) time(NULL));
   
    arraySize[0] = rand() % 30;
    for (c = 0; c < Counter; c++)
       arraySize[c] = rand() % Counter;
   return;
   
}

void display(const int arraySize[], int Counter) {
   
   int c;
   
   for (c = 0; c < Counter; c++) {
       
       cout << setw(5) << arraySize[c];
       if (c % 10 == 9)
           cout << endl;
       
   }
   
   cout << endl;
   return;
   
}

int linearSearch(int arraySize[], int low, int high, int size) {
   
   int i=0;
   int b=0;
    int a;
   
   while((a <= high) && (b==0)){
       
       if (size == arraySize[i])
          b=1;
       else
            i++;
   }
   
   if(b==1)
       a = i;
   else
       a = -1;
   
   return a;
   
}
0
 
LVL 4

Expert Comment

by:markdoc
ID: 13401677
hey, at first glance, one source of your errors would be a missing line after the #include lines:

using namespace std;
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question