aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/Functions.cpp
blob: 0070aef13c7287d501978b2c73f09908649bfea5 (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "Header.h"

//13b

void getInput(int valuesList[], int& VALUE)
{
	while (valuesList[VALUE] != '\0')
	{
		VALUE++;

		cout << "\nEnter value #" << VALUE + 1 << ". Enter 0 to stop input and display median.\n";
		cin >> valuesList[VALUE];
		if (valuesList[VALUE] == 0)
		{
			valuesList[VALUE] = '\0';
		}
	}

	VALUE = 0;
}

void calculateLength(int valuesList[], int& VALUE, int& valuesTotal)
{
	while (valuesList[VALUE] != '\0')
	{
		valuesTotal++;
		VALUE++;
	}

	VALUE = 0;
}

void lowestToHighest(int valuesList[], int& VALUE, int& valuesTotal, int reorderList[], int& reorderTotal)
{
	while (reorderTotal < valuesTotal)
	{
		int mostBig = 0;
		int mostBigPos = 0;
		VALUE = 0;

		while (VALUE < valuesTotal)
		{
			if (valuesList[VALUE] > mostBig)
			{
				mostBig = valuesList[VALUE];
				mostBigPos = VALUE;
			}
			VALUE++;
		}

		reorderList[((valuesTotal)-reorderTotal) - 1] = mostBig;
		valuesList[mostBigPos] = 0;

		reorderTotal++;
	}

	VALUE = 0;

	while (VALUE < valuesTotal)
	{
		valuesList[VALUE] = reorderList[VALUE];

		VALUE++;
	}

	VALUE = 0;
}

void calculateMedian(int valuesList[], int& VALUE, int& valuesTotal)
{
	if (valuesTotal % 2 == 0)
	{
		int mid1 = 0;
		int mid2 = 0;

		while (VALUE <= (valuesTotal / 2))
		{

			if (VALUE == ((valuesTotal / 2) - 1))
			{
				mid1 = valuesList[VALUE];
			}

			if (VALUE == ((valuesTotal / 2)))
			{
				mid2 = valuesList[VALUE];
			}

			VALUE++;
		}

		cout << "\nThe median of your input is: " << (mid1 + mid2) / 2
			<< endl;

	}
	else
	{
		int mid = 0;

		VALUE = ((valuesTotal / 2));
		mid = valuesList[VALUE];

		cout << "\nThe median of your input is: " << mid
			<< endl;
	}
}

//13c

int checkFile()
{
	ifstream inFile;
	inFile.open("employee data.txt");

	if (!inFile)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

void createFile()
{
	ofstream newFile;
	newFile.open("employee data.txt");
	newFile.close();

	cout << "No file detected. "
		<< "A new file has been created. "
		<< "Please input data into the file and restart the program.\n";
}

void formatData()
{
	int wage[100] = {};
	string text;

	//if the file is detected
	cout << "File detected. Formatting data...\n"
		<< endl;

	ifstream readFile;
	readFile.open("employee data.txt");

	while (readFile)
	{
		getline(readFile, text);
		cout << text << endl;
	}

	cout << "\nAll data has been read. The program will now end."
		<< endl;
}