Thread: An equivalent "strlen function" for integer arrays?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    An equivalent "strlen function" for integer arrays?

    Good day C gurus

    I was trying to code the biggest commum divisor for two number program but was confronted to a problem

    here is my code by the way

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int a=100;
        int b=34;
        int diva[100]={0};// is the array in which i store the divisors or the numbuer a;
        int divb[100]={0};// is the array in which i store the divisors or the numbuer b;
        int max=1;
        int counta=0;int countb=0 ;int i; int j=0;int k=0;
        for(i=1;i<a;++i)
        {
            if(a%(a-i)==0)
            {
            diva[j]=a-i;
            j++;
            }
    
    
        }
    
    
            for(i=1;i<b;++i)
            {
            if(b%(b-i)==0)
            {
                 divb[k]=b-i;
            k++;
            }
    
    
            }
    for(j=0;diva[j]!=0;++j)
    {
        printf(" the divisors of the number %i are %i\t",a,diva[j]);
    ++counta;  // I needed to use a counter to know how many elements in my array diva I do have
    }
    
    
    for(k=0;divb[k]!=0;++k)
    {
       printf("\nthe divisors of the number %i are %i\t",b,divb[k]);
    ++countb; // I needed to use a counter to know how many elements in my array diva I do have
    }
    
    
    for(j=0;j<=counta;++j)
    {
       for(k=0;k<=countb;++k)
       {
        if(diva[j]==divb[k] && diva[j]>max )
        max=diva[j];
       }
    
    
    
    
    }
    
    
    printf(" the biggest commun divisor  is:%d", max);
    }
    My code is fine, but during the coding process i got stuck with the following problems and i'm just trying to understand them:

    • If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?
    • Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?

    As you can see, im using the counters (counta and countb) to figure out the necessary loops numbers.
    Many thanks in advance!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    You already have a count of the number of elements in diva, it's j.

    > for(j=0;diva[j]!=0;++j)
    So if you use another var, you can simply write

    for ( x = 0 ; x < j ; x++ )


    > If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?
    Because they're filled with garbage values, yet you try to use diva[j]!=0 test to find the end of the valid data.

    > Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?
    There is no standard nul integer in the same way that there is a nul character. strlen works on char arrays, because everyone agrees that \0 marks the end of strings, so it's useful to provide a function.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    > If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?

    Because that is just the way C is. Local variables are uninitialized by default1.

    > Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?

    Yes, but you would have to code this yourself. You would need to pick a sentinel value, a special integer value you would never need (for char strings, this is '\0'). Then you could write an intlen() function that works like strlen for int arrays, and checks for that special value. However, you would have to remember to always terminate your int arrays with this value every time you add a new element, or intlen() would not work correctly (just like forgetting to null terminate your string).

    As a side note, '\0' is a character literal (like 'a' or '!'), and as such has a numeric value which is zero. Thus, you could use it as your sentinel value if you never need to keep the actual value 0 in diva or divb.

    But I still prefer the counta/countb method you are currently using. It's the simplest/cleanest IMO.

    1 Technically this only applies to automatic storage duration local variables (non-static), but that's the default and by far the most common case, so no need to worry about static local variables just yet.

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Thanks a million to both of you Salem and Anduril!!

    Each time i come here I got valuable informations!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-22-2011, 08:39 AM
  2. Replies: 14
    Last Post: 11-08-2010, 01:47 AM
  3. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 09:20 PM
  4. "is Integer" function
    By diegozuke in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2006, 05:58 PM
  5. Equivalent of Java's "super" keyword
    By JasonLikesJava in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2002, 10:21 AM

Tags for this Thread