Thread: Dividing array by scalar

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    Dividing array by scalar

    I wrote this algorithm for dividing an array by a scalar number and putting it in another array, now saying that this does not work i keep getting an error, i just cant seem to see why. And also is there anyway of implementing a "." into an array location?

    Code:
    	int a[]={5,3,0,7,0,0};
    	int b[6];
    	int i;
    	int divider=7;
    	int j;
    	for(i=0;i<6;i++)
    	{
    		b[i]=a[i]/divider;
    		a[i+1]=a[i+1]+((a[i]%divider)*10);
    	}
    	for(j=0;j<6;j++)
    	{
    		printf("%d",b[j]);
    	}

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When i==5 and you have a[i+1], you're out of the array bounds.

    What errors are you getting. What output is wrong? What output are you expecting?

    Details!

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Error: Run-Time Check Failure #2- stack around the variable "a" was corrupted.

    Now my output is 075814 which is correct, but still have the error

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Post your updated code.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Same code lol

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    So basically i see now that i was outside my array bounds, now knowing that, is there a way to create more array bounds as you go through the algorithm when needed?

    Edit: Error was solved when the loop is i<5

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What do you mean by "more array bounds"? You have a low boundary, and a high boundary. That's it.

    C doesn't stop your program from possibly giving you a correct answer, even though the program has other errors. Naturally, you should remove those other errors.

    Did you change your <6 to <5 in the loop to fix the problem with a[] ?
    Last edited by Adak; 11-12-2010 at 11:42 AM.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Well i mean can i increase the High boundary as i go through, basically adding more array locations, so that when i need more decimal places for instance i can generate them

    Yes thats what i fixed, to remove the error.

    Code:
    	int a[]={5,3,0,7,0,0};
    	int b[6];
    	int i;
    	int divider=7;
    	int j;
    	for(i=0;i<5;i++)
    	{
    		b[i]=a[i]/divider;
    		a[i+1]=a[i+1]+((a[i]%divider)*10);
    	}
    	for(j=0;j<6;j++)
    	{
    		printf("%d",b[j]);
    	}
    Last edited by omGeeK; 11-12-2010 at 11:44 AM.

  9. #9
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Lets try something else.

    Why are you doing this?
    Code:
    a[i+1]=a[i+1]+((a[i]%divider)*10);

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    EDIT: Disregard, way off base here:
    Aw yes, that's the VLA (Variable length array). It is working it's way up the ladder, and will be a standard part of C, "soon".

    Some compilers have this feature already, but my old workhorse, doesn't! < cry >

    Until yours does, the only way to enlarge an array is by making it dynamic, and malloc()'ing what you need (or realloc() if you have already malloc'd the memory.

    The general way to handle this in a practical and easy manner, is to size your array, LARGE, and then use only what you need. Then count the number of good elements that have data, and use the array, like it was just that size:
    Code:
    for(i=0;i<size of my data, not th size of the array; i++) 
      printf("%d", arrayName[i]);
    Last edited by Adak; 11-12-2010 at 12:09 PM.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Quote Originally Posted by msh View Post
    Lets try something else.

    Why are you doing this?
    Code:
    a[i+1]=a[i+1]+((a[i]%divider)*10);
    Well i do that to carry over the remainder just like long division, example if i have 57/5

    a[0]=5 , a[1]=7

    now i make a[1] = 7 + (the remainder of the the first number which is 0 and times that by 10 to move it over to the right place)

    which would then give me a[1]=7 again and i would get a remainder of 2 this time and continue the process until i have the required precision
    Last edited by omGeeK; 11-12-2010 at 11:57 AM.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Quote Originally Posted by Adak View Post
    Aw yes, that's the VLA (Variable length array). It is working it's way up the ladder, and will be a standard part of C, "soon".

    Some compilers have this feature already, but my old workhorse, doesn't! < cry >

    Until yours does, the only way to enlarge an array is by making it dynamic, and malloc()'ing what you need (or realloc() if you have already malloc'd the memory.

    The general way to handle this in a practical and easy manner, is to size your array, LARGE, and then use only what you need. Then count the number of good elements that have data, and use the array, like it was just that size:
    Code:
    for(i=0;i<size of my data, not th size of the array; i++) 
      printf("%d", arrayName[i]);
    Ahhh i see, alright thanks for the help

  13. #13
    Novice
    Join Date
    Jul 2009
    Posts
    568
    What does this have to do with VLAs?

    VLAs let you define arrays whose size is not known at compile-time. And that's pretty much it; they do not resize dynamically. There is std::vector in C++, which is, basically, a self-resizing array, but there is none of that in C, ANSI or C99.

  14. #14
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    First of all i have been programming for only two months now, so pardon me but what is a VLA? lol

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks, msh. I had those two concepts, entwined.

    Now, I'm depressed!

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. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM