Thread: Filling a 2d Array cause program to crash

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    Filling a 2d Array cause program to crash

    ok, im writing a code that will triangulize (sp?) a 3 system, 3 variable equation. however, upon inputing the final variable in the array (the sum in the 3rd equation) it says the program crashed and i will all unsaved data in it, blah blah blah. it happened here at school on windows 95, and at home on XP. here is the code, it compiles fine for me, but if you dont use dev-cpp i think you might have to change the system pause to getch or whatever you use. i also apologise for the lack of comments
    Code:
    #include <iostream>
    #include <string>
    
    void input();
    void calculate();
    void output();
    
    int data[4][5];
    int main()
    {
       input();
       calculate();
       output();
       system("Pause");
       return 0;
    }
    
    void input()
    {
       for (int a = 0; a < 3; a++)
       {
          for (int b = 0; b < 4; b++)
          {
             switch (b)
             {
                case 0:
                cout << "\nEnter the X in the " << a + 1 << " equation.\n";
                break;
                case 1:
                cout << "\nEnter the Y in the " << a + 1 << " equation.\n";
                break;
                case 2:
                cout << "\nEnter the Z in the " << a + 1 << " equation.\n";
                break;
                case 3:
                cout << "\nEnter the sum of the " << a + 1 << " equation.\n";
                break;
                default:
                cout <<"\n\nERROR: Line 39\n\n";
             }
             cin >> data[a][b];
          }
       }
    }
    
    void calculate()
    {
       int temp = data[0][0] / data[1][0];
       for (int a = 0; a < 4; a++)
       {
          data[1][a] *= temp;
          data[1][a] = data[0][a] - data[1][a];
       }
       temp = data[0][0] / data[2][0];
       for (int a = 0; a < 4; a++)
       {
          data[2][a] *= temp;
          data[2][a] = data[0][a] - data[2][a];
       }
       temp = data[1][1] / data[2][1];
       for (int a = 0; a < 4; a++)
       {
          data[2][a] *= temp;
          data[2][a] = data[1][a] - data[2][a];
       }
       temp = data[2][2] / data[2][3];
       data[1][2] *= temp;
       data[0][2] *= temp;
       temp = (data[1][3] - data[1][2]) / data[1][1];
       data[0][1] *= temp;
       temp = (data[0][3] - (data[0][1] + data[0][2])) / data[0][0];
       data[0][0] *= temp;
    }
    
    void output()
    {
       for (int a = 0; a < 3; a++)
       {
          for (int b = 0; b < 4; b++)
             cout << data[a][b] << "  ";
          cout << "\n";
       }
    }
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    After you input the final data?
    Try to isolate the problem. First comment out calculate() in main. Perhaps you get a division by 0? Did you input any 0's in data?
    Code:
    int temp = data[0][0] / data[1][0];
    Another thing, in your definition of data you define it as int[4][5] while int[3][4] would be enough.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    oh, i just figured out the problem as i was writing this. i declared temp as an int, when i need it to be a float or double, because i was often having times when it would be a fraction. then a lot of times that fraction was less than .5, so it rounded temp to 0, and this caused many of the values to be set to 0. which would cause a divide by zero error, now i can fix it. thx for the help
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM