Empty Array

This is a discussion on Empty Array within the C Programming forums, part of the General Programming Boards category; How would someone go about creating an empty array that would be filled from user input ??? Thanks...i have no ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Empty Array

    How would someone go about creating an empty array that would be filled from user input ???

    Thanks...i have no code for this yet cause i have no clue how to start an empty array

    thanks

    devilsknight

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have to have it empty. Just fill it up and keep track of your last entry (assuming you aren't using the entire thing). There is no "empty" state in C. You could specify a value as "empty" and check that spot to see if it equals "empty", but there is no such thing as an empty array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    what i meant was if i didn't know what the user was to enter, for example if i asked for integer numbers...and he entered 20 how would i tell the array to be that big ?

    thanks

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Use a vector of strings like this:
    Code:
    std::vector<std::string> myStrings;
    
    std::string tmp;
    do {
      std::getline(std::cin, tmp, '\n');
      myStrings.push_back(tmp);
    } while(tmp != "stop")
    myStrings.pop_back();

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Well, since the STL didn't make it into the C standard, you can use malloc().
    Code:
    int size = 20;
    int *a = malloc(size * sizeof(int));

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    God, sorry. I forgot I was in the C forum. Nevermind my post.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > and he entered 20 how would i tell the array to be that big ?
    There's no such thing as an expanding array in C.

    Either you have
    Code:
    int arr[100];
    int numEntries = 0;
    printf( "Enter up to 100 numbers\n" );
    for ( i = 0 ; i < 100 ; i++ ) {
      if ( scanf("%d",&arr[i]) != 1 ) {  // not the best input routine
        // end of data (somehow)
        numEntries =  i;
        break;
      }
    }
    Or you could dynamically extend an allocated pointer by using realloc.
    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.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Linked list?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The closest you can get is realloc.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    That stuff sounds kind of hard and probably way ahead of what i'll learn in this class

    here's the teachers question...the rest of the question i left out cause i pretty much know how to program that part

    "In statistics, the mode of a set of values is the value which occurs most often or with the greatest frequency. Write a function that accepts the following arguments:
    An array of integers
    An integer that indicates the number of elements in the array
    "

    how would i go about programming that ? if we didn't know how many numbers a user were to say enter then having to store them into an array ?

    or should i make the array rediculously large lol

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Just declare an array (of some size).
    Then ask the user "How many numbers do you want to input?"

    If that's bigger than your array size, just print a message and exit.
    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.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    If the number of elements is input first, you don't need the realloc() stuff. You can go with simple malloc(). As long as you've learned pointers, that's enough.

    If you haven't learned pointers, either go with Salem's approach or complain to the teacher.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    ok i think i figured out a way around it...i ask the user for how many numbers it wants to enter...then pass that number to a function and create an array

    however i get this error:
    constant expression required in function meanArray

    Code:
    #include <stdio.h>
    
    /*
    In statistics, the mode of a set of values is the value which occurs most often or with the 
    greatest frequency. Write a function that accepts the following arguments:
    	An array of integers
    	An integer that indicates the number of elements in the array
    */
    
    int meanArray(int arrayNum);
    
    int main()
    {
    	int num;
    
    	printf("How many numbers do you want to enter ? ");
    
    	scanf("%i", &num);
    
    	meanArray(num);
    
    	return 0;
    }
    
    int meanArray(int arrayNum)
    {
    	
    	int makeArray[arrayNum];
    
    	return 0;
    }
    Last edited by devilsknight; 07-18-2006 at 02:46 PM.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Woudn't it be easier just to have an array already declared of a fixed length? i.e. can you make assumptions about the maximum number of elements the user will input?

    Is there a requirement that the array has to be dynamically created based on user input?

    Just trying to make your job easier...

  15. #15
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    yah the project states that i must let the user input what size then pass it along...

    so how do i fix that error ???

    i'm been workin away on it and have no clue how to work it

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. making an empty 2-D array
    By starX in forum C Programming
    Replies: 4
    Last Post: 02-08-2002, 12:09 AM

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