selfstudy: Remove all smallest integers

This is a discussion on selfstudy: Remove all smallest integers within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define MAX_ARRAY_SIZE 100 int RemoveAllSmallest(int A[],int n) { int i; int id_smallest=0; int min_value=A[0]; for (i=1;i<n;i++) { ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    selfstudy: Remove all smallest integers

    Code:
    #include <stdio.h>
    
    #define MAX_ARRAY_SIZE 100
    
    int RemoveAllSmallest(int A[],int n) 
    {
    	int i;
    	int id_smallest=0;
    	int min_value=A[0];
    
    		for (i=1;i<n;i++)
    		{
    			if (A[i]<min_value)
    			{
    				min_value=A[i];
    				id_smallest=i;
    			}
    		}
    
      
    		for (i=id_smallest;i<n-1;i++)
    			{
    				A[i]=A[i+1];
    			}
    	
    }
    
    
    void main()
    {	int i;
    	
    	int A[MAX_ARRAY_SIZE];
    	int n;
    	
    	//Ask and read the number of integers.
    	printf("How many integers totally? (1-%d)",MAX_ARRAY_SIZE);
    	scanf("%d",&n);
    	printf("\nInput the integers.\n");
    	printf("(Each integers should be in the range 0-99. ");
    	printf("Seperate them with spaces.)\n");
    	
    	//Read all integers 
    	for (i=0;i<n;i++)
    		scanf("%d",&(A[i]));
    
    	//Remove all smallest numbers
    	n=RemoveAllSmallest(A,n);
    		
    	//Display result
    	printf("\nAfter removing all smallest number(s):\r\n");
    	for (i=0;i<n;i++)
    		printf("%d ",A[i]);
    }
    ------------------------------------------

    I have tried the above, but it can not remove all the samllest integer, but only one. Can anyone help me to approch it?
    Last edited by Salem; 09-14-2006 at 12:56 AM. Reason: code tagged - please learn to use them

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Layout ten M&Ms in front of you in a row. Start removing all the yellow ones and sliding the others over to fill in the empty spaces. Each time you remove another yellow one, notice how the sliding you are doing changes. Do the same thing with your array.

    Also, don't forget to return a value from your function. What value you return will also help you figure out when to stop the deletion loop.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    First: code tags are your friend.

    Second: This is the C++ forum; your program is C. Either you should convert it to C++ (which in fact would make this far, far easier to solve -- using a std::vector in place of the array makes the logic for removal trivial) or ask the mods to move the post to the correct forum.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Code:
    void main()
    I thought many compilers would flag this as an error nowdays. Void main has long since gone.
    I use
    Code:
    int main ( void )
    . I think void main is non standard, but i could be wrong.
    Perhaps C compilers like Miriacle C still accept it, but many standard C++ compilers wouid give at least a warning

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Miracle C isn't a C compiler.
    It's a student assignment pretending to be a compiler, but since it's $$$ nagware, and there are many free (and fully featured) compilers available, it makes for a very poor choice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 06:44 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 09:58 PM
  4. Finding the smallest value......HARD!!!
    By AssistMe in forum C Programming
    Replies: 21
    Last Post: 03-10-2005, 06:23 AM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

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