473,472 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Division (C language)

2 New Member
Hello

I'm dividing consecutive prime numbers (larger over lesser) and I get 1.0000(....) for all of them.
Does it have anything to do with runnig gcc on windows? I've tried the calculator applet that comes with the OS and it works properly...
The program prints a column with the order, a second with each prime, then the difference between two consecutives, the cumulative sum and the last one was supposed to be the ratio between two consecutive primes.
Here's part of the code:

(main)

(........)
for ( m = 0; m <= MAX_DIM; m++ ){
if ( vect_primos[ m ] != 0 && vect_primos[ m ] < 10000 )
printf ( "%6d%8d%12d%12d%17.6lf\n", ( m + 1 ), vect_primos[ m ], ( vect_primos[ m ] - vect_primos[ m - 1 ] ), cum_sum( &vect_primos[ m ] ), divisao( &vect_primos[ m ] ) );

}
return 0;
}

------------------------------------------
int cum_sum( int v[ ]){

int i;

for ( i = 0; i <= MAX_DIM; i++ ){
acumulado[ i ] = acumulado[ i ] + v[ i ];
return acumulado[ i ];
}
return 0;
}

----------------------------------------------------------
double divisao( int vec[ ] ){

int i;

for ( i = 0; i <= MAX_DIM; i++ ){
div[ i ] = vec[ i + 1 ] / vec[ i ];
return div[ i ];
}
return 0;
}


Any suggestions?

Many thanks in advance.
Sep 5 '09 #1
2 3277
donbock
2,426 Recognized Expert Top Contributor
@bearchen
For example I'll use consecutive primes 5 and 3, the value of 5/3 is different than 5.0/3.0. The first expression uses integer division; the second uses floating-point arithmetic.

By the way, function divisao references the passed array element and the next one. Any time you infer the address of a parameter like this you need to be extra careful that your loop-termination criteria stops at the right time. You don't want to pass divisao the address of the last item in the array.
Sep 5 '09 #2
bearchen
2 New Member
Thanks, I keep forgetting these basic principles. I promoted the array vec[ ] to float and now it works.
After some modifications and patches, here's the end result:
Attached Images
File Type: jpg primos.jpg (13.9 KB, 404 views)
Sep 5 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
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...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
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 =...
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.
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
0
by: thomas.mertes | last post by:
Generally C is well suited as target language for compilers. I use C as target language for the Seed7 compiler. This works good. There are just some things where the behaviour of C is undefined: ...
16
by: spl | last post by:
To increase the performance, how to change the / operator with bitwise operators? for ex: 25/5, 225/25 or 25/3 or any division, but I am not bothered of any remainder.
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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 projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.