Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: A function in "C"  (Read 5477 times)

0 Members and 1 Guest are viewing this topic.

ŽOŠK

  • Guest
A function in "C"
« on: May 20, 2007, 08:06:30 AM »
I have a problem with writing a function in "C". I don't know how to do this - to make an array with some elements an empty array. I mean to clean the array so that it doesn't have any elements and then fill it again with other elements.

yanng1



    Beginner

    Re: A function in "C"
    « Reply #1 on: May 28, 2007, 09:43:24 AM »
    do you know how to code  a loop? A 'for' or 'while' loop would work nicely.
    I am going to run a code example, but it is fairly simple, because it assumes that the array elements will only be integers.  If you are filling the array with more complicated elements, like objects, Than you must know that the array type must match the type of every single element to be indexed in the array.  having said that:


     Let's pseudo-code it:

    //declare array and size

    array myarray[10]; <- this array has 10 elements.

    //run for loop to index every element, and set its value to NULL
    //NULL is the default "no value" indicator in C.

    for( index=0;index<= number of elements in array; index++ )

    myArray[index]= NULL;


    Now remember this is pseudo-coded, so the syntax is not correct.  I havent
    written C in so long, im sure to lead you wrong if I try to use the language syntax.  Hope that helps!


    ŽOŠK

    • Guest
    Re: A function in "C"
    « Reply #2 on: May 28, 2007, 03:12:40 PM »
    I tried this way but I think it doesn't work...  :-\

    yanng1



      Beginner

      Re: A function in "C"
      « Reply #3 on: May 28, 2007, 03:42:58 PM »
      I wonder, how do you know it is not working?  Your reply sounds a little unsure.  If it is throwing an exception, I would advise to be absolutely sure you index and count from zero, not 1, as it the start index for an array.  let me know how you know it's not working for you

      Dilbert

      • Moderator


      • Egghead

      • Welcome to ComputerHope!
      • Thanked: 44
        Re: A function in "C"
        « Reply #4 on: May 28, 2007, 10:13:17 PM »
        yanng1, there is no array variable-type. This would be what you're looking for:

        int myArray[10];
        "The geek shall inherit the Earth."

        yanng1



          Beginner

          Re: A function in "C"
          « Reply #5 on: May 31, 2007, 10:41:47 AM »
          *  I see, Dilbert.  when i said type, I don't mean to say there is a type called array. 
          I wrote "Array myArray" b/c I am a java head.

          Now, what I meant for our friend Rock, is that every array should have a single type.  let it be function type, primitive data type (like int) or  String... w/e. 
          that the array is most easily manipulated for future use if  you can keep
          it down to storing only one kind of information.

          int myArray[10] <--- for instance would be of the type "int", Rock.


          Dilbert

          • Moderator


          • Egghead

          • Welcome to ComputerHope!
          • Thanked: 44
            Re: A function in "C"
            « Reply #6 on: May 31, 2007, 10:55:53 PM »
            True, but the OP may have misunderstood. ;)
            "The geek shall inherit the Earth."

            Neil



              Expert
            • Fear me Track. Noone can escape my wrath.
            • Thanked: 3
              Re: A function in "C"
              « Reply #7 on: June 12, 2007, 09:08:43 AM »
              It sounds like you are talking about a dynamic or variable-sized array. Please give more information about the exact nature of this array, and any code you have written. This will put a 0 in all elements of an array of length N.

              for (int i = 0; i < N; i++) array = 0;

              Shynnie



                Intermediate

              • "All I want is his whereabouts, then I'm rich"..!!
                Re: A function in "C"
                « Reply #8 on: June 25, 2007, 03:13:03 AM »
                Hi Bra
                I c u had trouble with fuctions in C, i'm kind not too good but can help out.

                int array[5], i=0;
                   
                   for ( i=0;array<i;i++){
                      cin>>array;

                This insert a specific value into an array at a specific position..
                =============================================================

                "those who know how will always work 4 those who know why"

                “the absence of evidence is not the evidence of absence”