Thread: questions about arrays

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    questions about arrays

    I gonna post 2 questions , thanks in advanced for anyone!

    1) I created a program that takes an array(arr[] in my prog.), and put the number of it in a different array, but only once (check in my prog).
    for example , for
    Code:
    arr={1,2,3,1,2}
    we'll get
    Code:
    check={1,2,3}
    for this task I wrote a function isexist wich returns 0 if the number is not found already in "check" and positive number if it's already there.
    as you may guess , this program does not work and get stuck at the searching and putting part.
    can someone tell my what is the error? it drives me crazy because I can't see why it's not working..
    Code:
    #include <stdio.h>
    
    int isexist (int x1,int arr1[]);
    
    int main (void)
    {
    int arr[5];
    int x;
    printf ("enter 5 numbers: \n");
    for (int i=0;i<5;i++)
        {
        scanf ("%d",&x);
        arr[i]=x;    
        }    
    
    int check[5];
    int z=0;
    
    for (int j=0;j<5;j)
    {
    if (isexist(arr[j],check)==0)    
        {check[z]=arr[j];
        z++;
        }
    
    }
    
    
    for (int i=0;i<5;i++)
    printf ("%d\n",check[i]);
    return 0;    
    }
    
    
    int isexist (int x1,int arr1[])
    {
        int exist=0;
    for (int i=0;i<5;i++)
        if (x1==arr1[i])
        exist++;    
    return exist;
    }

    2) I tried to write a function which says how many elements there are in an array. this function brings back weird numbers sometimes, and also stops counting if one of the elements are 0. can someone advise me how to improve this function?
    Code:
    int  howmany (int array[])
    {
    int i,z;
    for (i=0;array[i]!=0;i++)
    z=0; //just a garbage code line to give the for loop something to do while i is getting bigger
    return i;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For your first question, where do you ever assign any values to the check array?

    For you second question, which element of the check array did you assign a value of zero.

    Remember arrays in C are not zero terminated and have undefined values until initialized. You should be passing the sizeof the array into the function as another parameter and using that value to terminate your loop.

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    about the first question - how can I put initial numbers into the array and yet make "isexist" work with every array? if I put numbers in "check" to initialize it it can clash with numbers from "arr" array

    about the second - it's a general function, not necessarily for the first program.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Dave11 View Post
    2) I tried to write a function which says how many elements there are in an array. this function brings back weird numbers sometimes, and also stops counting if one of the elements are 0. can someone advise me how to improve this function?
    Its not possible in general to know how many elements in an array are "used up". Usually you have to keep track of this information in a separate counter, e.g.

    Code:
    const int arr_max = 100;
    int arr[arr_max];
    int arr_size = 0; // initially we say the size is 0
    With this arrangement, you can increment arr_size whenever you place an element into the array, and decrement it when you take something out. For example, you can add the element 42 to this array

    Code:
    assert(arr_size + 1 < arr_max);  // prevent buffer overflows in debug mode
    arr[arr_size++] = 42;
    Functions that operate on the array usually need all three pieces of information, arr, arr_max and arr_size. You can also put them into a struct to make it easier to use.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    thanks c99tutorial !
    jimblumberg - I initialized check with zeroes , and still the program get stuck..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming questions including the use of arrays
    By Volodya93 in forum C Programming
    Replies: 3
    Last Post: 12-03-2012, 09:36 AM
  2. some questions about arrays
    By student111 in forum C++ Programming
    Replies: 15
    Last Post: 02-17-2012, 08:51 AM
  3. Questions on Character Pointers and Arrays
    By 7heavens in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2010, 04:00 AM
  4. A couple of questions concerning arrays
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2006, 06:47 PM
  5. questions about arrays
    By laasunde in forum C++ Programming
    Replies: 3
    Last Post: 08-14-2003, 05:53 AM