Question: I have this program that reads a file that looks like this : 1,2,3,6,7,8,9,4,5, 5,8,4,2,3,9,7,6,1, 9,6,7,1,4,5,3,2,8, 3,7,2,4,6,1,5,8,9, 6,9,1,5,8,3,2,7,4, 4,5,8,7,9,2,6,1,3, 8,3,6,9,2,4,1,5,7, 2,1,9,8,5,7,4,3,6, 7,4,5,3,1,6,8,9,2, What i have
I have this program that reads a file that looks like this :
1,2,3,6,7,8,9,4,5,
5,8,4,2,3,9,7,6,1,
9,6,7,1,4,5,3,2,8,
3,7,2,4,6,1,5,8,9,
6,9,1,5,8,3,2,7,4,
4,5,8,7,9,2,6,1,3,
8,3,6,9,2,4,1,5,7,
2,1,9,8,5,7,4,3,6,
7,4,5,3,1,6,8,9,2,
What i have to do is read the File display it on screen, and find a seqnce of numbers. So far i have this, but im having trouble getting the seqnce input, while trying to deal with the menue.
#include
#pragma warning(disable:4996)//to stop fopen_s error.
char mymatrix(FILE *fp);
int cols(FILE *fp) {
char ch;
int col = 0;
//reading the file till end of the file
while ((ch = getc(fp)) != EOF) {
//if current character is coma then we increment the columns
if (ch == ',') {
col++;
}
//if new line is found we can break the loop
if (ch == ' ') {
break;
}
}
//returning the columnss
return col;
}
//function to calculate number of rows
int rows(FILE *fp) {
char ch;
int row = 0;
//reading the file till end of the file
while ((ch = getc(fp)) != EOF) {
//for every new line we increment the rows
if (ch == ' ') {
row++;
}
}
//retruning the rows
return row;
}
int main()
{
char input[40][40], ch, option;
int N = 0, M = 0, col, row;
char choice = 65; int end = 0;
int menucounter = 0;
printf("Hello, this program will read a file that you will input its location. What i wiill do is find a sequnce of your choice! ");
printf(" a.Enter file location(fullpath) b.Display the puzzle c.Enter a sequence to find d.Exit ");
while (end != 1)
{
choice = getchar();
if (' ' == choice)
{
/* Be silent on linefeeds */
continue;
}
switch (choice)
{
case 'a':
case 'A':
{
menucounter = 1;
printf("Please enter the file location ");
char filename[100];
scanf("%s", filename);
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error, please press A and try again ");
continue;
}
else
{
printf("I found it ");
}
do {
// read characters one by one
fscanf(fp, "%c", &ch);
//if new line char found, go to next row (increase N)
// and set N=0.
if (ch == ' ') {
N++;
M = 0;
}
//if character is not newline or not comma
// then put char in the array
//increase M
if (ch != ',') {
input[N][M] = ch;
M++;
if (col col = M; } } } while (!feof(fp)); // do till eof reache col = col - 1; N = N - 1; break; } case 'b': case 'B': { int i, j; if (menucounter == 0) { printf("Please enter option A first "); break; } else { for (i = 0; i < N; i++) { printf(" "); // new row for (j = 0; j < col +1; j++) { //column printf("%c ", input[i][j]); } //closing the file } } printf(" "); break; } case 'C': case 'c': { if (menucounter == 0) { printf("Please enter option A first "); } else { printf("Enter a sequnce "); int i, j; char user[10]; char ch; char string[15]; // big enough array to hold input int size = 0; while (1) { ch = getchar(); if (ch == ' ') { break; } if (ch >= '0' && ch <= '9') { string[size++] = ch; } } // displaying char array printf("Your string is: "); for (i = 0; i < size; ++i) { printf("%c ", string[i]); } int check = 1; int k = 0; int count = 0, rowfound, columnfound; for (i = 0; i < 2; i++) { for (j = 0; j < 10; j++) { if (input[i][j] == string[0]) { columnfound = j; count = 0; count++; int ref = j; for (k = 1; k < size && ref < 10; k++, ref++) if (ref < 10 && input[i][ref + 1] == string[k]) { // printf("yes "); // printf("%c %c ",mynums[i][ref+1],string[k] ); rowfound = i; count++; } } if (count == size) { // printf(" %d : %d ",count,size ); break; } } } j = 0; printf(" "); if (count == size) { while (j < size) { printf("%c found at [%d][%d] ", string[j], rowfound + 1, columnfound + 1); columnfound++; j++; } } else { printf("Not found!!! "); } } break; } case 'd': case 'D': { printf("Goodbye! "); end++; break; default: printf("!ERROR, invalid choice, pick a valid menu option! "); break; } } } system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
