swicth statement crashing the program

+2 ragmar thomas · August 29, 2014
#include <iostream>

using namespace std;
void is(int array[], int arraySize);
int bbs(int array[], int arraySize);
void ss(int array[], int arraySize);
int linearSearch(int list[],int key, int size);
int binarysearch(int list[], int key, int size);


int main(){
    int option, option_1, option_2, i;

    cout << endl << endl;
    cout << "                      sorting   -1" << endl;
    cout << "                      searching -2" << endl;
    cin >> option;

    switch(option){
/*=================================================
sorting part
===================================================*/
case 1:
    cout << endl << endl << endl << endl;
    cout << "                sorting with selection -1" << endl;
    cout << "                sorting with insertion -2" << endl;
    cout << "                sorting with bubble    -3" << endl;
    cin >> option_1;

    switch(option_1){
/*-------------------
selection sort main
---------------------*/
case 1:
    cout << endl;
    cout << "sort with selection" << endl;
    cout << "enter the size of the array " << endl;
    int sizes;
    cin >> sizes;
    int lists[sizes];
    cout << "enter it elements" << endl;
for(i = 0; i < sizes; i++)
    cin >> lists;

cout << "the array you entered is " << endl;
for(i = 0; i < sizes; i++){
    cout << "array [" << i << "] = ";
    cout << lists << endl;
}
ss(lists, sizes);
cout << "the new sorted list is: " << endl;;
for(i = 0; i < sizes; i++)
    cout << lists << " ";
    break;
/*--------------------
insertion sort main
----------------------*/
case 2:
    cout << endl;
    cout << "sort with insertion" << endl;
    cout << "enter the size of the array " << endl;
    int sizei;
    cin >> sizei;
    int listi[sizei];
    cout << "enter it elements" << endl;
for(i = 0; i < sizei; i++)
    cin >> listi;

cout << "the array you entered is " << endl;
for(i = 0; i < sizei; i++){
    cout << "array [" << i << "] = ";
    cout << listi << endl;
}

is(listi, sizei);
cout << "the new sorted list is: " << endl;;
for(i = 0; i < sizei; i++)
    cout << listi << " ";
    break;
/*-----------------
bubble sort main
------------------*/
case 3:
    cout << endl;
    cout << "sort with bubble" << endl;
        cout << "enter the size of the array " << endl;
    int sizeb;
    cin >> sizeb;
    int listb[sizeb];

    cout << "enter it elements" << endl;
for(i = 0; i < sizeb; i++){
    cin >> listb;
}
cout << "the array you entered is " << endl;
for(i = 0; i < sizeb; i++){
    cout << "array [" << i << "] = ";
    cout << listb << endl;
    }
    bbs(listb, sizeb);

    cout << "the sorted array after bubble sort is: ";
for(i = 0; i < sizeb; i++)
        cout << listb << " ";
    break;
}
    default:
/*=================================================
searching part
===================================================*/
case 2:
    cout << endl << endl;
    cout << "searching with linear search -1" << endl;
    cout << "searching with binary search -2" << endl;
    cin >> option_2;

    switch(option_2){
/*----------------
linear search
------------------ */
case 1:
    cout << "linear search" << endl;

    int value_in_linear, i;
    cout<<"enter the size of array\n";
int sizeofLinear;
    cin >> sizeofLinear;
    int array[sizeofLinear];

    cout<<"the array and its elements\n";
for(i = 0; i < sizeofLinear; i++){
    cin >> array;
        }
for(i = 0; i < sizeofLinear; i++){
    cout << "array [" << i << "] =  ";
    cout << array << endl;

}
    cout << "enter the key to search\n";
    cin >> value_in_linear;
int result;
    result = linearSearch(array, value_in_linear, sizeofLinear);
    if(result > 0)
    cout << "the key is found at index " << result << endl;
    else
    cout << "the key is not found " << endl;
    break;
/*-------------
binary search
---------------*/
case 2:
    cout << endl;
    cout << "binary search" << endl;
    cout << "enter the size of the array " << endl;
    int size;
    cin >> size;
    int list;
    cout << "enter it in ascending order" << endl;
for(i = 0; i < size; i++){
    cin >> list;
}
    cout << "the array you entered is " << endl;
for(i = 0; i < size; i++){
    cout << "array [" << i << "] = ";
    cout << list << endl;
}
    cout << "enter the key" << endl;
    int searchValue;
    cin >> searchValue;

result = binarysearch(list, size, searchValue);
if(result < 0)
    cout << "value not found" << endl;
else
    cout << "the value was found at " << result << endl;
    break;
        }
    break;
    }
return 0;
}
//insertion sort
void is(int array[], int arraySize){
int temp, i, j;

for(i = 1; i < arraySize; i++){
    temp = array;
    for(j = i; j > 0 && temp < array[j-1]; j--){
        array[j] = array[j-1];
        array[j-1] = temp;
        }
    }
}
//bubble sort
int bbs(int array[], int arraySize){
int temp, i, j;

for(i = 0; i < arraySize; i++){
    for(j = arraySize - 1; j > i; j--){
        if(array[j] < array[j-1]){
            temp = array[j];
            array[j] = array[j-1];
            array[j-1] = temp;
            }
        }
    }
}
//selection sort
void ss(int array[], int size){

for(int i = 0; i <=size-1; i++){
    int mini = i;
    for(int j = i + 1; j < size; j++){
        if(array[j] < array[mini])
            mini = j;
    }
    int temp = array;
    array = array[mini];
    array[mini] = temp;
    }
}
//linear search
int linearSearch(int list[],int key, int size){
            int index = 0, found = 0;

            do{
                if(key == list[index])
                    found = 1;
                else
                    index ++;
                }while(found == 0 && index < size);

                if(found == 0)
                    index =- 1;

                return index;
}
//binary search
int binarysearch(int list[], int key, int size){
    int index, left, rigth, found, mid;
    left = 0;
    rigth = size - 1;
    found = 0;
    key = size;

    do{
        mid = (left + rigth) / 2;
        if(key == list[mid])
            found = 1;
        else{
            if(key < list[mid])
                rigth = mid - 1;
            else
                left = mid + 1;
        }
    }while(found == 0 && left <= rigth);
    if(found == 0)
        index = -1;
    else
        index = mid;
return index;
}


Post a Reply

Replies

Oldest  Newest  Rating
+1 ragmar thomas · September 1, 2014
sorry i didn't realize it was my draft code, if you don't mind guys u can check it i edited it, thanks in advance.
+1 ragmar thomas · September 1, 2014
nope there are only three warnings, i don' t know the reason, but we might be using different compilers. mine is codeblock. what about yours?
+1 ragmar thomas · September 2, 2014
dude i have checked it in dev, c-free, and codeblocks as well bunch of times i works fine.
and btw codeblocks have this version "codeblocks-13.12mingw-setup-TDM-GCC-481"
  • 1

C++

106,949 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator