C code crashing question.

This is a discussion on C code crashing question. within the C Programming forums, part of the General Programming Boards category; goal of this code is to type numbers and arrange them by order. if i put number 2 it works ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    C code crashing question.

    goal of this code is to type numbers and arrange them by order.
    if i put number 2 it works fine but more than 2, dos just crashes.
    I used bcc32 compiler.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    float order(int i, float * item);
    
    void main ()
    {
      float item[200];
      int i=0,j,p;
      
      do 
      {
    	printf("item #%d: ",i+1); p = scanf("%f",&item[i]);
    	i++;
      } while ( p != EOF && i<200);
      order (i,&item);
      
    }
    float order (int i, float * item)
    {
      float * order;
      int t,k;
    	order= malloc(i*sizeof(float));
     for (t= 0; t<i; t++)
    	order[t]=0;
    	printf("%d",i);
    	printf("%d",sizeof(*order));
     system("pause");
     for (k = 0; k<i; k++)
    	 if ( order[0]<= item[k] )
    	order[0]=item[k];
    	printf("%f",order[0]);
      system("pause");
     for ( t=1; t<i; t++)
     for (k = 0; k<i; k++)
     if ( order[t]<= item[k] && order[t-1] > item[k])
    	order[t]=item[k];
     return 0;
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    order (i,&item);
    Should just be:
    Code:
    order( i, item );
    This:
    Code:
     float * order;
    Should probably be renamed. You generally don't want to name functions and variables the same thing. You should also be calling free when you're done, since you're allocating memory.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    If you need to put things in order, implement a sorting algorithm, like these.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    3

    awwwwwwwww

    thanks for reply but still crashing even after i changed emmmmmmmmmmmmm (((

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What sorting algorithm are you using? Why do you need to allocate memory??

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    When you get a crash, your first order of business should be to run your program through a debugger. That should tell you where the crash happened (and perhaps why, especially with Valgrind). Even if your debugger can't tell you why the crash happened, it can at least give you a good starting point.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    3
    i don't know, it is a homework from some beginning C program class so I could make up any algorithm as long as i reach the goal.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got pauses and output there, so where exactly is it crashing? Put in some more printf statements listing your current variables' values if you need to, if you don't want to, can't, or don't know how to use a debugger.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question, trying to understand this code...
    By matthayzon89 in forum C Programming
    Replies: 11
    Last Post: 03-23-2010, 02:04 PM
  2. Question related to returning to earlier parts of the code.
    By Cgrasshopper in forum C Programming
    Replies: 2
    Last Post: 03-13-2010, 11:00 PM
  3. A question about c++ code and what to use
    By hunterx1x in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2010, 03:04 PM
  4. Question on some 'borrowed' code
    By cdalten in forum C Programming
    Replies: 3
    Last Post: 02-27-2006, 08:47 AM
  5. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 11:35 AM

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