C programming problem - Arrays

This is a discussion on C programming problem - Arrays within the C Programming forums, part of the General Programming Boards category; Hi again, I have to create a program that should replace the smallest number in a 1-d array, on each ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    C programming problem - Arrays

    Hi again,
    I have to create a program that should replace the smallest number in a 1-d array, on each turn
    so 5 elements
    1
    2
    3
    4
    5
    it should be 9999 2 3 4 5
    newline
    9999 9999 3 4 5
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define ROW 5
    main()
    {
        int unsorted[ROW] ;
        int i ;
        int j  ;
        int smallest[ROW] ;
    
        int temp ;
        
        printf("Enter %d numbers\n", ROW);
        for(i=0;i<ROW; i++) 
        {
            scanf("%d", &unsorted[i]) ;
        }
          for(i=0;i<ROW; i++) 
            {
                smallest[i] = unsorted[i] ;
            }
      
        
      
        for(i=0;i<5;i++)
        {
           if(unsorted[i] < smallest[i+1]) 
           {
                
                for(i=0;i<ROW; i++) 
                {
                    temp = 9999 ;
                    unsorted[i] = temp ;
                
                   
                    for(i=0; i<ROW; i++)
                    {
                        printf("%d\t", unsorted[i]) ;
                    }
                   
                }
                
               
            }
        }
           
          
           
      
       
                
            
    
    
        getch() ;
    }
    Please help...it doesnt finish the loop!!

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define ROW 5
    main()
    {
        int unsorted[ROW] ;
        int i ;
        int j  ;
    
        printf("Enter %d numbers\n", ROW);
        for(i=0;i<ROW; i++) 
        {
            scanf("%d", &unsorted[i]) ;
        }
    
      
        for(i=0;i<ROW;i++)
        {
           if(unsorted[i] <= unsorted[i+1])
           {
               unsorted[i] = 999 ;
               
               
               
               for(i=0;i<ROW;i++)
                {
                    
                    printf("%d\t", unsorted[i]) ;  
            
                }
                
    
            }
            
    
        }
        
        printf("\n") ;
        
         for(i=0;i<ROW;i++)
            {
               if(unsorted[i] <= unsorted[i+1])
               {
                   unsorted[i] = 999 ;
                   
                   
                   
                   for(i=0;i<ROW;i++)
                    {
                        
                        printf("%d\t", unsorted[i]) ;  
                
                    }
                    
        
                }
                
        
            }
           
            for(i=0;i<ROW;i++)
            {
               if(unsorted[i] <= unsorted[i+1])
               {
                   unsorted[i] = 999 ;
                   
                   
                   
                   for(i=0;i<ROW;i++)
                    {
                        
                        printf("%d\t", unsorted[i]) ;  
                
                    }
                    
        
                }
                
        
            }
       
                
            
    
    
        getch() ;
    }
    Ok i fixed the problem but i need about 3 loops!! why can't i use just one loop?

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    You should be able to (I didn't look at your code). Just need two variables, one for the lowest value and the index of that lowest value. Just do a comparison for each iteration and if the current value is lower than the lowest value, set that value as the new lowest value and that index as the index of the lowest value. After that iteration, you should have the index of the lowest value and then you can say array[lowestindex]=999; or whatever.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you should go thru the array once with two variables:

    int low = INT_MAX;
    int index;

    To use INT_MAX you need to include limits.h. Or you could just set "low" to a very high number.

    Each iteration of the loop you test unsorted[i]. If is <= to low (the first time, that is guaranteed, because low is INT_MAX) you set low = unsorted[i] and set index=i.

    After the loop is complete, "index" will equal the subscript index of the lowest number in the array, so you just set unsorted[index] = 999.
    Actually, since you are subing in 999, I guess that should be the initial value of "low", not INT_MAX.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Arrays
    By dldsob in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2009, 08:43 PM
  2. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  3. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  4. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 02:06 AM
  5. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 02:57 AM

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