Computer Hope

Software => Computer programming => Topic started by: ŽOŠK on May 20, 2007, 08:06:30 AM

Title: A function in "C"
Post by: ŽOŠK 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.
Title: Re: A function in "C"
Post by: yanng1 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!

Title: Re: A function in "C"
Post by: ŽOŠK on May 28, 2007, 03:12:40 PM
I tried this way but I think it doesn't work...  :-\
Title: Re: A function in "C"
Post by: yanng1 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
Title: Re: A function in "C"
Post by: Dilbert 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];
Title: Re: A function in "C"
Post by: yanng1 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.

Title: Re: A function in "C"
Post by: Dilbert on May 31, 2007, 10:55:53 PM
True, but the OP may have misunderstood. ;)
Title: Re: A function in "C"
Post by: Neil 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;
Title: Re: A function in "C"
Post by: Shynnie 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..