# Thread: Dividing array by scalar

1. ## 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. 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. 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

5. Same code lol

6. 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. 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[] ?

8. 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]);
}```

9. Lets try something else.

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

10. 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]);```

11. Originally Posted by msh
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

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. 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. First of all i have been programming for only two months now, so pardon me but what is a VLA? lol

15. Thanks, msh. I had those two concepts, entwined.

Now, I'm depressed!