#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<<",";
}
}
}
Ana Mae
0
Newbie Poster
Recommended Answers
Jump to PostBased 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:
- What is this code supposed to do
- What is it actually doing
- What …
All 2 Replies
Reverend Jim
4,780
Hi, I'm Jim, one of DaniWeb's moderators.
Moderator
Featured Poster
rproffitt
commented:
The old "the computer did what you told it to do" problem.
+15
Ana Mae
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
Ana Mae
0
Newbie Poster
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.