Repository navigation
Expand file tree
/
Copy path8_reverse_float_array.cpp
More file actions
137 lines (113 loc) · 4.44 KB
/
Copy path8_reverse_float_array.cpp
File metadata and controls
137 lines (113 loc) · 4.44 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
134
135
136
137
/*
====================================================================
Reverse Float Array with Input Validation
--------------------------------------------------------------------
Description:
This C++ program prompts the user to input the size of a float array,
validating that the size is a positive integer (non-zero). The user
then enters float values to populate the array. The program reverses
the array contents in-place and displays the reversed array in a formatted output.
Features:
- Validates that array size is numeric and greater than zero
- Allows quitting by entering 'q' or 'Q'
- Reverses the float array in-place
- Displays the reversed array with formatted output
- Prompts user if they want to run the program again (only 'y' or 'n' accepted)
Author: [CS secttion 1, group 5]
Date: [June 5, 2025]
====================================================================
*/
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Maximum allowed size of the array
const int MAX_SIZE = 10000;
// Function to reverse the elements of a float array
void reverse(float array[], int size) {
int startIndex = 0, endIndex = size - 1;
float temp;
while (startIndex < endIndex) {
temp = array[startIndex];
array[startIndex] = array[endIndex];
array[endIndex] = temp;
startIndex++;
endIndex--;
}
}
// Function to validate that input is a positive, non-zero integer
bool isValidInput(string input) {
if (input.empty() || input == "0") return false;
for (int index = 0; index < input.length(); index++) {
if (!isdigit(input[index])) return false;
}
return true;
}
int main() {
string inputString;
int arraySize;
// Descriptive startup message
cout << "---------------------------------------------\n";
cout << "Welcome to the Float Array Reversal Program!\n";
cout << "This program allows you to enter float numbers\n";
cout << "into an array and then reverses the array.\n";
cout << "To quit at any time, type 'q'.\n";
cout << "---------------------------------------------\n\n";
while (true) {
cout << "Enter the size of your array : ";
cin >> inputString;
if (inputString == "Q" || inputString == "q") {
break;
}
if (!isValidInput(inputString)) {
cout << "Invalid input, please input a valid size of array.\n";
continue;
}
arraySize = stoi(inputString);
if (arraySize > MAX_SIZE) {
cout << "Array size too large. Please enter a size less than or equal to " << MAX_SIZE << ".\n";
continue;
}
float numbers[arraySize];
for (int index = 0; index < arraySize; index++) {
while (true) {
cout << "Enter the float element " << index + 1 << " : ";
cin >> numbers[index];
if (cin.fail()) {
cin.clear(); // clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard invalid input
cout << "Invalid input. Please enter a valid float value.\n";
} else {
break; // valid input
}
}
}
reverse(numbers, arraySize);
cout << "The reversed array is : " << endl;
cout << "[";
for (int index = 0; index < arraySize; index++) {
if (index == arraySize - 1) {
cout << numbers[index];
break;
}
cout << numbers[index] << ",";
}
cout << "]\n";
// Ask user if they want to reverse another array (strict yes/no)
string choice;
while (true) {
cout << "\nDo you want to reverse another array? (y/n): ";
cin >> choice;
if (choice == "y" || choice == "Y") {
cout << endl;
break; // Continue outer main loop
} else if (choice == "n" || choice == "N") {
cout << "\nThank you for using the program. GoodBye!" << endl;
return 0; // Exit program immediately
} else {
cout << "\nPlease enter 'y' for yes or 'n' for no." << endl;
}
}
}
return 0;
}