Program crashes any ideas

This is a discussion on Program crashes any ideas within the C Programming forums, part of the General Programming Boards category; I have problem when i tiping numer larger then 4. Program crashes and I don't know what is the problem. ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Program crashes any ideas

    I have problem when i tiping numer larger then 4. Program crashes and I don't know what is the problem. Program do smth like:
    1

    1 1
    1 1

    1 1 1
    1 1 1
    1 1 1

    while matrix will be lenght x lenght size.

    here is code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    
    //create piramide
    void pyramid( int length)
    {
    	int j = 1, x=1, y=1, i, k=length, l=1, a[x][y];
        while ( k > 0 ){
              for ( x = 0; x < l; x++)
                  for ( y = 0; y <l; y++)
                  a[x][y]=rand()%200;
                  for ( y = 0; y <l; y++){
                      for ( x = 0; x <l; x++)
                      printf ( "%d ", a[x][y]);
                      printf (" \n");
                      }
              k--;
              l++;
              x=1;
              y=1;
              printf (" \n");
              }
    }
    
    
    //main
    int main( )
    {
    	int length;
    
    	printf( "write piramide lenght:" );
    	scanf( "%d", &length );
        pyramid (length);
      	getch( );
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    looking at the output you are providing in your query i am wondering how can u get the answer by the code which you wrote

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Nice indentation

    Anyway, your array has room only for one number (a[0][0]) and you are trying to store more values in it.

    Also nice use of l for variable name: looping from 0 to 1 would be OK, but not to l if it is more than 1.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    And i don't think so that the program will crash it will print the junk value not the expected one

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Quote Originally Posted by anon View Post
    Nice indentation

    Anyway, your array has room only for one number (a[0][0]) and you are trying to store more values in it.

    Also nice use of l for variable name: looping from 0 to 1 would be OK, but not to l if it is more than 1.
    so what you offer to me?

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377

    Post

    Quote Originally Posted by anon View Post
    Nice indentation

    Anyway, your array has room only for one number (a[0][0]) and you are trying to store more values in it.

    Also nice use of l for variable name: looping from 0 to 1 would be OK, but not to l if it is more than 1.
    I don't think that in the program:--

    Code:
       a[x][y]=rand()%200;
    in the above line the value of x and y will be any thing other than 1

    he is correct at his logic in that point no overflow of value or out of index in case of 2 D int array called a

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by auryc View Post
    I have problem when i tiping numer larger then 4. Program crashes and I don't know what is the problem. Program do smth like:
    1

    1 1
    1 1

    1 1 1
    1 1 1
    1 1 1

    while matrix will be lenght x lenght size.

    here is code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    
    //create piramide
    void pyramid( int length)
    {
    	int j = 1, x=1, y=1, i, k=length, l=1, a[x][y];
        while ( k > 0 ){
              for ( x = 0; x < l; x++)
                  for ( y = 0; y <l; y++)
                  a[x][y]=rand()%200;
                  for ( y = 0; y <l; y++){
                      for ( x = 0; x <l; x++)
                      printf ( "%d ", a[x][y]);
                      printf (" \n");
                      }
              k--;
              l++;
              x=1;
              y=1;
              printf (" \n");
              }
    }
    
    
    //main
    int main( )
    {
    	int length;
    
    	printf( "write piramide lenght:" );
    	scanf( "%d", &length );
        pyramid (length);
      	getch( );
    	return 0;
    }
    Change the size of the 2D array. For eg.
    Code:
    int a[10][10];
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Doesnt crash in debug mode, crashes normally
    By chubigans in forum C Programming
    Replies: 8
    Last Post: 11-10-2009, 10:53 PM
  2. program crashes + fscanf() question
    By happyclown in forum C Programming
    Replies: 27
    Last Post: 01-16-2009, 02:51 PM
  3. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 09:27 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  5. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 09:27 PM

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