Division (C language) | Bytes (2024)

Home Posts Topics Members FAQ

bearchen

Division (C language) | Bytes (1) 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

Subscribe Reply

2 Division (C language) | Bytes (2) 3283 Division (C language) | Bytes (3)

donbock

2,426 Division (C language) | Bytes (4) 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

reply

bearchen

2 Division (C language) | Bytes (5) 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
Division (C language) | Bytes (6) primos.jpg (13.9 KB, 404 views)

Sep 5 '09 #3

reply

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

Similar topics

19 5171

negative integer division

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 amazing, modulos of negative number give negative values! (in c). from an algebraic point of view, python seems right, but I thought python conformity to the underlying c compiler was a strong commitment, obviously not here, and I found no...

Python

24 19386

Is integer division always truncated

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

.NET Framework

17 2426

Python 3000 deat !? Is true division ever coming ?

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 replacement" and would generally like 5/2 ==2.5 So, I was contemplating to default all my modules/scripts to start with "from __future__ import division" but if it is never coming (in this decade, that is) then it would be a

Python

669 26180

What is Expressiveness in a Computer Language

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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf

Python

10 3195

Assigning the result of a '/' division to an integer type causes rounding

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/3 'after assignment, x is 1, whereas a sane person would say it should be 0 Does Microsoft have a reason for this design decision? I understand that this type of rounding can reduce the overall error in long computation chains by reducing the...

Visual Basic .NET

13 7124

division problem

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 this way: unsigned int dividend : divident store low 32 bits of big integer, dividend store high 32 bits of big integer.

C / C++

31 2552

division by 7 efficiently ???

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.

Python

94 11505

division by 7 without using division operator

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 keeping a counter that kept a tab on how many times I subtracted. Later, the EA sport guy told me that of course there are can be better technique by using bit operator.

C / C++

1272

Using C as target language

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: The right shift operator (>>) of C may or may not sign extend for signed negative numbers. Since the right shift operation of Seed7 is defined as sign extending shift, I have to check if the C compiler does

C / C++

16 5308

How to replace /(division) operator

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.

C / C++

9353

Changing the language in Windows 10

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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...

Windows Server

9975

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

by: jinu1996 |last post by:

In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...

Online Marketing

1 9909

The easy way to turn off automatic updates for Windows 10/11

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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...

Windows Server

9788

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

General

8794

AI Job Threat for Devs

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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...

Career Advice

6623

Couldn’t get equations in html when convert word .docx file to html file in C#.

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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

C# / C Sharp

5384

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 3889

transfer the data from one system to another through ip address

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 we have to send another system

C# / C Sharp

3 3481

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

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

To disable or enable advertisem*nts and analytics tracking please visit the page.

Division (C language) | Bytes (2024)

References

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5434

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.