Storing input in dynamic array

This is a discussion on Storing input in dynamic array within the C Programming forums, part of the General Programming Boards category; Hi, I'm currently having big trouble with simple program, the job is to input the data (only integers) from user ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Storing input in dynamic array

    Hi,
    I'm currently having big trouble with simple program, the job is to input the data (only integers) from user until he type dot ('.'), than store this integers in DYNAMIC array and finally sort it in ascending order.

    I'm having trouble dealing with DYNAMIC array, all these pointers. And so on.
    Here's my attempt:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(){
    
    
    int size=0; // size of array 
    int *array; 
    int *n, *m; // first element of array and last element of array 
    int i;
    int o=0;
    
    do
    {
    scanf("%d", &tab[o]);
    size++;
    o++;
    }while(getchar()!='.');
    
    array=(int*)malloc(n*sizeof(int)) // allocating memory; 
    
    n=array // address of first element of array 
    m=array+n*sizeof(int) // address of last element of array 
    
    qsort(array); 
    
    //DISPLAYING ARRAY AFTER SORTING
    
    for(array, array<(m); array=array+sizeof(int)
    {
    printf("Value is: %d", *i); 
    }
    
    return 0; 
    }
    Can you correct my code ? I can't make it work
    Any further explanation why or for what reason is highly desirable.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You code isn't even remotely near anything which can be compiled.

    How about beginning with
    int array[10];

    Now write the code which allows you to
    - input a maximum of 10 integers
    - sort them
    - print them

    Convince us you can do that much (or not, doesn't matter, we'll still help you wherever you get stuck), then we can talk about how to turn it into something using dynamic memory.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Thanks for advice !
    Ok, so here's my second attempt.
    Now, how can I transfer it to dynamic.

    Code:
    #include <stdio.h> 
    
    void bubbles(int [], int); // array of ints with it's size; 
    
    int main()
    {
      int n=0; 
      int array[10]; 
      int integer, i; 
    
      do
      {
         scanf("%d", &integer); 
         array[n]=integer;
         n++;
      } while(getchar()!='.')
    
      bubbles(array, n); 
    
      for(i=0; i<10; i++)
      {
         printf("%d\n", array[i]
      }
    
       return 0; 
    }
    
    void bubbles(int array1[], int size)
    {
      int a,b,c; 
      for(a = 0; a < (size - 1); a++)
      {
         for(b = 0; b < size - a - 1; b++)
         {
            if(array1[b]>array[b+1])
            {
               c=array1[b];
               array1[b]=array1[b+1];
               array1[b+1]=c;
            }
         }
      }
    }
    I want to make it dynamic, any further advices, hints ?
    Last edited by samtheearlier; 01-21-2013 at 01:56 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int main()
    {
      int n=0; 
      int array[n];
    Salem told you to make an array of 10 ints:
    Code:
    int array[10]
    Why don't you follow his advice?

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,057
    Quote Originally Posted by samtheearlier View Post
    Thanks for advice !
    Ok, so here's my second attempt.
    Now, how can I transfer it to dynamic.
    It still won't compile. You missed some semicolons in the main function, and in your sorting function you called it `array1' not `array'.
    Also, your printing procedure assumes 10 items even if they only gave 4.
    Finally, your input loop seems a bit suspicious. If you give this input

    9
    3
    8
    2
    .

    I would expect to get back 2, 3, 8, 9. However, because of the way you set up the loop, this will give incorrect results unless you type it without the final newline at the end

    Code:
    9
    3
    8
    2.
    I would suggest to make it work as correct as possible with a static buffer before you worry about trying to use dynamic allocation.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Like so
    Code:
    int *array = NULL;
    size_t usedSize = 0;
    size_t availableSpace = 0;
    
    // then in the loop
    if ( usedSize == availableSpace ) {
        size_t nextSpace = availableSpace == 0 ? 16 : availableSpace * 2;
        void *temp = realloc( array, nextSpace * sizeof(*array) );
        if ( temp != NULL ) {
            array = temp;
            availableSpace = nextSpace;
        } else {
            // no more room at the inn
            // break out of the loop now!
            // later on, you'll need free(array)
        }
    }
    array[usedSize++] = value;
    A couple of things to note
    - the use of a temporary variable in case realloc fails
    - the calculation of the next size. Do not do something like just allocate one extra slot, it will thrash memory.
    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. Storing user input into integer array
    By samii1017 in forum C Programming
    Replies: 2
    Last Post: 10-28-2012, 03:31 PM
  2. Storing input values into Array
    By nelly26 in forum C Programming
    Replies: 1
    Last Post: 11-30-2011, 08:42 AM
  3. Storing values into a dynamic array
    By porsche911nfs in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2009, 09:08 AM
  4. how to create a Dynamic Array storing objects?
    By simjay in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2006, 03:39 PM
  5. Trouble storing file input in array
    By difficult.name in forum C Programming
    Replies: 1
    Last Post: 10-10-2004, 11:54 PM

Tags for this Thread


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