473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Division with arrays?

How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.
Nov 14 '05 #1
9 12721
mj***@pf.pl (Marcin) writes:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


First, define what you mean by division for arrays. The most obvious
meaning result would be an array consisting of the result of dividing
each element in turn (a[0]/b[0], a[1]/b[1], ...), but your arrays are
of different lengths (and the 0's in b would cause problems).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2

Marcin wrote:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2}; short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};
short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


You have to manually do this (go through pairs of elements - one in
each array). You're arrays are of different sizes, so your result
array will be the size of the shorter array (and division by zero has
to be handled).

example (assuming your declarations for array 'a' and 'b' are done):

..double *result;
..size_t i;

..size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);

..result = malloc(sizeof(*result) * limit);

/* loop below divides elements of 'a' by 'b' places into result */
..for(i = 0; i < limit; i++)
..{
.. if(b[0] = 0)
.. result[0] = 123456789.123456789; /* handle divide by 0 somehow
*/
.. else
.. result[0]= (double)a[0]/b[0];
..}

At this point, the division is done, and you have a dynamically
allocated buffer (which you can reference like an array) result, which
holds the division results for each element. You have to handle
division by zero somehow, possible choosing some value that can't be
the result of your divisions.

When you're done with the result array you have to free the buffer
using:

free(result);

Otherwise, you have a memory leak.

Nov 14 '05 #3

Kobu wrote:
Marcin wrote:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] =

{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


You have to manually do this (go through pairs of elements - one in
each array). You're arrays are of different sizes, so your result
array will be the size of the shorter array (and division by zero has
to be handled).

example (assuming your declarations for array 'a' and 'b' are done):

.double *result;
.size_t i;

.size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);

.result = malloc(sizeof(*result) * limit);

/* loop below divides elements of 'a' by 'b' places into result */
.for(i = 0; i < limit; i++)
.{
. if(b[0] = 0)
. result[0] = 123456789.123456789; /* handle divide by 0

somehow */
. else
. result[0]= (double)a[0]/b[0];
.}
all the subscripts within the loop should be i, not 0

At this point, the division is done, and you have a dynamically
allocated buffer (which you can reference like an array) result, which holds the division results for each element. You have to handle
division by zero somehow, possible choosing some value that can't be
the result of your divisions.

When you're done with the result array you have to free the buffer
using:

free(result);

Otherwise, you have a memory leak.


Nov 14 '05 #4

Kobu wrote:
Kobu wrote:
Marcin wrote:
How I can make division of two numbers placed in arrays, example:

short int a[] =

{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] =

{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


You have to manually do this (go through pairs of elements - one in
each array). You're arrays are of different sizes, so your result
array will be the size of the shorter array (and division by zero has to be handled).

example (assuming your declarations for array 'a' and 'b' are done):
.double *result;
.size_t i;

.size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);
the expression to the right of the = operator evaluates to a compile
time constant, which you can use to statically or automatically
allocate a result array (if you wanted to)

.result = malloc(sizeof(*result) * limit);

/* loop below divides elements of 'a' by 'b' places into result */
.for(i = 0; i < limit; i++)
.{
. if(b[0] = 0)
. result[0] = 123456789.123456789; /* handle divide by 0

somehow
*/
. else
. result[0]= (double)a[0]/b[0];
.}


all the subscripts within the loop should be i, not 0

At this point, the division is done, and you have a dynamically
allocated buffer (which you can reference like an array) result,

which
holds the division results for each element. You have to handle
division by zero somehow, possible choosing some value that can't be the result of your divisions.

When you're done with the result array you have to free the buffer
using:

free(result);

Otherwise, you have a memory leak.


Nov 14 '05 #5
On Tue, 01 Feb 2005 22:12:21 +0000, Keith Thompson wrote:
mj***@pf.pl (Marcin) writes:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


First, define what you mean by division for arrays. The most obvious
meaning result would be an array consisting of the result of dividing
each element in turn (a[0]/b[0], a[1]/b[1], ...), but your arrays are
of different lengths (and the 0's in b would cause problems).


The question did state that theree are 2 numbers, so presumable each array
a and b represents a number in some fashion. We don't know how but at a
guess it is probably as a sequence of decimal digits. In which case some
form of decimal long division looks appropriate.

Lawrence

Nov 14 '05 #6
On Tue, 01 Feb 2005 15:52:06 -0800, Kobu wrote:

Marcin wrote:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] =

