Thread: Array help!!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    35

    Unhappy Array help!!

    I have written a code that outputs a series of numbers, where these numbers are elements of the array. But I have problem for large ranges of this array. System uses random variables for the array elements that have an index out of the range [0,11]. I want my program not to display the array elements with their index out of the range [0,11], simply prevent them from being printed. How can I restrict my output just down to the this range's elements?

  2. #2
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    I don't quite get your question. So your indexes are randomly generated? Or the values in the array are?

    If you want to print the contents between two ranges, create a function to do it. The function will accept an array and two ints, One for the start and one for the end of the range of the indices you want to print.

    If you want to limit the range of a random number generator use a little mod math. rand() % 11 etc.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by farukyaz View Post
    I have written a code that outputs a series of numbers, where these numbers are elements of the array. But I have problem for large ranges of this array. System uses random variables for the array elements that have an index out of the range [0,11]. I want my program not to display the array elements with their index out of the range [0,11], simply prevent them from being printed. How can I restrict my output just down to the this range's elements?
    You need to post the minimum code sample that will demonstrate the problem.

    We are not mind readers.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by CommonTater View Post
    You need to post the minimum code sample that will demonstrate the problem.

    We are not mind readers.
    Here is the concerning part of the code:

    Code:
    for(a=1; a<=machines; a++)
        printf(" %d machine: %d, %d, %d, %d\n", a, sort[number-a], sort[number+a-(2*machines)-1], sort[number-a-(2*machines)], sort[number-(4*machines)+a-1]);
    Here, it generates arrays like sort[-1] or sort[12] and prints random values for them. I don't want this. I want it to print the ones that are between sort[0] and sort[11]. Is my problem clear now? Is there a way to do this?

    Quote Originally Posted by \007 View Post
    I don't quite get your question. So your indexes are randomly generated? Or the values in the array are?

    If you want to print the contents between two ranges, create a function to do it. The function will accept an array and two ints, One for the start and one for the end of the range of the indices you want to print.

    If you want to limit the range of a random number generator use a little mod math. rand() % 11 etc.
    They're not randomly created, the problem is their indexes are changeable. There are functions inside the index part of the array, so there I am getting thing like a[-1] too as a result of the function inside and I don't want them to be printed with random values that system generates. I posted my part of code, so you can see there. Can I eliminate them?
    Last edited by farukyaz; 04-05-2011 at 12:57 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here, it generates arrays like sort[-1] or sort[12] and prints random values for them. I don't want this. I want it to print the ones that are between sort[0] and sort[11]. Is my problem clear now? Is there a way to do this?
    You won't like this but... rework your code to stay in array bounds. C has no mechanism for preventing out of bounds access (a major weakness in the language, IMO) and it's entirely up to you as the programmer to stay within the limits.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by CommonTater View Post
    You won't like this but... rework your code to stay in array bounds. C has no mechanism for preventing out of bounds access (a major weakness in the language, IMO) and it's entirely up to you as the programmer to stay within the limits.
    I would accept if the program writes zero instead of not displaying them. So I simply wrote this code to assign zero value to all of the array elements out of range.
    Code:
    if(k>number-1 || k<0)
      {
        for(i=0; i<number; i++)
          if(sort[k]!=sort[i])
          sort[k]=0 ;
      };
    But this time my exe stops running. Where is the mistake?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(k>number-1 || k<0)
      {
        for(i=0; i<number; i++)
          if(sort[k]!=sort[i])
          sort[k]=0 ;
      }
    Do you see the problem yet?

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

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try it like this...

    Code:
    if((k < number) && (k > -1))
      {
        for(i=0; i<number; i++)
          if(sort[k]!=sort[i])
          sort[k]=0 ;
      };
    Last edited by CommonTater; 04-05-2011 at 05:41 PM.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by quzah View Post
    Code:
    if(k>number-1 || k<0)
      {
        for(i=0; i<number; i++)
          if(sort[k]!=sort[i])
          sort[k]=0 ;
      }
    Do you see the problem yet?

    Quzah.
    I do not, because my mind doesn't work correctly now. I wanted to make zero the array elements for which my code calculates an index less then zero or larger then number-1 (which is actually 11). So anything like sort[-1] or sort[12] should be zero. What's wrong with those k's?

    Quote Originally Posted by CommonTater View Post
    Try it like this...

    Code:
    if((k < number) && (k > -1))
      {
        for(i=0; i<number; i++)
          if(sort[k]!=sort[i])
          sort[k]=0 ;
      };
    I tried, this time it went back to assigning random value. But one point I don't understand, doesn't the code you wrote work for my range, not the outside?
    Last edited by farukyaz; 04-05-2011 at 05:55 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays start at 0 and go to size-1. So:
    Code:
    int array[3];
    Means you get access to:
    Code:
    array[-1] = /* YAY I might crash my program! */
    array[0] = something;
    array[1] = somethingelse;
    array[2] = anotherthing;
    array[3] = /* YAY I might crash my program! */
    You have to stay in the allowed bounds of your array.


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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    My code keeps k inside your array bounds.

    This is no small issue.... you have to learn to stay in the appropriate ranges. As I said before C does not do this for you. No matter what you're writing in those slots outside the array bounds you're going to crash your code.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by quzah View Post
    Arrays start at 0 and go to size-1. So:
    Code:
    int array[3];
    Means you get access to:
    Code:
    array[-1] = /* YAY I might crash my program! */
    array[0] = something;
    array[1] = somethingelse;
    array[2] = anotherthing;
    array[3] = /* YAY I might crash my program! */
    You have to stay in the allowed bounds of your array.


    Quzah.
    So there's no way of nondisplaying the part of my array that is out of the bounds, rather than crashing the program.. Gotta write the whole thing over again now.

    Quote Originally Posted by CommonTater View Post
    My code keeps k inside your array bounds.

    This is no small issue.... you have to learn to stay in the appropriate ranges. As I said before C does not do this for you. No matter what you're writing in those slots outside the array bounds you're going to crash your code.
    Okay I will write things in the way that won't exceed my array bounds. Thanks

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't have any part of your array out of bounds. Are you confusing "out of bounds" as "valid array indexes" with "not valid numbers I want them to use"? That's two things entirely different. It's up to you to keep track of all of that and decide what to do if something would fall out of bounds of a valid array index, as well as limiting your input/output/whatever it is you are using to the context of how you plan on using it.


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

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by quzah View Post
    You shouldn't have any part of your array out of bounds. Are you confusing "out of bounds" as "valid array indexes" with "not valid numbers I want them to use"? That's two things entirely different. It's up to you to keep track of all of that and decide what to do if something would fall out of bounds of a valid array index, as well as limiting your input/output/whatever it is you are using to the context of how you plan on using it.


    Quzah.
    I'd prefer C to automatically eliminate the part out of the bounds because it will be hard to rearrange my code so that I won't go out of bounds. But here I have to do.. Thanks for the information.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by farukyaz View Post
    I'd prefer C to automatically eliminate the part out of the bounds because it will be hard to rearrange my code so that I won't go out of bounds. But here I have to do.. Thanks for the information.
    There are two answers to this...

    1) A well written program doesn't go out of bounds... even on languages that prevent it by tossing errors at you.

    2) I think we would all like to see certain improvements in C, including bounds checking and real strings... but you would be very unwise to hold your breath waiting for it.

    The problem as it is now is that the compiler/runtime code doesn't actually know how big an array is... really if you do char* array = malloc(500) it's all well and duly forgotten as soon as malloc() returns. So the onus is on us as programmers to always "color inside the lines".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 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

Tags for this Thread