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
|
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
ifstream fin;
#define SIZE 100
int maxRow = -1, maxColumn = 0;
void getInput(int input[SIZE][SIZE]);
void makeNewArray(int oldArray[SIZE][SIZE], int newArray[SIZE][SIZE]);
void pixelAverage(int inArray[SIZE][SIZE], int outArray[SIZE][SIZE]);
void printArray(int array[SIZE][SIZE]);
int main() {
fin.open("testData.txt");
int inputArray[SIZE][SIZE] = {0},
outputArray[SIZE][SIZE] = {0};
getInput(inputArray);
makeNewArray(inputArray, outputArray);
pixelAverage(inputArray, outputArray);
printArray(outputArray);
}
void getInput(int input[SIZE][SIZE]) {
string test, temp1; int temp2=0;
//I would mainly like to thank some random person on StackOverflow for solving my problem
while (getline(cin,test)) {
temp2 = -2;
istringstream ss(test);
while(getline(ss,temp1,' ')) {
temp2++;
input[maxRow][temp2] = stoi(temp1);
}
if (temp2>maxColumn) {
maxColumn = temp2+2; //2 is added to make the math work.
}
maxRow++;
}
maxRow++;
}
void makeNewArray(int oldArray[SIZE][SIZE], int newArray[SIZE][SIZE]) {
for (int i = 0; i < SIZE-1; i++) {
for (int j = 0; j < SIZE-1; j++) {
newArray[i][j] = oldArray[i-1][j-1];
}
}
}
void pixelAverage(int inArray[SIZE][SIZE], int outArray[SIZE][SIZE]) {
int sum = 0, n = 0, average, timesRan=0; //n is the number of surrounding pixels
for (int i = -1; i < maxRow-1; i++) {
for (int j = -1; j < maxColumn-1; j++) {
sum=0; n=0;
if ( !((i-1)<1) ) {
cout << "LEFT)" << inArray[i-1][j] << " ";
sum += inArray[i-1][j];
n++;
}
if ( (i+1)<maxRow ) {
cout << "DOWN)" << inArray[i+1][j] << " ";
sum += inArray[i+1][j];
n++;
}
if ( !((j-1)<1) ) {
cout << "UP)" << inArray[i][j-1] << " ";
sum += inArray[i][j-1];
n++;
}
if ( (j+1)<maxColumn ) {
cout << "RIGHT)" << inArray[i][j+1] << " ";
sum += inArray[i][j+1];
n++;
}
if ( !((i-1)<0) && !((j-1)<0) ) {
cout << "UPLEFT)" << inArray[i-1][j-1] << " ";
sum += inArray[i-1][j-1];
n++;
}
if ( !((i-1)<0) && ( (j+1)<maxColumn ) ) {
cout << "DOWNLEFT)" << inArray[i-1][j+1] << " ";
sum += inArray[i-1][j+1];
n++;
}
if ( (i+1)<maxRow && (j+1)<maxColumn ) {
cout << "DOWNRIGHT)" << inArray[i+1][j+1] << " ";
sum += inArray[i+1][j+1];
n++;
}
if ( !((j-1)<0) && (i+1)<maxRow) {
cout << "UPRIGHT)" << inArray[i+1][j-1] << " ";
sum+=inArray[i+1][j-1];
n++;
}
cout << inArray[i][j] << " ";
sum+=inArray[i][j];
n++;
timesRan++;
average = sum/n;
cout << endl << sum << " " << n << " " << average << endl;
outArray[i+1][j+1]=average;
}
}
cout << timesRan << endl;
}
void printArray (int array[SIZE][SIZE]) {
for (int i = 0; i < maxRow; i++)
{
for (int j = 0; j < maxColumn; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
|