Question: Simple C code fix. I have the 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,
Simple C code fix.
I have the 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,
How can i make it so the user types in the location of the file instead of hard coding it, like i have in this program.
I cant seem to make the scanf part work.
:
#include
#pragma warning(disable : 4996) // for visual stdio safe scan error
//function to calculate number of colmuns
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 + 2;
}
int main()
{
//file pointer
FILE *fp;
char cha;
//opening the file and reading the file
fp = fopen("H:\\puzzleInput.txt", "r");
//calculating number of rows and columns and storing into N and M
int M = cols(fp);
int N = rows(fp);
//closing the file
fclose(fp);
//character array to store data in the file
char mynums[40][40];
//opening the file and reading the file to store data in array
fp = fopen("H:\\puzzleInput.txt", "r");
/*
* loading the file into array
*/
int i = 0, j = 0;
//reading the file till end of the file
while ((cha = getc(fp)) != EOF)
{
//if current character is valid letter then store that character in to array
if (cha != ',' && cha != ' ' && cha != '\0' && cha != ' ') {
mynums[i][j++] = cha;
}
//for every new line we increment the index i is incremented
//and index j becomes 0
if (cha == ' ') {
i++;
j = 0;
}
}
/*
* displaying the array in given format
*/
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
printf("%c ", mynums[i][j]);
}
printf(" ");
}
printf("Enter a sequnce ");
char user[9];
char ch;
char string[15]; // big enough array to hold input
int size = 0;
printf("Enter a string of digits: ");
while (1) {
ch = getchar();
if (ch == ' ') {
break;
}
if (ch >= '0' && ch <= '9') {
string[size++] = ch;
}
}
char reversem[40][40];
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
reversem[i][M - 1 - j] = mynums[i][j];
}
}
char transpose[40][40];
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
transpose[j][i] = mynums[i][j];
}
}
char transposeM[40][40];
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
transposeM[i][N - 1 - j] = transpose[i][j];
}
}
// displaying char array
printf("Your string is: ");
for (i = 0; i < size; ++i) {
printf("%c ", string[i]);
}
// bool check=true;
// int j=0;
int check = 1;
int k = 0;
int count = 0, rowfound, columnfound;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
k = 0;
count = 0;
if (mynums[i][j] == string[k]) {
rowfound = i;
columnfound = j;
count = 0;
count++;
int ref = j;
for (k = 1; k < size; k++, ref++)//k should be less than size of input of user
{
if (ref < M && mynums[i][ref + 1] == string[k]) {
// printf("yes ");
// printf("%c %c ",mynums[i][ref+1],string[k] );
count++;
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
j = 0;
printf(" ");
if (count == size) //This shows string has been found
{
while (j < size)//printing positions
{
printf("%c found at [%d][%d] ", string[j], rowfound + 1, columnfound + 1);
columnfound++;
j++;
}
}
else {
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
k = 0;
count = 0;
if (reversem[i][j] == string[k]) {
rowfound = i;
columnfound = j;
count = 0;
count++;
int ref = j;
for (k = 1; k < size; k++, ref++)//k should be less than size of input of user
{
if (ref < M && reversem[i][ref + 1] == string[k]) {
// printf("yes ");
// printf("%c %c ",mynums[i][ref+1],string[k] );
count++;
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
j = 0;
printf(" ");
if (count == size) //This shows string has been found
{
while (j < size)//printing positions
{
printf("%c found at [%d][%d] ", string[j], rowfound + 1, 8 - columnfound + 1);
columnfound++;
j++;
}
}
else {
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
k = 0;
count = 0;
if (transpose[i][j] == string[k]) {
rowfound = i;
columnfound = j;
count = 0;
count++;
int ref = j;
for (k = 1; k < size; k++, ref++)//k should be less than size of input of user
{
if (ref < 9 && transpose[i][ref + 1] == string[k]) {
// printf("yes ");
// printf("%c %c ",mynums[i][ref+1],string[k] );
count++;
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
j = 0;
printf(" ");
if (count == size) //This shows string has been found
{
while (j < size)//printing positions
{
printf("%c found at [%d][%d] ", string[j], columnfound + 1, rowfound + 1);
columnfound++;
j++;
}
}
else {
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
k = 0;
count = 0;
if (transposeM[i][j] == string[k]) {
rowfound = i;
columnfound = j;
count = 0;
count++;
int ref = j;
for (k = 1; k < size; k++, ref++)//k should be less than size of input of user
{
if (ref < N && transposeM[i][ref + 1] == string[k]) {
// printf("yes ");
// printf("%c %c ",mynums[i][ref+1],string[k] );
count++;
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
}
if (count == size) //This shows string has been found
{
// printf(" %d : %d ",count,size );
break;
}
}
j = 0;
printf(" ");
if (count == size) //This shows string has been found
{
while (j < size)//printing positions
{
printf("%c found at [%d][%d] ", string[j], 8 - columnfound + 1, rowfound + 1);
columnfound++;
j++;
}
}
else {
printf("sorry, that set of numbers is not in this matrix ");
}
}
}
}
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
