Need help with arrays..

This is a discussion on Need help with arrays.. within the C Programming forums, part of the General Programming Boards category; Lets say I have the array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } and ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    Need help with arrays..

    Lets say I have the array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } and I want to pick 4 of those arrays, somewhere in the middle... like for an example, I want to only use 4 of those elements starting at 3... in that way, it prints out 3, 4, 5, 6... How do i do this using arrays?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Just search for the element whose value is 3, then take it and the next three elements.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    How do i do that, like I'm just learning about arrays...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know how to access a particular element of an array?

    Do you know how to repeat a statement (or group of statements) repeatedly until some condition is met?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    also, the lets say the array will change here and there, so I won't be able to search for 3 if there isn't a 3 in the array. I want to pick the "third " element in the array and that is what I want to know, sorry that i was confusing.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    How would you search for an element with the value of 3? Provide some example code.

    I want to pick the "third " element in the array and that is what I want to know, sorry that i was confusing.
    The third element in the array is array[2].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Code:
    /*
     * I want to print out how many elements in the array starting with the index given
     * function for pick is pick(int many, int starter, arr[])
     * My output with int array[] = {1, 5, 6, 8, 1, 10, 11, 13};
     * pick(4, 3, array);
     * 8 1 10 11
     *

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay, so now we'll add "do you know how to repeat a statement (or group of statements) a fixed number of times?" to the list.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    I only know how to do things like "while this is true, do this, until its proven false" or "for(i=0;i<0;i++).. I dunno if this helps.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    I've been working on it, I guess the for loop is used here and i thought i had it, but... nothing happens.

    Code:
    void pick(int many, int starter, int arr[]) {
    	int i, s, h;
    	many= h;
    	starter= s;
    
    	for(i=s;s<h;i++)
    	{
    		printf("&#37;d ", arr[i]);
    		fflush(stdout);
    	}
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    You should give your variables descriptive names. Okay, we can make an exception for i since it is a conventional name for a loop/array index, but s and h are meaningless. Worse still, you assign h to many, but h was not initialised, and you clearly want to use the value of many.

    Your loop itself either does not run or does not terminate (in theory) since if s < h, then s is always less than h since neither s nor h change in the loop body.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So for loops do things for a fixed number of times, yes. So:
    1. How many times do you want your for loop to run?
    2. How do you write a for loop that runs that many times?
    3. If this is the i'th iteration of the loop (where i = 0, 1, 2, ... ?) what array index do you want to access?
    4. How do you access and print that array element?

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by tabstop View Post
    So for loops do things for a fixed number of times, yes. So:
    1. How many times do you want your for loop to run?
    2. How do you write a for loop that runs that many times?
    3. If this is the i'th iteration of the loop (where i = 0, 1, 2, ... ?) what array index do you want to access?
    4. How do you access and print that array element?
    Well I want to loop it on the number of "many" that is put into the function pick.
    I want to access the "starter" part of the array, so if number 3 is put into the function, then i want it to start at that element.
    Well, the only thing I can think of is... "%d" or "%s".

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Its all good you guys... I can't solve it, been thinking about it for like 1.5 hours now.. but I won't give up... I know there is a way to do it and i know its possible. I just need to find out how but thanks though.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Do you just mean this?
    Code:
    void pick( int many, int starter, int arr[])
    {
    	int i;
    
    	for (i = starter; i < starter + many; i++)
    		printf( "%d ", arr[i]);
    	printf( "\n");
    }
    BTW, programmers often work in groups, so you should work on your communication skills!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21