How to fill a 2d array from a file?

This is a discussion on How to fill a 2d array from a file? within the C++ Programming forums, part of the General Programming Boards category; Im a newbie and need to know how to fill a 2d array from a file of integers. This code ...

  1. #1
    Unregistered
    Guest

    How to fill a 2d array from a file?

    Im a newbie and need to know how to fill a 2d array from a file of integers. This code doesn't seem to work:

    #include<iostream.h>
    #include<fstream.h>
    #define in_file "array.dat"

    int a[5][5];

    int main()
    for(int i=0;i<5;i++)
    for (int j=0;j<5;j++)
    ins >> a;

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ins >> a[i][j];

  3. #3
    Unregistered
    Guest

    Question

    Thanks it worked. But the problem is now that I can read the array in , I have to make a program to check if it is symmetric, upper triangle or diagonal.

    Im reading this from the array.dat file:
    0 1 2 3 4
    1 5 6 7 11
    2 6 8 9 12
    3 7 9 10 13
    4 11 12 13 14

    When i run the program it should output symmetric is 1(true) but it doesn't work. Here's my code.. any ideas?

    //3 Void functions to check whether a 2 dimensional array falls under the special classes
    // of symmetric, upper triangle, or diagonal.
    #include<iostream.h>
    #include<fstream.h>
    #define in_file "array.dat"

    void checkuppertriangle(int array[5][5], int& result);
    void checksymmetric(int array[5][5], int& result);
    void checkdiagonal(int array[5][5], int& result);

    int a[5][5];
    int main()
    {
    ifstream ins;
    ins.open(in_file);

    for(int i=0;i<5;i++) //sets the loop to fill the array from a file
    for (int j=0;j<5;j++)
    ins >> a[i][j];

    int triangle,symmetric,diagonal; //variables to output results
    checkuppertriangle(a,triangle); //call function
    checksymmetric(a,symmetric); //call function
    checkdiagonal(a,diagonal); //call function
    cout << "Results"
    << "Upper Triangle " << triangle << endl
    << "Symmetric " << symmetric << endl
    << "Diagonal " << diagonal << endl;

    int wait;
    cin >> wait;
    return 0;
    }

    void checkuppertriangle(int array[5][5], int& result)
    {
    for(int i=0;i<5;i++) //sets the loop to go through each cell in array
    for (int j=0;j<5;j++)
    {
    if (i > j)
    {
    result = 0; //passing back the result using a reference
    return; //result set to 0 if not symmetric return to
    }
    else if ((i < j) && (a[i,j]==0))
    {
    result=1; //set result to 1, continue to check. If all are
    }
    }
    return;
    }

    void checksymmetric(int array[5][5], int& result)
    {
    for(int i=0;i<5;i++) //sets the loop to go through each cell in array
    for (int j=0;j<5;j++)
    {
    if ((a[i,j])!=(a[j,i]))
    {
    result = 0; //passing back the result using a reference
    return; //result set to 0 if not symmetric return to
    }
    else if ((a[i,j])==(a[j,i]))
    {
    result=1; //set result to 1, continue to check. If all are
    }
    }
    return;
    }

    void checkdiagonal(int array[5][5], int& result)
    {
    for(int i=0;i<5;i++) //sets the loop to go through each cell in array
    for (int j=0;j<5;j++)
    {
    if ((a[i,j]!=0) || (i>j))
    {
    result = 0; //passing back the result using a reference
    return; //result set to 0 if not diagonal return to main.
    }
    else if ((a[i,j]==0)&& (i<j))
    {
    result=1; //set result to 1, continue to check. If all are
    }
    }
    return;
    }

  4. #4
    Unregistered
    Guest

    Smile

    Ok i seemed to figure out the problem. Accessing arrays as
    "a[i,j]" was wrong it should be "a[i][j]..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. file into 2d array
    By lakai02 in forum C Programming
    Replies: 0
    Last Post: 12-22-2002, 04:02 PM

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