#include
#include
using namespace std;
//global variable declaration
int a[10];
int active, size;
void menu() {//prints menu
cout << \"menu\" << endl;
cout << \"1.Insert \\\"Insert\\\" key\" << endl; //code 45
cout << \"2.Delete \\\"Delete\\\" key\" << endl; //code 46
cout << \"3.Sort \\\"F2\\\" key\" << endl; //code 113
cout << \"4.Select \\\"Down Arrow\\\" key\" << endl; //code 40
cout << \"5.Move Right \\\"Right Arrow\\\" key\" << endl; //code 39
cout << \"6.Move Left \\\"Left Arrow\\\" key\" << endl; //code 37
cout << \"7.Exit \\\"F1\\\" key\" << endl; //code 112
cout << \"press the required key or number to continue\" << endl;
}
//reads the data from file
void readdata() {
ifstream f;
f.open(\"F://MyFile.txt\");
if (f.is_open()) {
f>>size;
f>>active;
if (size > 10) {
cout << \"Numbers exceeds array limit\";
return;
} else {
for (int i = 0; i < size; i++) {
f >> a[i];
}
}
f.close();
}
}
//sorts the data
void sort() {
//sorting using bubble sort
int pass = 0, so = 0, t, i;
while ((pass <= size - 1) && (so == 0)) {
so = 1;
for (i = 0; i < size - 1 - pass; i++) {
if (a[i] > a[i + 1]) {
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
so = 0;
}
}
pass = pass + 1;
}
}
//prints the data
void print() {
cout<<\"\ \";
for (int i = 0; i < size; i++) {
cout << a[i] << \"\\t\";
}
cout << \"Active=\" << active << endl;
}
//writes the data into the file
void writedata() {
ofstream f;
f.open(\"F://MyFile.txt\");
f << size << endl;
f << active << endl;
for (int i = 0; i < size; i++) {
f << a[i] << \" \";
}
f.close();
}
//inserts an element in array and makes it active
void insert() {
if (size == 10)//checks if the list is full
cout << \"list is full\";
else {
int x, i;
cout << \"Enter a number to insert: \";//takes user input
cin>>x;
for (i = 0; i < size; i++) {
if (a[i] == active)//finds active index
break;
}
size++;//increment the size
int y = size;
while (y != i) {//shifting
a[y] = a[y - 1];
y--;
}
a[i] = x;//insert
active = x;//sets active
}
}
//deletes the active value and sets the next one as active
void del() {
int i;
for (i = 0; i < size; i++) {//finds active index
if (a[i] == active)
break;
}
active = a[i + 1]; //sets the next one as active
while (i < size - 1) {//shifting
a[i] = a[i + 1];
i++;
}
size--;//decrements the size
}
//changes the active value
void select() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)//finds active index
break;
}
if (i == size - 1)
active = a[0]; //selects the first one as active
else
active = a[i + 1]; //sets the next one as active
}
//moves the active value to its left
void moveleft() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)
break;
}
if (i == 0)
cout << \"moving left is not possible\"; //selects the first one as active
else
{ int temp = a[i - 1];
a[i-1]=a[i];
a[i]=temp;
//moves the active value to the left of the previous element by swapping
}
}
//moves the active value to its right
void moveright() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)
break;
}
if (i == size - 1)
cout << \"m.
1. #include
#include
using namespace std;
//global variable declaration
int a[10];
int active, size;
void menu() {//prints menu
cout << "menu" << endl;
cout << "1.Insert "Insert" key" << endl; //code 45
cout << "2.Delete "Delete" key" << endl; //code 46
cout << "3.Sort "F2" key" << endl; //code 113
cout << "4.Select "Down Arrow" key" << endl; //code 40
cout << "5.Move Right "Right Arrow" key" << endl; //code 39
cout << "6.Move Left "Left Arrow" key" << endl; //code 37
cout << "7.Exit "F1" key" << endl; //code 112
cout << "press the required key or number to continue" << endl;
}
//reads the data from file
void readdata() {
ifstream f;
f.open("F://MyFile.txt");
if (f.is_open()) {
f>>size;
f>>active;
if (size > 10) {
cout << "Numbers exceeds array limit";
return;
} else {
for (int i = 0; i < size; i++) {
f >> a[i];
}
}
f.close();
}
}
2. //sorts the data
void sort() {
//sorting using bubble sort
int pass = 0, so = 0, t, i;
while ((pass <= size - 1) && (so == 0)) {
so = 1;
for (i = 0; i < size - 1 - pass; i++) {
if (a[i] > a[i + 1]) {
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
so = 0;
}
}
pass = pass + 1;
}
}
//prints the data
void print() {
cout<<" ";
for (int i = 0; i < size; i++) {
cout << a[i] << "t";
}
cout << "Active=" << active << endl;
}
//writes the data into the file
void writedata() {
ofstream f;
f.open("F://MyFile.txt");
f << size << endl;
f << active << endl;
for (int i = 0; i < size; i++) {
f << a[i] << " ";
}
f.close();
}
3. //inserts an element in array and makes it active
void insert() {
if (size == 10)//checks if the list is full
cout << "list is full";
else {
int x, i;
cout << "Enter a number to insert: ";//takes user input
cin>>x;
for (i = 0; i < size; i++) {
if (a[i] == active)//finds active index
break;
}
size++;//increment the size
int y = size;
while (y != i) {//shifting
a[y] = a[y - 1];
y--;
}
a[i] = x;//insert
active = x;//sets active
}
}
//deletes the active value and sets the next one as active
void del() {
int i;
for (i = 0; i < size; i++) {//finds active index
if (a[i] == active)
break;
}
active = a[i + 1]; //sets the next one as active
while (i < size - 1) {//shifting
a[i] = a[i + 1];
i++;
}
size--;//decrements the size
}
4. //changes the active value
void select() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)//finds active index
break;
}
if (i == size - 1)
active = a[0]; //selects the first one as active
else
active = a[i + 1]; //sets the next one as active
}
//moves the active value to its left
void moveleft() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)
break;
}
if (i == 0)
cout << "moving left is not possible"; //selects the first one as active
else
{ int temp = a[i - 1];
a[i-1]=a[i];
a[i]=temp;
//moves the active value to the left of the previous element by swapping
}
}
//moves the active value to its right
void moveright() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)
break;
}
if (i == size - 1)
5. cout << "moving right is not possible"; //selects the first one as active
else
{ int temp = a[i + 1];
a[i+1]=a[i];
a[i]=temp;
//moves the active value to the right of the next element by swapping
}
}
int main() {
int ch;
readdata();
print();
menu();
while (1) {
// switch (getchar()) {//keyboard key input
// case 45:
// insert();
// print();
// break;
// case 46:
// del();
// print();
// break;
// case 113:
// sort();
// print();
// break;
// case 40:
// down();
// print();
// break;
// case 39:
// right();
// print();
// break;
// case 37:
6. // left();
// print();
// break;
// case 112:
// print();
// writedata();
// return 0;
// break;
// default:
// cout << "Invalid key" << endl;
// }
cin>>ch;
switch (ch) {//numeric input
case 1:
insert();
print();
break;
case 2:
del();
print();
break;
case 3:
sort();
print();
break;
case 4:
select();
print();
break;
case 5:
moveright();
print();
break;
case 6:
moveleft();
print();
7. break;
case 7:
print();
writedata();
return 0;
break;
default:
cout << "Invalid key" << endl;
}
menu();
}
return 0;
}
Solution
#include
#include
using namespace std;
//global variable declaration
int a[10];
int active, size;
void menu() {//prints menu
cout << "menu" << endl;
cout << "1.Insert "Insert" key" << endl; //code 45
cout << "2.Delete "Delete" key" << endl; //code 46
cout << "3.Sort "F2" key" << endl; //code 113
cout << "4.Select "Down Arrow" key" << endl; //code 40
cout << "5.Move Right "Right Arrow" key" << endl; //code 39
cout << "6.Move Left "Left Arrow" key" << endl; //code 37
cout << "7.Exit "F1" key" << endl; //code 112
cout << "press the required key or number to continue" << endl;
}
//reads the data from file
void readdata() {
ifstream f;
8. f.open("F://MyFile.txt");
if (f.is_open()) {
f>>size;
f>>active;
if (size > 10) {
cout << "Numbers exceeds array limit";
return;
} else {
for (int i = 0; i < size; i++) {
f >> a[i];
}
}
f.close();
}
}
//sorts the data
void sort() {
//sorting using bubble sort
int pass = 0, so = 0, t, i;
while ((pass <= size - 1) && (so == 0)) {
so = 1;
for (i = 0; i < size - 1 - pass; i++) {
if (a[i] > a[i + 1]) {
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
so = 0;
}
}
pass = pass + 1;
}
}
//prints the data
void print() {
cout<<" ";
for (int i = 0; i < size; i++) {
9. cout << a[i] << "t";
}
cout << "Active=" << active << endl;
}
//writes the data into the file
void writedata() {
ofstream f;
f.open("F://MyFile.txt");
f << size << endl;
f << active << endl;
for (int i = 0; i < size; i++) {
f << a[i] << " ";
}
f.close();
}
//inserts an element in array and makes it active
void insert() {
if (size == 10)//checks if the list is full
cout << "list is full";
else {
int x, i;
cout << "Enter a number to insert: ";//takes user input
cin>>x;
for (i = 0; i < size; i++) {
if (a[i] == active)//finds active index
break;
}
size++;//increment the size
int y = size;
while (y != i) {//shifting
a[y] = a[y - 1];
y--;
}
a[i] = x;//insert
active = x;//sets active
}
10. }
//deletes the active value and sets the next one as active
void del() {
int i;
for (i = 0; i < size; i++) {//finds active index
if (a[i] == active)
break;
}
active = a[i + 1]; //sets the next one as active
while (i < size - 1) {//shifting
a[i] = a[i + 1];
i++;
}
size--;//decrements the size
}
//changes the active value
void select() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)//finds active index
break;
}
if (i == size - 1)
active = a[0]; //selects the first one as active
else
active = a[i + 1]; //sets the next one as active
}
//moves the active value to its left
void moveleft() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)
break;
}
if (i == 0)
cout << "moving left is not possible"; //selects the first one as active
11. else
{ int temp = a[i - 1];
a[i-1]=a[i];
a[i]=temp;
//moves the active value to the left of the previous element by swapping
}
}
//moves the active value to its right
void moveright() {
int i;
for (i = 0; i < size; i++) {
if (a[i] == active)
break;
}
if (i == size - 1)
cout << "moving right is not possible"; //selects the first one as active
else
{ int temp = a[i + 1];
a[i+1]=a[i];
a[i]=temp;
//moves the active value to the right of the next element by swapping
}
}
int main() {
int ch;
readdata();
print();
menu();
while (1) {
// switch (getchar()) {//keyboard key input
// case 45:
// insert();
// print();
// break;
// case 46:
// del();