Thread: Error while running

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Unhappy Error while running

    Hello, my program crashes when I try to run it. What could possibly be wrong?

    Here's the code:

    #include <iostream.h>
    int main()
    {

    int x, y, x1, y1; // Variáveis


    // Apresentação
    cout<<"Programa que calcula matrizes\n\n";
    cout<<"Quantas linhas tem a matriz que quer visualizar? ";
    cin>>x;
    cout<<"Quantas colunas tem a matriz que quer visualizar? ";
    cin>>y;

    // Cria uma matriz X por Y
    int matriz['x']['y'];


    // Completa a matriz
    x1=0;
    while (x!=x1)
    {
    x1++;
    y1=0;
    while (y!=y1)
    {
    y1++;
    cout<<"\nQuanto vale o termo da linha "<<x1<<" e coluna "<<y1<<"? ";
    cin>>matriz['x1']['y1'];
    }
    }



    return 0; // Finaliza o programa
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    First off, in this line:
    Code:
    // Cria uma matriz X por Y 
    int matriz['x']['y'];
    I assume you are trying to declare a two dimension array with the dimensions as inputted from the user. But actually, it's allocating a fixed array of size: [120][121]. Because 'x' yields ascii value of x, which is 120.

    Basically: 'x' does NOT mean value contained in variable x.

    As an aside, a lot of us here only know English (like me). So if your comments are in English, it helps us. And, you can put the code in between code tags. End code with: [/code] and start it with: [code].
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    14
    cin>>matriz['x1']['y1'];
    Removing those apostrophe like things stopped the program from crashing but whether that is allowed, I do not know.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    3
    I assume you are trying to declare a two dimension array with the dimensions as inputted from the user
    Well, that is what I am trying to do... how can I create this kind of array?

    Here is the code:
    Code:
    #include <iostream.h> 
    int main() 
    { 
    
    int x, y, x1, y1; 
    
    
    //Asks how many columns and rows the (two dimensioned) array should have
    cout<<"Programa que calcula matrizes\n\n"; 
    cout<<"Quantas linhas tem a matriz que quer visualizar? "; 
    cin>>x; 
    cout<<"Quantas colunas tem a matriz que quer visualizar? "; 
    cin>>y; 
    
    // Creates a two dimensioned X by Y array
    int matriz['x']['y']; 
    
    
    // Fills the array with values given by the user
    x1=0; 
    while (x!=x1) 
    { 
    x1++; 
    y1=0; 
    while (y!=y1) 
    { 
    y1++; 
    cout<<"\nQuanto vale o termo da linha "<<x1<<" e coluna "<<y1<<"? "; 
    cin>>matriz['x1']['y1']; 
    } 
    } 
    
    
    
    return 0; 
    }
    And thanks for the help !

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    6
    You need to dynamically allocate memory for a multi-dimensional array which can be tricky. I think the following is the correct way to do it but I'm not too good with that sort of thing.Also arrays start at 0 (int a[2] makes a[0] and a[1]) not 1 like your program used.

    Code:
    #include <iostream.h> 
    int main() 
    { 
    
    int x, y, x1, y1; 
    
    
    //Asks how many columns and rows the (two dimensioned) array should have
    cout<<"Programa que calcula matrizes\n\n"; 
    cout<<"Quantas linhas tem a matriz que quer visualizar? "; 
    cin>>x; 
    cout<<"Quantas colunas tem a matriz que quer visualizar? "; 
    cin>>y; 
    
    // Creates a two dimensioned X by Y array -changed
    int **matriz = new int*[y];
    for(int t = 0;t<x;t++)
    {
    	matriz[t] = new int[x];
    }
    
    // Fills the array with values given by the user
    x1=0; 
    while (x!=x1) 
    { 
    x1++; 
    y1=0; 
    while (y!=y1) 
    { 
    y1++; 
    cout<<"\nQuanto vale o termo da linha "<<x1<<" e coluna "<<y1<<"? ";
    cin>>matriz[y1-1][x1-1]; 
    } 
    }
    
       // delete all allocated memory -added
       for(int t = 0; t < x ; t++) 
          delete[] matriz[t]; 
       delete[] matriz; 
    
    return 0; 
    }

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Smile

    Thank you, now I see what was wrong in my program!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  3. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  4. Replies: 2
    Last Post: 05-12-2006, 10:28 AM
  5. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM