Trouble with 2d array in program.

This is a discussion on Trouble with 2d array in program. within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm working on a homework assignment and I am utterly stuck. I'm not quite sure where the problem ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Trouble with 2d array in program.

    Hi there,
    I'm working on a homework assignment and I am utterly stuck. I'm not quite sure where the problem in my program lies and was hoping someone could point me in the right direction.
    The assignment is relatively simple; find groups of X's in a text file (in this program "group.txt")and output the number of groupings.
    Here's the basic design:
    1. Declare a 2d char array
    2. Clear the array out (i.e. set all char to spaces).
    3. Read in the array to the file (in this case "group.txt").
    5. Call a recursive function to find the X groupings.
    (The program should find 6 groupings. My program finds 20).
    arraysize_row and arraysize_col are set in the assingnment; I can't change them. There are actually only 20 rows and 70 columns needed but the extra 2 rows and columns act as a border around the array so my program doesn't try to access things outside the array.

    For some reason my groupings are being miscounted. I'm not sure if I'm reading the array incorrectly or if it is a problem somewhere else.

    I've posted some of my code (only a portion of my recursive program).

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    const int arraysize_row=22; 
    const int arraysize_col=72;
    void displaythearray(char ma[][arraysize_col]);
    void cleararray(char ma[][arraysize_col], int , int);
    void readinfile(char ma[][arraysize_col]);
    void groupfinder(char ma[][arraysize_col]);
    
    
    void getgroup(char ma[][arraysize_col],int r, int c);
    
    
    int main (void)
    {
       char matrix[arraysize_row][arraysize_col];
       
      checkfile();
      cleararray(matrix, arraysize_row, arraysize_col);
       readinfile(matrix);
       displaythearray(matrix);
      groupfinder(matrix);
    system("pause");
    return 0;
    }
    void cleararray(char matrix[][arraysize_col], int r, int c)
    {
    int row=0, col=0;
    for (row=0; row <r; row++)
    for (col=0; col <c; col++)
       matrix[row][col]=' ';
     
     
    }
    
    
    void readinfile(char ma[arraysize_row][arraysize_col])
    {
    ifstream infile;
    infile.open("group.txt");
    char mychar=' ';
    infile.get(mychar);
    for (int r=1; r<arraysize_row-1;r++){
     for (int c=1; c<arraysize_col-1; c++){
    ma[r][c]=mychar;
     infile.get(mychar);
     }
    }
    
    
    infile.close();
     
    }
    
    
    void groupfinder(char ma[][arraysize_col])
    {
    int group=0;
    for (int row=1; row<(arraysize_row-1); row++)
    {
      for (int col=1; col< (arraysize_col-1); col++)
       {
           if (ma[row][col]=='X')
              {
                getgroup(ma, row, col);
                 group++;
              }
      }
    }
     
    cout<<"Number of groups: " <<group<<endl;
    }
    void getgroup(char ma[][arraysize_col], int r, int c)
    {
    ma[r][c]=' '; //set current location to space.
    
    
      if (ma[r-1][c-1]=='X' ) // upper left of x
    {
     getgroup(ma,r-1,c-1);
     
    }
    //this goes on to test other locations near X.
    // ...
    else
    return;
    
    
    }
    Any help is appreciated!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    First, your indentation needs work -> SourceForge.net: Indentation - cpwiki

    Second, where is displaythearray() and does it display the correct information?
    That should answer your "I'm not sure if I'm reading the array incorrectly or if it is a problem somewhere else."

    3, try it with a simpler groups.txt file (one you create). Start with zero groups, then one group and so on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble w/ 2d array
    By Zaz in forum C Programming
    Replies: 2
    Last Post: 11-19-2009, 05:42 AM
  2. Array trouble
    By thealmightyone in forum C Programming
    Replies: 3
    Last Post: 03-27-2009, 06:50 AM
  3. Array trouble
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2006, 12:55 PM
  4. array program trouble
    By chrisjenkins212 in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2006, 01:16 AM
  5. if array trouble...
    By g1i7ch in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2006, 11:14 AM

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