Question on array

This is a discussion on Question on array within the C Programming forums, part of the General Programming Boards category; Hi , I have question about array. The program below has a fix array(100). I want the size of the ...

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

    Question on array

    Hi ,

    I have question about array. The program below has a fix array(100). I want the size of
    the array to be determined by the user. How to write the code ?

    The function "input_number" ask user to enter the size of the array. And i would like to allocate the entered value to be the size of the array. This way, user can enter more than 100 and my program will not crash.


    Code:
    #include<stdio.h>
    
    int n, decimal;
    int i, j, tmp;
    int number[100];
    
    
    input_number()
    {
     int ch;
     
     printf("Enter how many element to store in array: ");
            decimal = 0;
    	    ch = getchar();
      
       while(ch != '\n')
        {
    	 if('0' <= ch && ch <= '9')
    	    {
    	            decimal = decimal * 10;
                    decimal = decimal + (ch - '0');
                }
    	  
    	   ch = getchar();
        }
    	
    }
    
    
    input_array()
    {
     int array;
    
     for(i=0;i<decimal;i++)
      {
       printf("Enter array %d: ",i+1);
       
            array = 0;
    	    ch = getchar();
      
       while(ch != '\n')
        {
    	 if('0' <= ch && ch <= '9')
    	    {
    	            array = array * 10;
                    array = array + (ch - '0');
            }
    	  
    	   ch = getchar();
        } 
    
       number[i]=array;
      }
    }
      
    
    
    
    main()
    {
     
     
     input_number();
     input_array();
     
     n=decimal;
     //print the unsorted numbers
     printf("\nUnsorted number : ");
     for(i=0;i<n;i++)
      {
       printf("%d, ",number[i]);
      }
      
      printf("\n");
     
     for(i=0;i<n-1;i++)
      {
       for(j=0;j<n-i-1;j++)
        {
    	 if(number[j]>number[j+1])
    	  {
    	   tmp = number[j];
    	   number[j] = number[j+1];
    	   number[j+1] = tmp;
    	  }
    	}
      }
     
     //print the sorted numbers
     printf("Sorted number   : ");
     for(i=0;i<n;i++)
      {
       printf("%d, ",number[i]);
      }
    }

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    959
    If you're going to use C99, you can just use variable length arrays which are very straightforward:

    Code:
    int arraylength = 502;
    int array[arraylength];
    Otherwise, let me introduce malloc():

    Code:
    int arraylength = 502;
    int *array;
    
    /* allocate memory for arraylength number of integers
        size_t is the appropriate type for malloc and the type of sizeof()
        you do not need to cast malloc as an int type, this is unnecessary in ISO C
        see http://www.cprogramming.com/faq/cgi-bin/smartfaq.cgi?answer=1047673478&id=1043284351
    */
    array = malloc((size_t)arraylength * sizeof(int));
    
    if (!array)
    {
         printf("\nMalloc failed!");
         exit(EXIT_FAILURE);
    }
    
    /* do stuff */
    
    /* free up the memory that was allocated, set the array pointer to NULL so nothing else can use that now freed up memory block */
    free(array);
    array = NULL;
    Last edited by Epy; 11-25-2009 at 09:02 AM. Reason: added some more notes

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    959
    Should've mentioned that this is part of the C standard library. Also, you can check out calloc() which does the same thing except it initializes that memory block to 0s. Its syntax is slightly different:

    Code:
    array = calloc((size_t)arraylength, sizeof(int));
    malloc - C++ Reference
    calloc - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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