Originally Posted by
Babkockdood
You could find the number of columns in the matrix by finding how many integers there are in each line, then you could find the number of rows in the matrix by simply counting the lines (or count how many lines begin with an integer, if you want to be safe). Then create a two-dimensional array with each number you found.
And you can just use fscanf to get the array's elements.
I did just that.
The code below isn't complete, i realized that the path algorithm would only detect shortest routes adjacent to the current position, thus failing in the first example matrix in my original post.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
FILE *file = fopen("/Users/bluej322/desktop/sequence.txt","r");
char val=getc(file);
char line[40];
char tmpNum[10];
int totalLength = 0;
int currentRow = 1;
int x = 0;
int y = 0;
int rowCount = 1;
int colCount = 1;
int row;
int col;
int endQue=0;
int endNumber=0;
int lastDirect=1; //1=up,2=right,3=down,4=left
while (val!=EOF) {
printf("%c",val);
line[totalLength]=val;
++totalLength;
val=getc(file);
}
if (val=EOF) {
line[totalLength]=':';
printf("\n");
}
fclose(file);
printf("File Closed\n");
//row/col count
x=0;
while (line[x]!='\n') {
if (line[x]==',') {
++colCount;
}
++x;
}
col=colCount;
for (x=0;x<totalLength;++x) {
if (line[x]=='\n') {
++rowCount;
}
}
row=rowCount;
int matrix[row][col];
int path[totalLength];
int index=0;
printf("Rows: %d\nColumns: %d\n",rowCount,colCount);
//proto4
for (x=0,y=0,row=0,col=0;x<totalLength;++x) {
if (isdigit(line[x])) {
tmpNum[y]=line[x];
++y;
if (line[x+1]=='\n' || line[x+1]==':') {
tmpNum[y]='\0';
printf("- %s\n",tmpNum);
matrix[row][col]=atoi(tmpNum);
printf("Matrix[%d][%d]: %d\n",row,col,matrix[row][col]);
++col;
y=0;
if (line[x+1]==':') {
endNumber=matrix[row][col];
}
}
}else if (line[x]=='\n') {
++row;
printf("++ROW\n");
col=0;
y=0;
}else {
tmpNum[y]='\0';
printf("- %s\n",tmpNum);
matrix[row][col]=atoi(tmpNum);
printf("Matrix[%d][%d]: %d\n",row,col,matrix[row][col]);
printf("++COL\n");
++col;
y=0;
}
}
printf("\n");
for (x=0;x<rowCount;++x) {
for (y=0;y<colCount;++y) {
printf(" %d ",matrix[x][y]);
}
printf("\n");
}
//path algorithm
x=0;
y=0;
while (endQue==0) {
if (x==row && y==col) {
path[index]=matrix[x][y];
endQue=1;
//top-left
}else if (y-1<0 && x-1<0) {
if (matrix[x+1][y]<matrix[x][y+1]) {
printf("down\n");
path[index]=matrix[x][y];
++x;
++index;
lastDirect=3;
}else{
printf("right\n");
path[index]=matrix[x][y];
++y;
++index;
lastDirect=2;
}
//left side, space up and down
}else if (y-1<0 && (x+1)<=rowCount-1 && (x-1)>=0) {
if (matrix[x+1][y]<matrix[x-1][y]) {
if (matrix[x+1][y]<matrix[x][y+1] && lastDirect!=1) {
printf("down\n");
}else {
printf("right\n");
path[index]=matrix[x][y];
++y;
++index;
lastDirect=2;
}
}else if (matrix[x-1][y]<matrix[x+1][y]) {
if (matrix[x-1][y]<matrix[x][y+1] && lastDirect!=3) {
printf("up\n");
path[index]=matrix[x][y];
--x;
++index;
lastDirect=1;
}else{
printf("right\n");
path[index]=matrix[x][y];
++y;
++index;
lastDirect=2;
}
}
//right side, space up and down
}else if (y+1>colCount-1 && x-1>=0 && x+1<=rowCount-1) {
if (matrix[x+1][y]<matrix[x-1][y]) {
if (matrix[x+1][y]<matrix[x][y-1] && lastDirect!=1) {
printf("down\n");
path[index]=matrix[x][y];
++x;
++index;
lastDirect=3;
}else{
printf("left\n");
path[index]=matrix[x][y];
--y;
++index;
lastDirect=4;
}
}else if (matrix[x-1][y]<matrix[x+1][y]) {
if (matrix[x-1][y]<matrix[x][y-1] && lastDirect!=3) {
printf("up\n");
}else {
printf("left\n");
path[index]=matrix[x][y];
--y;
++index;
lastDirect=4;
}
}
//middle
}else if (x+1<=rowCount-1 && x-1>=0 && y+1<=colCount-1 && y-1>=0) {
if (matrix[x-1][y]<matrix[x+1][y]) {
if (matrix[x-1][y]<matrix[x][y-1]) {
if (matrix[x-1][y]<matrix[x][y+1] && lastDirect!=3) {
printf("up\n");
}else {
printf("right\n");
}
}else if (matrix[x][y-1]<matrix[x-1][y]) {
if (matrix[x][y-1]<matrix[x][y+1] && lastDirect!=2) {
printf("left");
}else {
printf("right\n");
}
}
}else if (matrix[x+1][y]<matrix[x-1][y]) {
if (matrix[x+1][y]<matrix[x][y-1]) {
<#statements#>
}else if (matrix[x+1][y]<matrix[x][y+1]) {
<#statements#>
}
}
}
}
}
}