#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
using namespace std;

void array(int arr[], int arrSize);
void add(int arr[], int arrSize, int newElem);
bool edit(int arr[], int arrSize, int index, int newVal);
bool delEm(int arr[], int arrSize, int elem); 
bool search(int arr[], int arrSize, int elem);
bool PunoNa(int arr[], int arrSize);
void display(int arr[], int arrSize);
void dispPo(int arr[], int arrSize, int index);
void shuffle(int arr[], int arrSize);
void loc(int arr[], int arrSize,int elem);

int main(){

    int choice = 0;
    int arrSize;

    cout<<"Enter size of array: ";
    cin>>arrSize;

    int arr[arrSize];
    if(arrSize<=20){
            for(int x=0; x<arrSize; x++){
                cout<<"Enter elem"<<x+1<<": ";
                cin>>arr[x];
        }
    }
    if(arrSize>20){

            cout<<"Error! Array size must be 1-20 only."<<endl;
            cout<<"Enter size of array: ";
            cin>>arrSize;
        if(arrSize<21){ 
        for(int x=0; x<arrSize; x++){
            cout<<"Enter elem"<<x+1<<": ";
            cin>>arr[x];
        }
    }else{

        cout<<"Error!"<<endl;
        system ("pause");
        return 0;
    }
}

    do{    

        cout<<"\n";
        cout<<"Main Menu"<<endl;
        cout<<"[1]Search an Element"<<endl;
        cout<<"[2]Display Elements"<<endl;
        cout<<"[3]Add an Element"<<endl;
        cout<<"[4]Delete an Element"<<endl;
        cout<<"[5]Edit an Element"<<endl;
        cout<<"[6]Shuffle Elements"<<endl;
        cout<<"[7]Save to File"<<endl;
        cout<<"[8]Exit"<<endl;
        cout<<"\n";
        cout<<"Enter choice: ";
        cin>>choice;

        if(choice == 1){
            int elem = 0;
            int position;

            cout<<"Enter element to search: ";
            cin>>elem;

            bool result = search(arr,arrSize,elem);
            if(result){

                cout<<"Element "<<elem<<" is in position no ";
                loc(arr,arrSize,elem);

            }else{

                cout<<"Error! Element "<<elem<<" not Found."<<endl;
            }

        }else if(choice == 2){
            display(arr,arrSize);
            cout<<"\n";
            for(int x=0; x<arrSize; x++){
            cout<<"Position no."<<x+1<<": "<<arr[x]<<""<<endl;
        }

        }else if(choice == 3){
            int newElem=0;

            bool result = PunoNa(arr,arrSize);
            if(result){
                cout<<"Cannot add value. Array is Full!"<<endl;
            }else{
                cout<<"Enter value to add: ";
                cin>>newElem;
                add(arr,arrSize,newElem);
                cout<<"New Element successfully added! :)";
                cout<<"\n";
            }

        }else if(choice == 4){
            int elem = 0;
            cout<<"Enter element to delete: ";
            cin>>elem;

            bool result = delEm(arr,arrSize,elem);
            if(result){
                cout<<"Successfully deleted element "<<elem<<". "<<endl;
            }else{
                cout<<"Element "<<elem<<" Not found in the array! "<<endl;
            }

        }else if(choice == 5){
            int index = -1;
            int newElem;
            int elem;

            cout<<"Enter position number to edit: ";
            cin>>index;
            cout<<"Enter element to be edited: ";
            cin>>elem;
            cout<<"\n";
            cout<<"Element "<<elem<<" found in position number"<<index<<"."<<endl;
            cout<<"\n";
            cout<<"Enter new element in position "<<index<<": ";
            cin>>newElem;
            bool result = edit(arr,arrSize,index,newElem);
            if(result){
                cout<<"Successfully edited position number "<<index<<"."<<endl;
            }else{
                cout<<"Unable to edit. Please check the position number entered."<<endl;
            }

        }else if(choice == 6){
            shuffle(arr,arrSize);

        }else if(choice == 7){

                    string filename;
                    ofstream file;
                    cin.ignore();
                    cout<<"Enter Filename: ";
                    getline (cin, filename);
                    filename = filename+" .txt";
                    file.open(filename.c_str(),ios::app);
                    for (int x=0;x<arrSize;x++){
                        file<<"Number in Index "<<x+1<<" is: "<<arr[x]<<endl;
                    }
                    file.close();
                    cout<<"File SAVED."<<endl;
                    }

    }while(choice != 8);

    return 0;
}
/*function array is PunoNa */
bool PunoNa(int arr[], int arrSize){

    bool punoNa = true;
    for(int x=0;x<arrSize;x++){
        if(arr[x] == 0){
            punoNa = false;
            break;
        }
    }
    return punoNa;
}

