Question

I posted this question earlier but realized that the code was not easy to test since...

I posted this question earlier but realized that the code was not easy to test since it was just screenshots and not text so I am reposting it:

I'm very new at working with file streams, and I'm having trouble with a HW assignment I have to work on. For the assignment I have to read an input file named "MagicSquaresIn.txt" (included at the bottom) and I have to check whether they are normal, associative, and panmagic.

To check that I am reading the file correctly, I am using fscanf to populate an array in the program and then printf to make sure that the arrays match what is in the file, however my output is completely blank. Can someone tell me what I'm doing wrong with my code? Or if there is a better approach to working this problem out? I would appreciate any input. Thanks! Here is my code below:

#include <stdio.h>

#define MAX_SQUARESIZE 15

int magicSquare[MAX_SQUARESIZE][MAX_SQUARESIZE];

/*

void checkBasic(int[][MAX_SQUARESIZE], int);

int getMagicNumber(int);

void checkNormal(int[][MAX_SQUARESIZE], int, int);

void checkAssociative(int[][MAX_SQUARESIZE], int, int);

void checkPanMagic(int[][MAX_SQUARESIZE], int);

*/

int main()

{

FILE *filePtr; // MagicSquareIn.txt file pointer

if ((filePtr = fopen("MagicSquareIn.txt", "r")) == NULL)

{

printf("Error. The file could not be opened\n");

} /* end if */

else

{

int magicNumber;

int arraySize;

fscanf(filePtr, "%d", &arraySize);

//printf("file opened properly\n");

while(!feof(filePtr))

{

// Nested for loop reads from file to populate array

for (int rowNum = 0; rowNum < arraySize; rowNum++)

{

for (int colNum = 0; colNum < arraySize; colNum++)

{

fscanf(filePtr, "%d", &magicSquare[rowNum][colNum]);

} /* End for loop going through column numbers */

} /* End for loop going through row numbers */

// Nested for loop prints out the array

for (int rowNum = 0; rowNum < arraySize; rowNum++)

{

for (int colNum = 0; colNum < arraySize; colNum++)

{

printf("%d ", magicSquare[rowNum][colNum]);

} /* End for loop going through column numbers */

printf("\n");

} /* End for loop going through row numbers */

/*

printf("\n");

checkBasic(magicSquare, arraySize);

magicNumber = getMagicNumber(arraySize);

checkNormal(magicSquare, arraySize, magicNumber);

checkAssociative(magicSquare, arraySize, magicNumber);

checkPanMagic(magicSquare, arraySize);

*/

// ^^^^ This is all commented out because I'm just checking to see that the file

// is read properly

fscanf(filePtr, "%d", &arraySize);

} /* end while-loop going through file */

fclose(filePtr);

} /* end else */

return 0;

} /* end main function */

Below is the data from the .txt file I was using:


3
8 1 6
3 5 7
4 9 2

4
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

5
8 17 1 15 24
11 25 9 18 2
19 3 12 21 10
22 6 20 4 13
5 14 23 7 16

5
1 15 24 8 17
23 7 16 5 14
20 4 13 22 6
12 21 10 19 3
9 18 2 11 25

9
47   58   69   80   1   12   23   34   45
57   68   79   9   11   22   33   44   46
67   78   8   10   21   32   43   54   56
77   7   18   20   31   42   53   55   66
6   17   19   30   41   52   63   65   76
16   27   29   40   51   62   64   75   5
26   28   39   50   61   72   74   4   15
36   38   49   60   71   73   3   14   25
37   48   59   70   81   2   13   24   35

4
16 21 10 18
19 9 24 13
22 15 17 11
8 20 14 23

5
1 15 24 8 17
23 7 16 5 14
20 13 4 22 6
12 21 10 19 11
9 18 2 25 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sorry, I am not able to reproduce your issue.

I believe one of the issue could be giving the incorrect file name or path. But when I give incorrect input I get the following error message.

With correct input I tried running your code and I got the following output. [Used Visual Studio]

[Please comment and let me know if you still face the issue.]

Add a comment
Know the answer?
Add Answer to:
I posted this question earlier but realized that the code was not easy to test since...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this is c code. please answer all questions on a piece of paper and show work....

    this is c code. please answer all questions on a piece of paper and show work. i need to prepare as i have a midterm i will have to be completing on paper 1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...

  • Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine...

    Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....

  • I don't understand how the outter loop decrementing i, setting j=i, and j incrementing. Why is...

    I don't understand how the outter loop decrementing i, setting j=i, and j incrementing. Why is this nested for loop and j=i necessary? What is happening here? void bubble_sort() { #define ARRAYSIZE 12 int array[ARRAYSIZE] = { 5, 7, 99, 12, 42, 31, 1, 23, 9, 21, 22, 45 }; for (int i = ARRAYSIZE - 2; i > 0; i--) { for (int j = i; j < ARRAYSIZE - 1; j++) { if (array[j] > array[j + 1])...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • please determine the output in a simple form #include <stdio.h> 2 3 //Item 02 4 int...

    please determine the output in a simple form #include <stdio.h> 2 3 //Item 02 4 int main() 5 6 7 8 9 10 int i for( i = 0 ; i < 5 ; i++ ) ifo 12 13 14 15 16 17 18 19 20 21 printf("A" printf("B"); if i > 2 ) printf("C"); 23 24 25 26 27 28 29 30 31 32 if( i 3) printf( "D") if>4) printf("E 34 35 36 37 38 39 40 41...

  • Modify the following code in order for it to perform the following operations: *This code is...

    Modify the following code in order for it to perform the following operations: *This code is meant to simulate Floyd's cycle finding algorithm* A) Start with two pointers at the head of the list. We'll call the first one tortoise and the second one hare B) Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic C) Advance tortoise by one...

  • In which line of the following code the first error is appeared (if any)? #include 2...

    In which line of the following code the first error is appeared (if any)? #include 2 int main() <stdio.h> 4 file p: 6 fopen("newname . txt", "rb"); *p if = (P NULL) 8 perror( "Error opening file") 9 else f while (fgetc(p)- eof) 12 13 if (feof(p)) 14 15 else 16 17 Fclose(p) 18 19 20 return e; 21 printf("%d\n", n); puts("End-of-File was not reached.) 23 O 10 0 17

  • write the jave code for the following 1 2 7 B9 4 5 10 11 12...

    write the jave code for the following 1 2 7 B9 4 5 10 11 12 15 13 16 23 24 14 22 17 25 26 18 19 21 20 INRerscrorTro zo Question 11 15 points Save Answer Suppose we have the following code in a Java program: int numbers- (23, 2, 32, 13, 43, 67, 102, 19, 5, 88) System.out.printin ("Please enter a number: ") Scanner console - new Scanner (System.in): int numEntered - console.nextInt (): Add additional lines...

  • 11- What is the test statistic for testing the overall linear (the test outlined in question...

    11- What is the test statistic for testing the overall linear (the test outlined in question 10)? Select one: a. 1.86×10−11 b. 22 c. 0.35 d. 7.64 e. -0.21 Create a pivot table to display how does the type of hitter and years of experience influence the number of major home runs. What is the average number of major home runs for power hitters that have 2 or less years of experience (0, 1 or 2 years of experience)? (hint:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT