Dilation function for char* in c++ not printing as it should be
up vote
2
down vote
favorite
I've made a program in c++ that takes in a file and the morph to apply to it through the command line. But, so far. My algorithm is taking in a file like :
.............
.............
..XXX.....X..
..XXX.....X..
..XXX........
..XXX........
..XXXXXXX....
..XXXXXXX....
..XXXXXXX....
.............
.............
and prints out something that looks like this :
.............
..X.X.....X..
.XXXXX...XXX.
..XXX.....X..
.XXXXX.......
..XXX.X.X....
.XXXXXXXXX...
..XXXXXXX....
.XXXXXXXXX...
..X.X.X.X....
.............
When it should be printing out something that looks like this:
.............
..XXX.....X..
.XXXXX...XXX.
.XXXXX...XXX.
.XXXXX....X..
.XXXXXXXX....
.XXXXXXXXX...
.XXXXXXXXX...
.XXXXXXXXX...
..XXXXXXX....
.............
I've made my program to go through each and every index and check to see if its the item they want to dilate, then look above, below, left, and right. To add another copy of its index to its surrounding grid. But why is my program printing out like that?
Here is the code I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
{
for (vector<char> &v : vec) // reference to innver vector
{
replace(v.begin(), v.end(), oldd, neww); // standard library algorithm
}
}
void dialationn(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
void erode(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
int main(int argc, char* argv) {
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
while(fin)
{
ch = fin.get();
if(ch!='n') {
temp.push_back(ch);
}
else
{
data.push_back(temp);
temp.clear();
}
}
if (argument2 == "replace") {
string argument4 (argv[4]);
string argument3 (argv[3]);
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j];
}
cout << endl;
}
}
if (argument2 == "dialate") {
string argument3 (argv[3]);
dialationn(data, argument3[0], 'i');
for (int m = 0; m < data.size(); m ++) {
for (int n = 0; n < data[m].size(); n++) {
cout << data[m][n];
}
cout << endl;
}
}
fin.close();
}
The main function takes the first argument as the text file name to parse it. Then, it looks at the second argument to see what the user wants to do, then based on argument 2, it calls the appropriate function replacee
or dialate
to apply a morph. After the char* containing the file has been modified from one of the functions, it then prints it out.
Note: the dialatee
function changes all the characters it sees fit to 'i' then calls the replacee
function to turn them back into the original output. This is done to prevent the function from reading already modified characters and applying unintentional morphs.
c++ vector char
add a comment |
up vote
2
down vote
favorite
I've made a program in c++ that takes in a file and the morph to apply to it through the command line. But, so far. My algorithm is taking in a file like :
.............
.............
..XXX.....X..
..XXX.....X..
..XXX........
..XXX........
..XXXXXXX....
..XXXXXXX....
..XXXXXXX....
.............
.............
and prints out something that looks like this :
.............
..X.X.....X..
.XXXXX...XXX.
..XXX.....X..
.XXXXX.......
..XXX.X.X....
.XXXXXXXXX...
..XXXXXXX....
.XXXXXXXXX...
..X.X.X.X....
.............
When it should be printing out something that looks like this:
.............
..XXX.....X..
.XXXXX...XXX.
.XXXXX...XXX.
.XXXXX....X..
.XXXXXXXX....
.XXXXXXXXX...
.XXXXXXXXX...
.XXXXXXXXX...
..XXXXXXX....
.............
I've made my program to go through each and every index and check to see if its the item they want to dilate, then look above, below, left, and right. To add another copy of its index to its surrounding grid. But why is my program printing out like that?
Here is the code I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
{
for (vector<char> &v : vec) // reference to innver vector
{
replace(v.begin(), v.end(), oldd, neww); // standard library algorithm
}
}
void dialationn(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
void erode(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
int main(int argc, char* argv) {
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
while(fin)
{
ch = fin.get();
if(ch!='n') {
temp.push_back(ch);
}
else
{
data.push_back(temp);
temp.clear();
}
}
if (argument2 == "replace") {
string argument4 (argv[4]);
string argument3 (argv[3]);
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j];
}
cout << endl;
}
}
if (argument2 == "dialate") {
string argument3 (argv[3]);
dialationn(data, argument3[0], 'i');
for (int m = 0; m < data.size(); m ++) {
for (int n = 0; n < data[m].size(); n++) {
cout << data[m][n];
}
cout << endl;
}
}
fin.close();
}
The main function takes the first argument as the text file name to parse it. Then, it looks at the second argument to see what the user wants to do, then based on argument 2, it calls the appropriate function replacee
or dialate
to apply a morph. After the char* containing the file has been modified from one of the functions, it then prints it out.
Note: the dialatee
function changes all the characters it sees fit to 'i' then calls the replacee
function to turn them back into the original output. This is done to prevent the function from reading already modified characters and applying unintentional morphs.
c++ vector char
2
"while(fin)
" ... Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 11 at 6:13
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've made a program in c++ that takes in a file and the morph to apply to it through the command line. But, so far. My algorithm is taking in a file like :
.............
.............
..XXX.....X..
..XXX.....X..
..XXX........
..XXX........
..XXXXXXX....
..XXXXXXX....
..XXXXXXX....
.............
.............
and prints out something that looks like this :
.............
..X.X.....X..
.XXXXX...XXX.
..XXX.....X..
.XXXXX.......
..XXX.X.X....
.XXXXXXXXX...
..XXXXXXX....
.XXXXXXXXX...
..X.X.X.X....
.............
When it should be printing out something that looks like this:
.............
..XXX.....X..
.XXXXX...XXX.
.XXXXX...XXX.
.XXXXX....X..
.XXXXXXXX....
.XXXXXXXXX...
.XXXXXXXXX...
.XXXXXXXXX...
..XXXXXXX....
.............
I've made my program to go through each and every index and check to see if its the item they want to dilate, then look above, below, left, and right. To add another copy of its index to its surrounding grid. But why is my program printing out like that?
Here is the code I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
{
for (vector<char> &v : vec) // reference to innver vector
{
replace(v.begin(), v.end(), oldd, neww); // standard library algorithm
}
}
void dialationn(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
void erode(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
int main(int argc, char* argv) {
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
while(fin)
{
ch = fin.get();
if(ch!='n') {
temp.push_back(ch);
}
else
{
data.push_back(temp);
temp.clear();
}
}
if (argument2 == "replace") {
string argument4 (argv[4]);
string argument3 (argv[3]);
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j];
}
cout << endl;
}
}
if (argument2 == "dialate") {
string argument3 (argv[3]);
dialationn(data, argument3[0], 'i');
for (int m = 0; m < data.size(); m ++) {
for (int n = 0; n < data[m].size(); n++) {
cout << data[m][n];
}
cout << endl;
}
}
fin.close();
}
The main function takes the first argument as the text file name to parse it. Then, it looks at the second argument to see what the user wants to do, then based on argument 2, it calls the appropriate function replacee
or dialate
to apply a morph. After the char* containing the file has been modified from one of the functions, it then prints it out.
Note: the dialatee
function changes all the characters it sees fit to 'i' then calls the replacee
function to turn them back into the original output. This is done to prevent the function from reading already modified characters and applying unintentional morphs.
c++ vector char
I've made a program in c++ that takes in a file and the morph to apply to it through the command line. But, so far. My algorithm is taking in a file like :
.............
.............
..XXX.....X..
..XXX.....X..
..XXX........
..XXX........
..XXXXXXX....
..XXXXXXX....
..XXXXXXX....
.............
.............
and prints out something that looks like this :
.............
..X.X.....X..
.XXXXX...XXX.
..XXX.....X..
.XXXXX.......
..XXX.X.X....
.XXXXXXXXX...
..XXXXXXX....
.XXXXXXXXX...
..X.X.X.X....
.............
When it should be printing out something that looks like this:
.............
..XXX.....X..
.XXXXX...XXX.
.XXXXX...XXX.
.XXXXX....X..
.XXXXXXXX....
.XXXXXXXXX...
.XXXXXXXXX...
.XXXXXXXXX...
..XXXXXXX....
.............
I've made my program to go through each and every index and check to see if its the item they want to dilate, then look above, below, left, and right. To add another copy of its index to its surrounding grid. But why is my program printing out like that?
Here is the code I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
{
for (vector<char> &v : vec) // reference to innver vector
{
replace(v.begin(), v.end(), oldd, neww); // standard library algorithm
}
}
void dialationn(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
void erode(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
int main(int argc, char* argv) {
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
while(fin)
{
ch = fin.get();
if(ch!='n') {
temp.push_back(ch);
}
else
{
data.push_back(temp);
temp.clear();
}
}
if (argument2 == "replace") {
string argument4 (argv[4]);
string argument3 (argv[3]);
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j];
}
cout << endl;
}
}
if (argument2 == "dialate") {
string argument3 (argv[3]);
dialationn(data, argument3[0], 'i');
for (int m = 0; m < data.size(); m ++) {
for (int n = 0; n < data[m].size(); n++) {
cout << data[m][n];
}
cout << endl;
}
}
fin.close();
}
The main function takes the first argument as the text file name to parse it. Then, it looks at the second argument to see what the user wants to do, then based on argument 2, it calls the appropriate function replacee
or dialate
to apply a morph. After the char* containing the file has been modified from one of the functions, it then prints it out.
Note: the dialatee
function changes all the characters it sees fit to 'i' then calls the replacee
function to turn them back into the original output. This is done to prevent the function from reading already modified characters and applying unintentional morphs.
c++ vector char
c++ vector char
edited Nov 11 at 9:56
asked Nov 11 at 5:40
xannax159
448
448
2
"while(fin)
" ... Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 11 at 6:13
add a comment |
2
"while(fin)
" ... Why is iostream::eof inside a loop condition considered wrong?
– Swordfish
Nov 11 at 6:13
2
2
"
while(fin)
" ... Why is iostream::eof inside a loop condition considered wrong?– Swordfish
Nov 11 at 6:13
"
while(fin)
" ... Why is iostream::eof inside a loop condition considered wrong?– Swordfish
Nov 11 at 6:13
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
You are overwriting your data before you parse it. Before setting an element of vec
to n
, make sure it is not suspect
.
Stepping through your code with a good debugger should let you see this happening.
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
I tried to implement this solution by incorporating lines ofif
statements like this one,if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.
– xannax159
Nov 11 at 6:28
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
add a comment |
up vote
1
down vote
When you hit the first 'X' you will write 'i' to the spaces above, below, left and right of it, replacing other 'X's you haven't read yet.
After your first pass through, things probably look like:
.............
..i.i.....i..
.iXiXi...iXi.
..iXi.....i..
.iXiXi.......
..XiX........
.iXiXiXiXi...
..XiXiXiX....
.iXiXiXiXi...
..i.i.i.i....
.............
So as you get to a spot that has been switched from 'X' to 'i' it doesn't match, and doesn't get treated the same way.
An easy fix would be to create a new vector, and any time you find an 'X' in the original vector, you place your 'i's up, down, left, right, and directly on (in the new vector) where the 'X' would be in your original vector. Then pass the new one on, since efficiency isn't your highest priority. This will avoid the overwriting, while reading, issues.
2
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
1
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
2
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
add a comment |
up vote
1
down vote
I think the following fixed version of dialationn
will work fine.
I removed the last argument n
and used cache table:
void dialationn(std::vector<std::vector<char>> &vec, char suspect)
{
std::remove_reference<decltype(vec)>::type cache(vec);
for (std::size_t i = 0; i < cache.size(); ++i) {
for (std::size_t j = 0; j < cache[i].size(); ++j) {
if (cache[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = suspect;
}
if (j > 0) {
vec[i][j-1] = suspect;
}
if (i + 1<vec.size()) {
vec[i+1][j] = suspect;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = suspect;
}
}
}
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You are overwriting your data before you parse it. Before setting an element of vec
to n
, make sure it is not suspect
.
Stepping through your code with a good debugger should let you see this happening.
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
I tried to implement this solution by incorporating lines ofif
statements like this one,if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.
– xannax159
Nov 11 at 6:28
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
add a comment |
up vote
1
down vote
accepted
You are overwriting your data before you parse it. Before setting an element of vec
to n
, make sure it is not suspect
.
Stepping through your code with a good debugger should let you see this happening.
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
I tried to implement this solution by incorporating lines ofif
statements like this one,if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.
– xannax159
Nov 11 at 6:28
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You are overwriting your data before you parse it. Before setting an element of vec
to n
, make sure it is not suspect
.
Stepping through your code with a good debugger should let you see this happening.
You are overwriting your data before you parse it. Before setting an element of vec
to n
, make sure it is not suspect
.
Stepping through your code with a good debugger should let you see this happening.
answered Nov 11 at 6:06
JaMiT
1,07212
1,07212
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
I tried to implement this solution by incorporating lines ofif
statements like this one,if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.
– xannax159
Nov 11 at 6:28
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
add a comment |
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
I tried to implement this solution by incorporating lines ofif
statements like this one,if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.
– xannax159
Nov 11 at 6:28
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
Ill try to implement this solution, So basically, dont add an i to a given direction of the pointer, while the index in the direction of the pointer = x.
– xannax159
Nov 11 at 6:21
I tried to implement this solution by incorporating lines of
if
statements like this one, if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.– xannax159
Nov 11 at 6:28
I tried to implement this solution by incorporating lines of
if
statements like this one, if(vec[i][j-1] != suspect) { vec[i][j-1] = n; }
but it didnt work, instead it made more than 95% of the grid = to suspect.– xannax159
Nov 11 at 6:28
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
i may be the biggest idiot ever, you see, my program is supposed to work with multiple files with different arrangements. and i was testing the wrong file the entire time trying to compare it to the one in the post. You're answer is indeed correct. I apologize for being such an idiot.
– xannax159
Nov 11 at 6:37
add a comment |
up vote
1
down vote
When you hit the first 'X' you will write 'i' to the spaces above, below, left and right of it, replacing other 'X's you haven't read yet.
After your first pass through, things probably look like:
.............
..i.i.....i..
.iXiXi...iXi.
..iXi.....i..
.iXiXi.......
..XiX........
.iXiXiXiXi...
..XiXiXiX....
.iXiXiXiXi...
..i.i.i.i....
.............
So as you get to a spot that has been switched from 'X' to 'i' it doesn't match, and doesn't get treated the same way.
An easy fix would be to create a new vector, and any time you find an 'X' in the original vector, you place your 'i's up, down, left, right, and directly on (in the new vector) where the 'X' would be in your original vector. Then pass the new one on, since efficiency isn't your highest priority. This will avoid the overwriting, while reading, issues.
2
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
1
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
2
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
add a comment |
up vote
1
down vote
When you hit the first 'X' you will write 'i' to the spaces above, below, left and right of it, replacing other 'X's you haven't read yet.
After your first pass through, things probably look like:
.............
..i.i.....i..
.iXiXi...iXi.
..iXi.....i..
.iXiXi.......
..XiX........
.iXiXiXiXi...
..XiXiXiX....
.iXiXiXiXi...
..i.i.i.i....
.............
So as you get to a spot that has been switched from 'X' to 'i' it doesn't match, and doesn't get treated the same way.
An easy fix would be to create a new vector, and any time you find an 'X' in the original vector, you place your 'i's up, down, left, right, and directly on (in the new vector) where the 'X' would be in your original vector. Then pass the new one on, since efficiency isn't your highest priority. This will avoid the overwriting, while reading, issues.
2
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
1
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
2
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
add a comment |
up vote
1
down vote
up vote
1
down vote
When you hit the first 'X' you will write 'i' to the spaces above, below, left and right of it, replacing other 'X's you haven't read yet.
After your first pass through, things probably look like:
.............
..i.i.....i..
.iXiXi...iXi.
..iXi.....i..
.iXiXi.......
..XiX........
.iXiXiXiXi...
..XiXiXiX....
.iXiXiXiXi...
..i.i.i.i....
.............
So as you get to a spot that has been switched from 'X' to 'i' it doesn't match, and doesn't get treated the same way.
An easy fix would be to create a new vector, and any time you find an 'X' in the original vector, you place your 'i's up, down, left, right, and directly on (in the new vector) where the 'X' would be in your original vector. Then pass the new one on, since efficiency isn't your highest priority. This will avoid the overwriting, while reading, issues.
When you hit the first 'X' you will write 'i' to the spaces above, below, left and right of it, replacing other 'X's you haven't read yet.
After your first pass through, things probably look like:
.............
..i.i.....i..
.iXiXi...iXi.
..iXi.....i..
.iXiXi.......
..XiX........
.iXiXiXiXi...
..XiXiXiX....
.iXiXiXiXi...
..i.i.i.i....
.............
So as you get to a spot that has been switched from 'X' to 'i' it doesn't match, and doesn't get treated the same way.
An easy fix would be to create a new vector, and any time you find an 'X' in the original vector, you place your 'i's up, down, left, right, and directly on (in the new vector) where the 'X' would be in your original vector. Then pass the new one on, since efficiency isn't your highest priority. This will avoid the overwriting, while reading, issues.
edited Nov 11 at 6:23
answered Nov 11 at 6:10
DMarczak
1119
1119
2
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
1
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
2
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
add a comment |
2
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
1
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
2
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
2
2
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
Your explanation is on point and i understand why it wont work, but your solution doesn't work. And i agree that there could be a more efficient solution but in the end, efficiency inst high up on the scale for this program.
– xannax159
Nov 11 at 6:18
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
instead, it brings back the problem that calling the replace function fixes.
– xannax159
Nov 11 at 6:22
1
1
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
I have amended my answer, as you have rightly pointed out, that my original suggestion was a bit off. Please let me know if this new suggestion isn't going to work for you.
– DMarczak
Nov 11 at 6:24
2
2
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
Thanks for helping me understand what was going on. Your solution does indeed work with the revision but before you changed it I tested another solution from another user that already worked with minimal code needed. I've tested your solution and indeed it does work. Thank you for the suggestions!
– xannax159
Nov 11 at 6:39
add a comment |
up vote
1
down vote
I think the following fixed version of dialationn
will work fine.
I removed the last argument n
and used cache table:
void dialationn(std::vector<std::vector<char>> &vec, char suspect)
{
std::remove_reference<decltype(vec)>::type cache(vec);
for (std::size_t i = 0; i < cache.size(); ++i) {
for (std::size_t j = 0; j < cache[i].size(); ++j) {
if (cache[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = suspect;
}
if (j > 0) {
vec[i][j-1] = suspect;
}
if (i + 1<vec.size()) {
vec[i+1][j] = suspect;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = suspect;
}
}
}
}
}
add a comment |
up vote
1
down vote
I think the following fixed version of dialationn
will work fine.
I removed the last argument n
and used cache table:
void dialationn(std::vector<std::vector<char>> &vec, char suspect)
{
std::remove_reference<decltype(vec)>::type cache(vec);
for (std::size_t i = 0; i < cache.size(); ++i) {
for (std::size_t j = 0; j < cache[i].size(); ++j) {
if (cache[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = suspect;
}
if (j > 0) {
vec[i][j-1] = suspect;
}
if (i + 1<vec.size()) {
vec[i+1][j] = suspect;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = suspect;
}
}
}
}
}
add a comment |
up vote
1
down vote
up vote
1
down vote
I think the following fixed version of dialationn
will work fine.
I removed the last argument n
and used cache table:
void dialationn(std::vector<std::vector<char>> &vec, char suspect)
{
std::remove_reference<decltype(vec)>::type cache(vec);
for (std::size_t i = 0; i < cache.size(); ++i) {
for (std::size_t j = 0; j < cache[i].size(); ++j) {
if (cache[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = suspect;
}
if (j > 0) {
vec[i][j-1] = suspect;
}
if (i + 1<vec.size()) {
vec[i+1][j] = suspect;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = suspect;
}
}
}
}
}
I think the following fixed version of dialationn
will work fine.
I removed the last argument n
and used cache table:
void dialationn(std::vector<std::vector<char>> &vec, char suspect)
{
std::remove_reference<decltype(vec)>::type cache(vec);
for (std::size_t i = 0; i < cache.size(); ++i) {
for (std::size_t j = 0; j < cache[i].size(); ++j) {
if (cache[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = suspect;
}
if (j > 0) {
vec[i][j-1] = suspect;
}
if (i + 1<vec.size()) {
vec[i+1][j] = suspect;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = suspect;
}
}
}
}
}
edited Nov 11 at 7:56
answered Nov 11 at 6:37
Hiroki
7881313
7881313
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246154%2fdilation-function-for-char-in-c-not-printing-as-it-should-be%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
"
while(fin)
" ... Why is iostream::eof inside a loop condition considered wrong?– Swordfish
Nov 11 at 6:13