Thread: Run-time error

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Run-time error

    I want to write a program to remove the first smallest number that i input..

    input 6 integer from keyboard, they are 12 15 2 16 2 4

    how can i amend the program that can remove the first '2' and shift ithe first '2' 's right hand side number to left, thanks.

    Output is : 12 15 16 2 4
    Code:
    void RemoveFirstSmallest(int A[], int n) 
    //A is an array containing n integers
    {
    	int i;
    	int min_value = A[0];
    	int id_min = 0;
    
    	for (i=1; i<n;i++)
    	{
    		if (A[i] < min_value) 
    		{
    			min_value = A[i];
    			id_min = i;
    		}
    	}
    		for (i=id_min;i<n-1;i++)
    		{
    			A[i] = A[i+1];
    		}
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That code is not the source of your run-time error. The first loop should run from 0 to n instead of 1 to n, but otherwise that code is correct. Maybe the problem is in your input method?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    if i input the number '2' twice...how can the program to remove the first '2' that i input??Thanks

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    and also the last number in A[] doesnt get left shifted

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Are you havng a problem with runtime error or your just having a problem making your program work?

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    19
    Apart from the things mentioned above, the code seems ok.

    when 12 15 2 16 2 4 is input.

    At the third position (i=2, for 0 to <n) id_min becomes =2.

    Now when we encounter the second time, we have a close that a[i] < minvalue, which will not be true. So id_min will remain to be 2 (correct value). So we just shift after this.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by itsme86
    That code is not the source of your run-time error. The first loop should run from 0 to n instead of 1 to n, but otherwise that code is correct. Maybe the problem is in your input method?
    1 to n is fine because they used the 0 value as the inital min value.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by loko
    and also the last number in A[] doesnt get left shifted
    It appears that it does get shifted, however it also remains in the last spot. So you'll need to do something to ignore it later.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Thantos
    1 to n is fine because they used the 0 value as the inital min value.
    Good point.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Thantos
    It appears that it does get shifted, however it also remains in the last spot. So you'll need to do something to ignore it later.
    Thats what i meant

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM