-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
134 lines (119 loc) · 3.18 KB
/
functions.cpp
File metadata and controls
134 lines (119 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "functions.h"
bool isDigit(string str1) {
return str1.find_first_not_of("0123456789") == string::npos;
}
int getArraySize(unsigned u, size_t t){
return u/t;
}
void UserMenu(){
cout << "==========Array Manipulator==========" << endl;
cout << "1. Check for Integer" << endl <<
"2. Modify via Index" << endl <<
"3. Append" << endl <<
"4. Remove" << endl <<
"Q. Quit" << endl <<
"Enter a selection 1-4: ";
}
void ReadInputFile(int arr[]){
ifstream inFile;
inFile.open("A1input.txt");
if (!inFile.is_open()) {
cout << "Unable to open file";
return;
}
string line;
int tempindex = 0;
//Read line by line
while (getline(inFile, line)) {
stringstream iss(line);
string value;
while (iss >> value) {
if (isDigit(value)) {
arr[tempindex++] = stoi(value);
}
}
}
inFile.close();
}
bool IntegerExists(int arr[], int size){
int value;
cout << "Enter a number to check: ";
cin >> value;
if (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
}
for(int i = 0; i < size; i++){
if (arr[i] == value)
return true;
}
return false;
}
void ModifyFromIndex(int arr[], int size){
int index;
int value;
cout << "Enter the index to modify: ";
cin >> index;
if (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
}
cout << "Enter the new value: ";
cin >> value;
if (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
}
try {
if (index < 0) {
throw out_of_range("Index cannot be negative");
}
if (index >= size) {
throw out_of_range("Index is out of bounds");
}
int old = arr[index]; //Keep old value to output and then reassign to the new value
arr[index] = value;
cout << "Old Value:" << setw(14) << "New Value:" << endl;
cout << old << setw(15) << arr[index] << endl;
} catch (const out_of_range& e) {
cerr << "Error: " << e.what() << endl;
}
return;
}
int* Append(int* arr, int size){
int value;
cout << "Enter a number to append: ";
cin >> value;
if (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
}
try {
int* arr2 = new int[size + 1]; //Creating a new array to be returned, keeping old values and adding the appended value at the end
for (int i = 0; i < size; i++) {
arr2[i] = arr[i];
}
arr2[size] = value;
delete[] arr; //since were making a new array, clear the old one from memory
return arr2;
} catch (...) { //wasn't sure what to catch here so just using ...
cerr << "An unknown error occurred." << endl;
return arr;
}
}
void Remove(int arr[], int size){
int index;
cout << "Enter the index to be removed: ";
cin >> index;
if (cin.fail()) {
cin.clear();
cin.ignore(100, '\n');
}
for (int i = index; i < size - 1; i++){
arr[i] = arr[i + 1];
}
if (index == size - 1){
arr[index] = 0;
}
return;
}