Thread: Homework - "custom" array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Homework - "custom" array

    I don't know the exact terminology for this, so excuse the wording. I'm looking to "customize" a predefined array to any given length(parameters being the actual size of the array). What I mean by this is, say I have an array of size 5000. I then, through some function, input the number 500. What this function does is take (5000 - Number_Input) indexes and declare them void, such that only 500 array indexes are "valid" for use.

    What is done after this array of size "500" isn't really of concern, because all the other things I need to do with the actual array aren't an issue.

    Is such a thing possible? Is there a better way to do this? If so, it would be absolutely fantastic if I could get a push in the right direction. I haven't written the code for this function yet because I really have no idea where to start after my initial prompt/scanf. I have not learned about pointers or dynamic memory allocation in my class yet.

    //edit: looking over another homework thread it seems I misunderstood the rule on crossposting, thinking it only applied to various subforums here(like spamming a "HALP PLZ" in a Q/A, here, the C++ forum, etc). I have posted my question on another forum, Link is here. If you decide to lock this thread for that reason, I understand completely.
    Last edited by Wheaties; 03-14-2011 at 12:13 AM.

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Looking at this problem, I would try to use dynamic memory management. But if it hasn't been taught in your class before, your instructor may be seeking a different answer here. You can't re-size arrays unless you use dynamic memory management.
    Perhaps just have a function that creates a new array, and copies over the old array's content? Maybe print a warning if the new array is too small to hold the content of the old array.

    But yeah, in terms of dynamic memory management look towards using malloc/calloc and realloc. Make sure you free your pointer when you're done with it.

    Edit: Code Example. Take note that pointers are almost the same things as arrays. You can't use sizeof to get the size of all of the elements in a pointer, but you can in an array declared locally, for example. So make sure you have a separate variable for keeping track of the size.
    Code:
    #include <stdlib.h> //for malloc & friends
    
    int main(void)
    {
    	int *intAry = malloc(5000 * sizeof(int)); //malloc memory block of 5000 * sizeof(int) bytes, assign it to a pointer called intAry.
    	intAry = realloc(intAry, 4500 * sizeof(int)); //rellocate the memory block to 4500 * sizeof(int) bytes @ the starting pos. of intAry.
    	free(intAry); //need to explicitly free our pointer
    	return 0;
    }
    Last edited by Phenax; 03-14-2011 at 12:09 AM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It sounds like you want to malloc an array of 500 (or 501) elements, and then move any good data you want, into that newer array.

    If you want that 500 as a valid index number, be sure to set your array size to 501, since C arrays always begin with 0.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Memory allocation and pointers haven't been taught yet. I was actually about to update the original post with that, but it seems you've already answered . I'd like to do that, but seeing as we haven't learned it I think something like that would be rather...obvious and out of place. We don't really stray from out textbook, and we have not covered pointers or dynamic memory allocation in the text chapters.

    Phenax, that idea about creating two arrays doesn't sound bad, but because I need to have the array I do comparisons with be a size defined by a user input, I think that leads me back to the same problem. Unless I misunderstood.

    Edit: I'm reading up on the stuff right now and I'll be playing with your example to see if I can get the hang of pointers and try to integrate it into my code.
    Last edited by Wheaties; 03-14-2011 at 12:23 AM.

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Are you able to post the purpose of this code, or some specifications? Because to me, it seems like the only way to accomplish this is making a new array and copying previous data (Which isn't a problem as long as the new array isn't too small), or using dynamic memory management. Perhaps the question you are asking isn't totally in sync with the problem you are having. Or perhaps I'm misunderstanding.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Quote Originally Posted by Phenax View Post
    Are you able to post the purpose of this code, or some specifications? Because to me, it seems like the only way to accomplish this is making a new array and copying previous data (Which isn't a problem as long as the new array isn't too small), or using dynamic memory management. Perhaps the question you are asking isn't totally in sync with the problem you are having. Or perhaps I'm misunderstanding.
    I'll use placeholder values here, because I prefer to keep things generalized so I make the logical connections myself.

    The purpose of the code is to establish an array within certain parameters(say, from 1 to 5000), input the values of the array whose size has been established by a user input and run various comparison tests on that given array, searches and sorting, etc. All the stuff that comes after the actual input for the size of the array isn't a problem, which is why I didn't feel it was necessary to post. That was probably a mistake as it could lead to misunderstandings and ambiguity, though.

    If dynamic memory management is out of the equation(it may not be, I've got various web FAQs and my textbook open regarding this), how would you go about the second method you've mentioned without needing to already establish they array sizes? Wouldn't copying previous data require a new array of an already defined size? Unless I'm mistaken or unaware of all the various, cool methods you can use.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at malloc() and realloc()

  8. #8
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by Wheaties View Post
    I'll use placeholder values here, because I prefer to keep things generalized so I make the logical connections myself.

    The purpose of the code is to establish an array within certain parameters(say, from 1 to 5000), input the values of the array whose size has been established by a user input and run various comparison tests on that given array, searches and sorting, etc. All the stuff that comes after the actual input for the size of the array isn't a problem, which is why I didn't feel it was necessary to post. That was probably a mistake as it could lead to misunderstandings and ambiguity, though.

    If dynamic memory management is out of the equation(it may not be, I've got various web FAQs and my textbook open regarding this), how would you go about the second method you've mentioned without needing to already establish they array sizes? Wouldn't copying previous data require a new array of an already defined size? Unless I'm mistaken or unaware of all the various, cool methods you can use.
    Maybe I'm totally misunderstanding but can't you do

    int size = 2000;
    int oldAry[size];
    scanf(...); //get user input
    int newAry[size - userinput];
    for(...) //copy oldAry contents to newAry
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  9. #9
    Novice
    Join Date
    Jul 2009
    Posts
    568
    The obvious solution to me is to take the new length and pretend that to be the new upper bound of the array. Is there anything wrong with that?

    It's not a clean solution, but it would be functional.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #10
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by msh View Post
    The obvious solution to me is to take the new length and pretend that to be the new upper bound of the array. Is there anything wrong with that?

    It's not a clean solution, but it would be functional.
    Dynamic memory management is definitely the most satisfactory answer to your question. But I would just stick with being a little bit inefficient and not using things your professor hasn't taught you yet.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by msh View Post
    The obvious solution to me is to take the new length and pretend that to be the new upper bound of the array. Is there anything wrong with that?

    It's not a clean solution, but it would be functional.
    Yep... it's totally harmless to have a 5000 element array with only 200 used.

    There are all kinds of end-detection schemes, sentinal values, index counters etc.
    Unless he's opening 500 of these things, I shouldn't think a bit of "extra space" would be much of a problem.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Yep... it's totally harmless to have a 5000 element array with only 200 used.

    There are all kinds of end-detection schemes, sentinal values, index counters etc.
    Unless he's opening 500 of these things, I shouldn't think a bit of "extra space" would be much of a problem.
    This sounds like it would be the best option. Sorry Phenax, but the previous solution you mentioned is something I tried earlier. Visualstudio doesn't allow me to write an array unless there's a constant value in the bracket(so damn annoying).

    //edit: Okay, so it really can be just as simple is limiting the array to whatever the user input if what I'm gathering is right. If so, I feel stupid. I'll post if there's any trouble with implementation.
    Last edited by Wheaties; 03-14-2011 at 01:57 AM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Wheaties View Post
    //edit: Okay, so it really can be just as simple is limiting the array to whatever the user input if what I'm gathering is right. If so, I feel stupid. I'll post if there's any trouble with implementation.
    Yep it can be as simple as counting the number of elements entered...

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Boy, this just gets better every hour. Apologies for making this simple thing so difficult, my lack of sleep certainly isn't helping either.

    Okay, so I've been trying various methods of adjusting how many indexes I do stuff with in my array, but they don't quite seem to work syntactically. Hilariously, the best thing I could think of was establishing a MAX value(defined at the top) so I don't need to plug in a constant, like "5" into my function calls in main.

    Naturally, I'm unable to change that value to the number that is input from my input function. I don't know exactly how to ask for help on this without example code, which is something I'd ideally like to avoid.

  15. #15
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Do you have to code this using C89? C99 has flexible arrays, which would make it easy. Most modern compilers usually have a switch to turn on C99.

    If you have to work with constants in the brackets, then your way sounds logical.... Maybe we're approaching this problem wrong in that direction, if you're restricted to constants. Have to take off for the day though, good luck.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  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