Thread: Looping through array.

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    Looping through array.

    How can I get it to loop through an array until it reaches the value of -1? Can I do while(i!=-1)? I think a for statement afterwards but what do I put in the middle? for(j=0,?,j++)?

    Dont know if that will work. The only thing I can pass to this function is the array itself. So I cant do a counter of the number of user inputs.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    What range of numbers does your array contain? Does it accend or decend from -1 to something, or is it random numbers that contain -1?

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    if i understand u better if u want to check for -1; it means that u printing the array in a revese order. u point the pointer at the end of anj array. and u loop back to the first elements. so your while look show be
    Code:
    #define MAX 10
    .
    .
    
    int i=MAX;
    int array[MAX] = {10,20,30,40,50};
    .
    .
    while(i>-1)
         printf("%d\t",array[i]);
    
    // with the for loop it same again
    
    for(j=MAX;j>-1;j++)
        printf("%d\t",array[i]);
    
    or if u wanted to print it asusual from the 0th element
    for(i=0;i<MAX;i++)
       printf("%d\t",array[i]);
    note that all array elements starts from 0(array[0],[1][2]...)

    it would be better if u could show us your code attempt

    ssharish2005

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What they mean is this:
    Code:
    int array[] = { 1, 4, 77, 2, 9, 123, -1 };
    int x;
    
    for( x = 0; array[ x ] != -1; x++ )
        ...do something...
    They posted a "do my homework for me!!!!OMGOMG!!" earlier that got deleted. If it wasn't them, and I'm pretty sure it was, it's someone with the same homework problem. At any rate, simply compare the value in the array to -1, and stop when you reach it.

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

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by quzah
    What they mean is this:
    Code:
    int array[] = { 1, 4, 77, 2, 9, 123, -1 };
    int x;
    
    for( x = 0; array[ x ] != -1; x++ )
        ...do something...
    They posted a "do my homework for me!!!!OMGOMG!!" earlier that got deleted. If it wasn't them, and I'm pretty sure it was, it's someone with the same homework problem. At any rate, simply compare the value in the array to -1, and stop when you reach it.

    Quzah.
    woah, somebody had similar homework.

    This is what I tried and it wont actually stop at the -1 but keeps on going. But for some reason at stops at 49 for the first set and at 74 for the second set.

    this is what I tried, similar to what is shown above:

    Code:
    int setCardinality(int a[]){
    	
    	int i;
    	int numberElements=0;
    
    	for(i=0;a[i]!=-1;i++){
    		numberElements++;
    		printf("i is %d, numberElements is %d",i, numberElements); /*put in for test, and see that it is not stopping when -1 is hit*/
    	}
    	return (numberElements);
    
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > This is what I tried and it wont actually stop at the -1 but keeps on going
    You also need to show the context in which it is being called then as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM