feat: finish lab 5-1
This commit is contained in:
parent
d40127ed66
commit
c281462187
4 changed files with 136 additions and 0 deletions
|
@ -38,3 +38,9 @@ c_4_program_3 = executable(
|
|||
],
|
||||
install: true,
|
||||
)
|
||||
|
||||
c_5_lab_5_1 = executable(
|
||||
'c_5_lab_5-1',
|
||||
['src/chapter_5/lab_5-1/main.cpp'],
|
||||
install: true,
|
||||
)
|
||||
|
|
111
src/chapter_5/lab_5-1/main.cpp
Normal file
111
src/chapter_5/lab_5-1/main.cpp
Normal file
|
@ -0,0 +1,111 @@
|
|||
// include any standard libraries needed
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
// - Passes in an array along with the size of the array.
|
||||
// - Returns the mean of all values stored in the array.
|
||||
double mean(const double array[], int arraySize) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
sum += array[i];
|
||||
}
|
||||
return sum / arraySize;
|
||||
}
|
||||
|
||||
// - Passes in an array, the size of the array by reference, and the index of a
|
||||
// value to be removed from the array.
|
||||
// - Removes the value at this index by shifting all of the values after this
|
||||
// value up, keeping the same relative order of all values not removed.
|
||||
// - Reduces arraySize by 1.
|
||||
void remove(double array[], int &arraySize, int index) {
|
||||
if (index >= arraySize) {
|
||||
return;
|
||||
}
|
||||
for (int i = index; i < arraySize - 1; i++) {
|
||||
array[i] = array[i + 1];
|
||||
}
|
||||
arraySize--;
|
||||
}
|
||||
|
||||
// - Passes in an array and the size of the array.
|
||||
// - Outputs each value in the array separated by a comma and space, with no
|
||||
// comma, space or newline at the end.
|
||||
void display(const double array[], int arraySize) {
|
||||
for (int i = 0; i < arraySize - 1; i++) {
|
||||
std::cout << array[i] << ", ";
|
||||
}
|
||||
std::cout << array[arraySize - 1];
|
||||
}
|
||||
|
||||
const int ARR_CAP = 100;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
// verify file name provided on command line
|
||||
if (argc < 2) {
|
||||
std::cout << "provide the file name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// open file and verify it opened
|
||||
std::fstream file = std::fstream(argv[1]);
|
||||
if (!file.is_open()) {
|
||||
std::cout << "file does not exist or could not be opened" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Declare an array of doubles of size ARR_CAP.
|
||||
double arr[ARR_CAP];
|
||||
|
||||
// Fill the array with up to ARR_CAP doubles from the file entered at the
|
||||
// command line.
|
||||
double temp;
|
||||
int i = 0;
|
||||
while (file >> temp) {
|
||||
if (i >= ARR_CAP)
|
||||
break;
|
||||
|
||||
arr[i] = temp;
|
||||
i++;
|
||||
}
|
||||
|
||||
int arr_actual_size = i;
|
||||
|
||||
if (i < ARR_CAP) {
|
||||
for (; i < ARR_CAP; i++) {
|
||||
arr[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Call the mean function passing it this array and output the
|
||||
// value returned.
|
||||
|
||||
std::cout << "Mean: " << mean(arr, arr_actual_size) << std::endl << std::endl;
|
||||
|
||||
// Ask the user for the index (0 to size - 1) of the value they want to
|
||||
// remove.
|
||||
int user_idx;
|
||||
std::cout << "Enter index of value to be removed (0 to "
|
||||
<< arr_actual_size - 1 << "): " << std::endl;
|
||||
std::cin >> user_idx;
|
||||
|
||||
// Call the display function to output the array.
|
||||
std::cout << "Before removing value: ";
|
||||
display(arr, arr_actual_size);
|
||||
std::cout << std::endl;
|
||||
|
||||
// Call the remove function to remove the value at the index
|
||||
// provided by the user.
|
||||
remove(arr, arr_actual_size, user_idx);
|
||||
|
||||
// Call the display function again to output the array
|
||||
// with the value removed.
|
||||
std::cout << "After removing value: ";
|
||||
display(arr, arr_actual_size);
|
||||
std::cout << std::endl;
|
||||
|
||||
// Call the mean function again to get the new mean
|
||||
std::cout << "Mean: " << mean(arr, arr_actual_size);
|
||||
|
||||
return 0;
|
||||
}
|
18
src/chapter_5/lab_5-1/one_hundred.dat
Normal file
18
src/chapter_5/lab_5-1/one_hundred.dat
Normal file
|
@ -0,0 +1,18 @@
|
|||
10 2 4
|
||||
3 9 8.1
|
||||
2 5.6
|
||||
8.1 9 3.0 4.67 8 2
|
||||
8 4 2 1 19 29.32
|
||||
|
||||
4 5 9.1 9 2 3.1
|
||||
8 4 1 2 1.2 9.45
|
||||
8 4 3 3 2.12 3
|
||||
9
|
||||
11
|
||||
|
||||
-2.3 4 2 8 9 11 21.3 9 4 9 1
|
||||
8 2 9 0.45 4.5 2 1 9 5.4
|
||||
|
||||
4 5 6 3 2 9 5 6.3 -16 5 9 2 0.3 34 5 2.1 6 8.7 9.23 10
|
||||
|
||||
5 9 3.2 5.67 -9.8 2 3 9 4.5 2.89 4 8 7 21 -43.2 5.2 9 8 2.0
|
1
src/chapter_5/lab_5-1/seven.dat
Normal file
1
src/chapter_5/lab_5-1/seven.dat
Normal file
|
@ -0,0 +1 @@
|
|||
-1 -2 3 4 9 2 7
|
Loading…
Reference in a new issue