Thread: trying to read from file of integers into 2D array

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    45

    trying to read from file of integers into 2D array

    hello again ..
    so i have this text file of integers :
    Code:
    3
    1
    -1
    4
    -9
    2
    2
    4
    2
    3
    6
    2
    21
    


    and i want to read from that text file into 2d array , here is my code :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MaxRows 5
    #define MaxCols 5
    
    
    
      
    int main(void)
    {
       int AugMatrix[MaxRows][MaxCols] ; // augmentation matrix
       int i,j;
       FILE *IntFile;
       int arr[7][7];
       
       
        IntFile= fopen("numbers.txt","r"); // read from file of integers
       
      
          if (IntFile == NULL) 
               {   
                  printf("Error! Could not open file\n"); 
                  exit(-1); // must include stdlib.h
                  }
                   
          printf("The Array: \n\n");
          
          for ( i=0; i<7; i++) // load matrix
          {
             printf("\t %d",AugMatrix[i]);
             for(j=0 ;j<7 ;j++)
             {
                
                printf("\n %d",AugMatrix[j]);
             }
                fclose(IntFile);
                printf("\n");
             }
    
      return 0;
      } // end of main
      


    ps: i'm using JGrasp to compile and run this code
    i want my output to look like this

    The array :

    1 -1 4 -9
    2 2 4 2
    3 6 2 21

    any help would be appreciated as i have so much on this project i want to make sure it works at the beginning.

    thank you !



  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    We can definitely!

    1. What output are you receiving when you run this program?
    2. Why have you defined constant values MaxCols and MaxRows but never use them?
    2a. Are those the max number of columns and rows for the output or the 2d array?
    3. Is the first number the number of columns and rows in the output matrix?

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by ButtahNBred View Post
    We can definitely!

    1. What output are you receiving when you run this program?

    the output i'm recieveing is this:
    Code:
    The Array: 
    ÏϧÏ
    ÏÏ§Ï         1420516048
    ÏÏ§Ï 1420516048
    ÏÏ§Ï 1420516068
    ÏÏ§Ï 1420516088
    ÏÏ§Ï 1420516108
    ÏÏ§Ï 1420516128
    ÏÏ§Ï 1420516148
    ÏÏ§Ï 1420516168
    ÏÏ§Ï         1420516068
    ÏÏ§Ï 1420516048
    ÏÏ§Ï 1420516068
    ÏÏ§Ï 1420516088
    
    ÏÏ§Ï 1420516108
    
    2. Why have you defined constant values MaxCols and MaxRows but never use them?
    actually , i used them in the beginning in the For loops instead of 7 , but then i changed my mind

    2a. Are those the max number of columns and rows for the output or the 2d array?

    yes they should be the max rows and cols for the 2D array in output

    3. Is the first number the number of columns and rows in the output matrix?
    well, this is complicated .. the first number in the file which is 3 should indicate how many equations i have
    so 3 will be 3 equations and (n*(n+1)) integers are the contents of the array which is 12 integers in my case

    Thank you

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Your 2d array is not represented properly in the nested loop. You need to use both indexes, not one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Prog. Read .txt file with strings and integers
    By akosinoah in forum C Programming
    Replies: 7
    Last Post: 11-16-2013, 12:30 PM
  2. read txt file with strings and integers
    By akosinoah in forum C Programming
    Replies: 4
    Last Post: 11-16-2013, 09:08 AM
  3. read unknown integers from text file
    By underlink in forum C Programming
    Replies: 2
    Last Post: 11-10-2009, 10:23 AM
  4. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 01:36 AM
  5. how to read integers from a file( novice )
    By Gorgorath in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2002, 04:25 AM