{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


You have to manually do this (go through pairs of elements - one in
each array). You're arrays are of different sizes, so your result
array will be the size of the shorter array (and division by zero has
to be handled).


1000 / 1 is 1000, i.e. the result is not the size of the "shorter array".
If there are no leading zeros in the numbers it would be
nelems(a)-nelems(b)+1, and if b is larger the result is trivially zero.
example (assuming your declarations for array 'a' and 'b' are done):

.double *result;
.size_t i;

.size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);


Here you are using the number of elements of the larger of the 2 arrays
which would work but isn't optimal. You might just as well use simply the
number of elements of a, the result of a valid integer division can't be
larger than the dividend.

Lawrence

Nov 14 '05 #7

Lawrence Kirby wrote:
On Tue, 01 Feb 2005 15:52:06 -0800, Kobu wrote:

Marcin wrote:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] =

{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


You have to manually do this (go through pairs of elements - one in
each array). You're arrays are of different sizes, so your result
array will be the size of the shorter array (and division by zero has to be handled).


1000 / 1 is 1000, i.e. the result is not the size of the "shorter

array". If there are no leading zeros in the numbers it would be
nelems(a)-nelems(b)+1, and if b is larger the result is trivially zero.

by size I mean: sizeof(array)/sizeof(*array)
I think I misunderstood the question, thinking that each array held a
separate number in each position. I now see what he probably meant.
example (assuming your declarations for array 'a' and 'b' are done):
.double *result;
.size_t i;

.size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);

should be < not > (under my original assumption)

Here you are using the number of elements of the larger of the 2 arrays which would work but isn't optimal. You might just as well use simply the number of elements of a, the result of a valid integer division can't be larger than the dividend.
yeap, good point

Lawrence


Nov 14 '05 #8
On 1 Feb 2005 13:57:28 -0800, mj***@pf.pl (Marcin) wrote:

How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division


Is b's value 10000000000000000000000002 or 20000000000000000000000001 ?
If it's the latter, the problem is very simple. If not, it's very
slightly more complicated.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #9
Lawrence Kirby <lk****@netactive.co.uk> writes:
On Tue, 01 Feb 2005 22:12:21 +0000, Keith Thompson wrote:
mj***@pf.pl (Marcin) writes:
How I can make division of two numbers placed in arrays, example:

short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,2};
short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,2};

short int result[] = a / b; 'division

without additional lib's and header files, only in standart C.


First, define what you mean by division for arrays. The most obvious
meaning result would be an array consisting of the result of dividing
each element in turn (a[0]/b[0], a[1]/b[1], ...), but your arrays are
of different lengths (and the 0's in b would cause problems).


The question did state that theree are 2 numbers, so presumable each array
a and b represents a number in some fashion. We don't know how but at a
guess it is probably as a sequence of decimal digits. In which case some
form of decimal long division looks appropriate.


Ok, that's probably what the OP meant, but he needs to clarify it. Do
the arrays represent sequences of decimal digits? In what order? How
are negative values represented (are only non-negative values
representable).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: joel | last post by:
I have a table which I want to update by dividing one field into another. The update runs with no errors, but the results come out as only a positive integer number. The datatype for the result...
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
2
by: kermit | last post by:
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the...
13
by: jamesonang | last post by:
Supposed unsigned int(32 bits) is the largest number that computer can represent with a single variable. Now, i have a big integer ( less than 64 bit, but great than 32 bit) . i represent it by...
31
by: krypto.wizard | last post by:
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.
4
by: aditya sriram | last post by:
hello, i have been trying to work on a division algorithm for numbers which have digits greater than 8. The solution i came up with was using character arrays and succesive subtraction but...
6
by: nigelmercier | last post by:
I'm still not comfortable with passing character arrays, and I think this is the problem with this code: char * formatHours(int decimin) { // convert integer deci-mins (6 seconds) to string:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.