Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Code Check on hw_6.cpp

Posted on 2005-02-24
2
Medium Priority
?
389 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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

581 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