Thread: terminating an indefinately sized array

  1. #1
    Unregistered
    Guest

    terminating an indefinately sized array

    int sum_function()

    {
    char std_array[20];
    int total;
    int z;
    int sum;

    for (z=0; z<20;Z++){
    scanf("%s",std_array);
    total=atoi(std_array;
    sum=sum+total;
    }
    printf("The sum of thes numbers is %i\n",sum);
    return 0;
    }

    this works fine for 20 numbers or 30 or whatso, but I want to create an array of indefinite size but it has a termination condition, so when you type in a character it will calculate the sum of the integers that has been entered. The problem is I dont know how and where to put the termination condition.

    I tried having something like:
    for(z=0;sizeof(std_array) I dont know if this is right or wrong but without a termination condition it will continue to take in either integers or characters.
    Any ideas would be most helpful

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    So you want to enter integers and when the user types a character value the loop stops? This should work then:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int val,
          sum = 0;
      while ( scanf ( "%d", &val ) == 1 )
        sum += val;
      printf ( "The total sum is %d\n", sum );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    not just if i hit a character but if I were to enter the integers
    1,2,3,4,5,6,7,8 and then typed in end and then enter I would want it to calculate the sum of the values for the 8 integers that are there.

    the code that I started with is

    for(z=0;sizeof(std_array);z++){
    scanf("%s",std_array);
    total=atoi(std_array);
    sum=sum+total;
    printf("The sum of these numbers is %i\n",sum);
    return 0;
    }

    the code that I was given was a long the right track but I am not sure as to where I would put that while loop amongst this code here. Basically I want to be able to put integer values in until I either type a string like end or exit or even if I hit a character.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    not just if i hit a character but if I were to enter the integers
    1,2,3,4,5,6,7,8 and then typed in end and then enter I would want it to calculate the sum of the values for the 8 integers that are there.
    So what's wrong with the code I suggested? You can enter as many numbers as you like and it will work until the integer overflows, and to print the sum you can simply type a letter or an entire word. Aside from not using an array, which you've only implied, this meets all of your requirements.

    If you want to use an array then it's more difficult because you have to handle a situation where the size of the array must be increased to hold more input. Note that the code I gave you requires a space between each number, but you can easily change that for some other delimiting character.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    no I am not saying that there is anything wrong with your code at all.......but I dont know how to apply it when working with an array of size unknown.


    the problem I am having is creating the array and then terminating it when you hit a character . I am not sure as to how to do that.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Dynamically allocate the array with malloc, then read input into the array and if there is more input than array, resize it with realloc:
    Code:
    /* to create */
    int *array = malloc ( size * sizeof *array );
    
    /* to resize */
    int *temp;
    temp = realloc ( array, newSize * sizeof *array );
    if ( temp != NULL ) {
      /* all is well */
      array = temp;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    ok you can do that with integer arrays, but can you do this with character arrays.

    I like what you have done, but I am looking for something that uses just a loop statement preferably a while loop that I can put within the current code that I have written with slight modification.

    More to the point there must be an easier way to do this, without having to use malloc.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Han
    ok you can do that with integer arrays, but can you do this with character arrays.
    You can "do that" with any "array" you want. Replace int with char with struct mystruct with float with...

    Originally posted by Han
    I like what you have done, but I am looking for something that uses just a loop statement preferably a while loop that I can put within the current code that I have written with slight modification.
    So put it in a loop.


    Originally posted by Han
    More to the point there must be an easier way to do this, without having to use malloc.
    No, there doesn't have to be an easier way. This is C, this is how you create "reiszable arrays". Like it or leave it.

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

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you look at your original code:
    Code:
    int sum_function() 
    
    { 
    char std_array[20]; 
    int total; 
    int z; 
    int sum; 
    
    for (z=0; z<20;Z++){ 
    scanf("%s",std_array); 
    total=atoi(std_array; 
    sum=sum+total; 
    } 
    printf("The sum of thes numbers is %i\n",sum); 
    return 0; 
    }
    You are not using an array to store 20 numbers. You store one number, convert it, then store a new number where the old number was. Either use Prelude's original solution, or:
    You could use the function isdigit() to check the string:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int sum_function();
    
    int main(void)
    {
       sum_function();
       return 0;
    }
    
    int sum_function()
    { 
       char std_array[20]; 
       int total; 
       int i;
       int sum = 0;
       bool quit = false;
    
       do { 
          scanf("%s",std_array);
          for (i=0; i<strlen(std_array); i++)
          {
             if (!isdigit(std_array[i]))
             {
                 quit = true;
                 break;
             }
          }
          if (!quit)
          { 
             total = atoi(std_array); 
             sum += total;
          } 
       } while (!quit);
       printf("The sum of these numbers is %i\n",sum); 
       return 0; 
    }

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    when I ran the code using isdigit I had to change a few things, because the unix system did not recognise it.

    bool quit=false is not recognised......(I just get an error stating that false has not been declared)
    so I changed that to int quit =1.(is that right?)

    from what I was told when something is equal to zero it can account to being false and when it is one it can account to being true.

    so instead of having:

    if(!isdigit(std_array[i]))
    {
    quit=true;
    break;
    }

    I changed quit=true to quit=1.

    when I ran the program I got an error though which stated:

    subscript has type char.
    the problem was with this line

    if(!isdigit(std_array[i])) /* it didnt like this line*/

    I am still trying to fix this error:
    Last edited by Han; 04-17-2002 at 10:36 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > when I ran the code using isdigit I had to change a few things,
    > because the unix system did not recognise it.


    isdigit is a standard ANSI function. You probably aren't including 'ctype.h'.

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

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    All the changes you made sound right. In fact, I was using an old compiler, so I didn't have a bool either, so I put this at the top:

    typedef int bool;
    const bool false = 0;
    const bool true = 1;

    but what you did works just as well.

    >subscript has type char.
    >the problem was with this line

    >if (!isdigit(std_array[i])) /* it didnt like this line*/

    Hmm, I can't figure that one out. Maybe, since isdigit() actually takes an int argument:
    if (!isdigit( (int) std_array[i]))

    or maybe

    if (isdigit(std_array[i]) == 0)

    I don't know. Hope you figure this one out.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >subscript has type char.

    It almost sounds like it thinks i is a char, not an int. i is the subscript in that line.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    ok sweet I got all those problems figured out but when I run the program I hit one integer and it terminates telling me that the sum of the numbers is zero.

    Is there something else in the code that I should change, because it terminates when I hit one key integer or character.

    From what I see if it is a digit it should continue to loop but it doesnt it just terminates giving me a zero total.

    from looking at the code doesnt make any sense as to why it is doing that. if I press a character it does the same thing.

    I see this as a little weird
    Last edited by Han; 04-17-2002 at 11:27 PM.

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    Thanks a lot guys for your help, eventually after fooling around with the code and moving things around I have managed to get the sum function to work.

    so thanks for your time most appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  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. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM