Reading a matrix (txt) to a matrix (c)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Reading a matrix (txt) to a matrix (c)

  1. #1
    Join Date
    Jun 2004
    Posts
    6

    Reading a matrix (txt) to a matrix (c)

    Last week i put on air my very very simple doubt (i think it should be that way!!!).
    So, i'm trying to copy a matrix of integers (0 and 1s) in text format (matrix.txt) to a matrix (bidimensional array on C++).
    Can anyone give me some suggestions please


    regards

  2. #2
    Join Date
    Apr 2004
    Posts
    100
    from a matrix integers to a matrix of what?
    do you want to dump the matrix into a file in binary format??

    not sure what you are trying to achieve here.

    if you just want to read the integers from the txt file into a C++ program, it should be easy.

  3. #3
    Join Date
    Jun 2004
    Posts
    6
    I would like to create a matrix in C++ with data from the text matrix. Something with the type matrix[i][j].

    thanx

  4. #4
    Join Date
    Apr 2004
    Posts
    100
    if this is the case, then let us assume that your text file contains only those numbers, and they are separated by space. so it would look something like

    1 2 3 4 4
    2 3 4 5 5
    5 5 5 5 5


    the code to read it is

    Code:
    int x[3][5];
    ifstream infile;
    infile.open("mymatrix.txt");
    
    for (int i = 0; i < 3; i++) {
       for (int j = 0; j < 5; j++) {
          infile>>x[i][j];
       }
    }

  5. #5
    Join Date
    Jun 2004
    Posts
    6
    I followed your suggestion and my programme comes like this:

    (see attach prog.doc)

    However as you can see some problems have occured.

    Í must tell that i'm using Microsoft Visual C++.
    Can it be that those errors come from the libraries i'm using? Should i open some others?

    Many thanx again.
    Attached Files Attached Files
    Diogo André Alagador
    MSc. Applied Math on Biological Sciences
    Lisbon, Portugal

  6. #6
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126
    I'm not a big fan of mixing C I/O functions (scanf, printf) and C++ I/O functions (iostream, fstream) ... but that is personal preference.

    Other than that, remove the space between the # and the include. It should be #include, not # include.
    gorshing
    newb

  7. #7
    Join Date
    Apr 2004
    Posts
    100
    if you are using Visual C++ .NET, you also need to include

    using namespace std;

    right after all your includes.

    another problem is that your input to the program are not used. they are supposed to indicate the rows and columns of the matrix, however, in your for loops, you have hard-coded the size to be 3x5. you need to replace them with lines and cols.

  8. #8
    Join Date
    Apr 2004
    Posts
    100
    one more thing. your code will still not compile, because you were attempting to do a dynamical array, sort of, but in an incorrect way.

    in C++, you cannot let the user input size of the array and then create it on the fly. you can only use constants like

    int matrix[3][5];

    or

    #define ROW 3
    #define COL 5

    int matrix[ROW][COL];

    but you cannot use variables.

    if you want to create dynamic array, you must use pointers which is a little more complicated.

  9. #9
    Join Date
    Jun 2004
    Posts
    6
    First i have to thanks emanresu and gorshing for your suggestions.
    Following some desperation as i think every day i understand less of C i have tried other way and, for the some kind of aim i built the following progr (with dynamic arrays). Once again probs occorred. What might be the errors?

    #include <stdio.h>

    void main()
    {
    int col, row, i, j;
    int **arr;
    FILE *fin;
    fin=fopen("test.txt","r");
    printf("number of cols:\n");
    scanf("%d\n\n",&col);
    printf("number of rows:\n");
    scanf("%d\n\n",&row);
    arr=new int*[row];
    for(i=0;i<row;i++)
    {
    arr[i]=new int[col];
    for(j=0;j<col,j+)
    {
    fscanf(fin,"%d \n,&arr[i][j]);
    }
    }
    fclose(fin);
    printf("%d \n",arr[1][1]);
    }


    Compiling...
    test.c
    C: \MyProjects\test\test.c(13) : error C2065: 'new' : undeclared identifier
    C:\ MyProjects\test\test.c(13) : warning C4047: '=' : 'int ** ' differs in levels of indirection from 'int '
    C:\ MyProjects\test\test.c(13) : error C2143: syntax error : missing ';' before 'type'
    C:\ MyProjects\test\test.c(16) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int '
    C:\MyProjects\test\test.c(16) : error C2143: syntax error : missing ';' before 'type'
    C:\ MyProjects\test\test.c(17) : error C2059: syntax error : ')'
    C:\ MyProjects\test\test.c(19) : error C2001: newline in constant
    C:\ MyProjects\test\test.c(25) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    test.exe - 6 error(s), 2 warning(s)
    Diogo André Alagador
    MSc. Applied Math on Biological Sciences
    Lisbon, Portugal

  10. #10
    Join Date
    Apr 2004
    Posts
    100
    new is what you use in C++ to define dynamic arrays, but not in C.

    in C++, you must use malloc() + sizeof()... so it will be like this


    arr = malloc(sizeof(int*) * row );

    for (int i=0; i <row; i++) {

    arr[i] = (malloc(sizeof(int) * col);
    }

  11. #11
    Join Date
    Jun 2004
    Posts
    6
    i have to thank u emanresu again by your encouraging explanations.
    They really worked!!!!!!!!!!!

    I'm afraid a day ahead i should come back with pretty more silly doubts.

    best regards
    Diogo André Alagador
    MSc. Applied Math on Biological Sciences
    Lisbon, Portugal

  12. #12
    Join Date
    Apr 2004
    Posts
    100
    no problem, codeblue. anytime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center