ah thanks guys dunno why I didn't think to just make an array for the returns, but now I'm having a little problem, I'm trying to add a fix or de-"glitching" function that compares a spot versus its neighbors. It works fine except along the edges I'm trying to figure a way to handle those values. Obviously I could easily just set up one loop for passing everything inside the box then make seperate loops for the edges and handle them seperately, but I want to get the function able to handle it, what I've made is
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
#define ROWS 20
#define COLS 30
char convert(int scan);
int fix(int center, int up, int down, int left, int right);
int main(void)
{
int i, j, orig[ROWS][COLS];
char picture[ROWS][COLS+1], read[MAX+1], mover[2];
FILE *fp;
if ((fp = fopen("/Users/philip/pict.txt", "r")) == NULL)
{
printf("Can't open pict filename\n");
exit(1);
}
i = 0;
while(fgets(read, 61, fp) != NULL)
{
// fputs(read, stdout);
for(j=0; j < COLS; j++)
{
strncpy(mover, read + (j*2), 2);
// puts(mover);
orig[i][j] = atoi(mover);
}
i++;
}
for(i=0; i < ROWS; i++)
for(j=0; j < COLS; j++)
orig[i][j] = fix(orig[i][j], orig[i+1][j], orig[i-1][j], orig[i][j-1], orig[i][j+1]);
/* puts("\n");
for(i=0; i < ROWS; i++)
{
for(j=0; j < COLS; j++)
printf("%d", orig[i][j]);
puts("");
}
*/
for(i=0; i < ROWS; i++)
{
for(j=0; j < COLS; j++)
picture[i][j] = convert(orig[i][j]);
picture[i][COLS] = '\0';
}
puts("\n");
for(i=0; i < ROWS; i++)
puts(picture[i]);
fclose(fp);
return 0;
}
int fix(int center, int up, int down, int left, int right)
{
int check = 0, a = 0;
if(up != NULL)
if ((center - up) > 1 || (center - up) < -1)
{
check++;
a++;
}
if(down != NULL)
if ((center - down) > 1 || (center - down) < -1)
{
check++;
a++;
}
if(left != NULL)
if ((center - left) > 1 || (center - left) < -1)
{
check++;
a++;
}
if(right != NULL)
if ((center - right) > 1 || (center - right) < -1)
{
check++;
a++;
}
if(check == 4)
return (up+down+left+right)/a;
else
return center;
}
char convert(int scan)
{
if (scan == 0)
return ' ';
if (scan == 1)
return '.';
if (scan == 2)
return '\'';
if (scan == 3)
return '\"';
if (scan == 4)
return '-';
if (scan == 5)
return '*';
if (scan == 6)
return '=';
if (scan == 7)
return '%';
if (scan == 8)
return '&';
if (scan == 9)
return '#';
return '?';
}
and obviously NULL is just saying "0" to it when its working with int so I'm not sure how I can check after the values are passed (though I could pass i and j and have if statements inside the function deciding if it's on an edge or not, but that would seem just as over complicated as setting up seperate for loops for the edges.
Also I swapped in brewbuck's shortened switch made and it replaces most higher numbers in the pict with ) or ( so I'll have to mess around and see why that's not working right. Though MacGyver's works fine.