GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Sep-2006, 15:07
JKSung5295 JKSung5295 is offline
New Member
 
Join Date: Sep 2006
Posts: 13
JKSung5295 is on a distinguished road

Bus error (core dumped) Problem on my C++ program


i have written a program that is suppose to check if 9*9 sudoku puzzle is correct solution. I have to check each row, column, and 3*3 regions.

I have worked out how to check the row and the column but i'm having a hard time getting the checking region to work correctly. when i compile the program i have no error message but when i run the program i get the "Bus error". i just can't figure out what i'm doing wrong so i was wondering if anyone can help me out.


here is the code
CPP / C++ / C Code:



#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void open_input_file (ifstream &in_f);
void read_input (ifstream &in_f, int puzzle[][9], int size);
bool check_row (int puzzle[][9], int size);
bool check_column (int puzzle[][9], int size);
bool check_region (int puzzle[][9], int size);
void bubblesort (int list [], int size);
void bubbleup (int list [], int index, int last);


const int size = 9;

int main()
{
ifstream input;
int sudoku [size][size];
bool answer_row, answer_column, answer_region;

open_input_file (input);
read_input (input, sudoku, size);
answer_row = check_row (sudoku, size);
answer_column = check_column (sudoku, size);
answer_region = check_region (sudoku, size);

if (answer_row == true && answer_column == true && answer_region == true)
cout << "The solution to the sudoku is correct" <<endl;
else
cout << "The solution to the sodoku is incorrect" << endl;


return 0;
}


void open_input_file (ifstream &in_f)
{
char input_file_name[80];

do
{ in_f.clear();
	cout << "Enter input file name: ";
	cin >> input_file_name;
	in_f.open(input_file_name);
} while ( in_f.fail() );


return;
}


void read_input (ifstream &in_f, int puzzle[][9], int size)
{

int ch;

    for (int i = 0; i < 9; i++) 
    {
        for (int j = 0; j < 9; j++) 
	{
            in_f >> ch;
	    {
            if (isdigit(ch)); 
            puzzle[i][j] = ch;
	    }
        }
    }


return ;
}

bool check_row (int puzzle[][9], int size)
{
int row[size];

for (int i=0; i < 9; i++)
{
	for(int j=0; j < 9; j++)
	row[j] = puzzle[i][j];
bubblesort (row, size);

    for (int k=0; k < 9; k++)
    {
    if (row[k] != k+1)
    return false;
    }
}
return true;
}

bool check_column (int puzzle[][9], int size)
{
int column[size];

for (int i=0; i < 9; i++)
{
        for(int j=0; j < 9; j++)
        column[j] = puzzle[j][i];
bubblesort (column, size);

    for (int k=0; k < 9; k++)
    {
    if (column[k] != k+1)
    return false;
    }
}
return true;
}

bool check_region (int puzzle[][9], int size)
{
int region[size];
int a = 0;

for (int i=0; i < 9;  i=i+3)
{
    for (int j=0; j < 9; j=j+3)
    {
	for (int k=0; k < 3; k++)
	{
	    for (int l=0; l < 3; l++)
	    {
		region [a] = puzzle[k+i][l+j];
	    }
	}
    }
}

bubblesort (region, size);

    for (int k=0; k < 9; k++)
    {
    if (region[k] != k+1)
    return false;
    }

return true;
}


// SORTING FUNCTION
void bubblesort(int list [], int size)
{
        for(int index = 0; index < size; index++)
                bubbleup(list, index, size);
        return;
}

        // SECOND PART OF THE SORTING FUNCTION
        void bubbleup (int list [], int index, int last)
        {

                for (int walker = last; walker > index; walker--)
                        if (list [walker] < list [walker-1])
                        {
                        char temp = list [walker];
                        list [walker] = list [walker - 1];
                        list [walker -1] = temp;
                        }
        return;
        }


i have checked already eveything works except the check_region function
  #2  
Old 24-Sep-2006, 16:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,520
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Bus error (core dumped) Problem on my C++ program


Quote:
Originally Posted by JKSung5295
i i just can't figure out what i'm doing wrong so i was wondering if anyone can help me out

Here's how you can help yourself (It's faster, I claim, than posting a request for help and waiting for a helpful response. It will be even faster the next time --- and there will be a next time, I'm guessing.)

The error may be caused by your accessing memory out of the range of an array.

If you can't see how your code cause that, then:

You can put print statements in your program to see what the program is trying to do. Make the program tell you what's wrong. (Or let it verify that you are doing it right, which is entirely possible.)

For example, since you think that the problem is in check_region:

CPP / C++ / C Code:
bool check_region(int puzzle[][9], int size)
{
    int region[size];
    int a = 0;

    for (int i = 0; i < 9; i = i + 3) {
        for (int j = 0; j < 9; j = j + 3) {
            for (int k = 0; k < 3; k++) {
                for (int l = 0; l < 3; l++) {
                    cout << "l = " << l
                         << ", j = " << j
                         << ", k = " << k
                         << ", i = " << i
                         << ", a = " << a
                         << ", k+i = " << k+i
                         << ", l+j = " << l+j
                         << ", a = " << a
                         << endl;
                    region[a] = puzzle[k + i][l + j];
                }
            }
        }
    }
}

If it bombs there, and one or more subsubscripts is out of range of that array, you know why it bombed, and you are where you need to be to fix it. If it doesn't bomb there, then you know more than you knew before, and you can look elsewhere.

Regards,

Dave
 

Recent GIDBlogGoing to Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Pipeline freeze simulation darklightred CPP / C++ Forum 6 27-Jul-2006 19:37
Problem compiling a program, my first vector. george89 CPP / C++ Forum 2 26-Jun-2006 21:10
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
Problem with program breggo CPP / C++ Forum 3 08-Jun-2005 13:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.