/* function add*/
void add(int arr[], int arrSize, int newElem){
    int position=0;
    int x;

    bool punoNa = true;
    for(int x=0;x<=arrSize;x++){
        if(arr[x] == 0){
            arr[x] = newElem;
            punoNa = false;
            break;
        }
    }
}

/* function edit*/
bool edit(int arr[], int arrSize, int index, int newElem){
    if(index<0||index>arrSize-1){
        return false;
    }else{
        arr[index-1]=newElem;
        return true;
    }

};

/* function del*/
bool delEm(int arr[], int arrSize, int elem){
    bool naayNaDel = false;
    for(int x=0;x<arrSize;x++){
        if(arr[x] == elem){
            arr[x] = 0;
            naayNaDel = true;
        }
    }
    return naayNaDel;
}

/* function search*/
bool search(int arr[], int arrSize, int elem){

    bool present = false;
    for(int x=0;x<arrSize;x++){
        if (arr[x]== elem){
            present = true;
        }
    }
    return present;

}
/* function display*/
void display(int arr[], int arrSize){

    for(int x=0;x<arrSize;x++){
        if(arr[x] == 0){
            cout<<" _,";
        }else{
            cout<<arr[x]<<", ";
        }

    }
}
/*function for shuffle*/
void shuffle(int arr[], int arrSize){
    srand ((time(0)));
    //int fakePos = 0;;

    for (int x=0;x<arrSize;x++){
        int arrPos = rand() % arrSize;
        int fakePos = arr[x];
        arr[x] = arr[arrPos];
        arr[arrPos] = fakePos;

    }
    for (int y = 0; y<arrSize; y++){
        cout<<arr[y]<<", ";  //for printing of shuffled elements
    }
}

void loc(int arr[], int arrSize,int elem){

    for(int x=0;x<arrSize;x++){
        if(arr[x] == elem){
            cout<<x+1<<",";
        }
    }

}

Recommended Answers

All 2 Replies

Based on the little information you provided, your code does exactly what it says it does. Of course, that's likely not what you wanted it to do. Perhaps if you provided a little more information like:

  1. What is this code supposed to do
  2. What is it actually doing
  3. What error messages (if any) are you getting
  4. Does it compile

In case anyone (other than the OP) is inclined to respond "it's obvious what the code is supposed to do", that may be the case but the OP has not gone to much effort in seeking help. I certainly would not take my car to a mechanic and say "fix this" without describing the problem.

commented: The old "the computer did what you told it to do" problem. +15
commented: Sorry for the little infos. The purpose of this code is for searching, editing, adding, deleting, displaying, saving and shuffling the variables in an +0

Sorry for the little infos. The purpose of this code is for searching, editing, adding, deleting, displaying, saving and shuffling the variables in an array. It promps the user to input its desired array size then input the elements/values they want and then stored it an array.

I doesn't have syntax errors.
It runs, but suddenly it tell exe has stopped working. I also tried this code in other laptops but does the same, runs but suddenly the console stopped working. Then I tried it on my phone. It works it definitely works but inlaptops it doesn't. theres one laptop that this code works its on my friends'. What could be the possible problem? it is a human error? in my code? in the laptops?

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.