473,672 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing an int datatype into float

64 New Member
how can i chang a datatype int into float in the middle of a function that uses arrays?
my func is

Expand|Select|Wrap|Line Numbers
  1. int avrg(int x[],int y)
  2. {    
  3. int counter;
  4.  float average,sum=0;
  5. for(counter=0;counter<y;counter++)
  6. sum+=x[counter];
  7. float counter;
  8. average=sum/counter;
  9. return average;
  10. }
  11.  
in this func i want to change the counter which is int into float after the expression
sum+=x[counter] in order to get the average in correct decimal places!!!
please help ASAP!!!!
Dec 7 '07 #1
3 2438
Studlyami
464 Recognized Expert Contributor
you could just do:
Expand|Select|Wrap|Line Numbers
  1. avg = sum/ (float)counter; //casts counter into a float
  2.  
Dec 8 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
And while we are chatting so pleasantly, remember that the average of 5 and 2 is 3 and is not 3.5.

That is, there are 3 2's in 7. You can't have half a 2. Integers have no decimal portion.

The value 2 represents all values between 2 and less than 3.

Further, the laws of significant figures say that a result cannot be more accurate than the coarsest of its components. That is, starting out with 5 and 2 you cannot have 3.5 as an average unless you start with 5.0 and 3.0.

Remove the cast.

Integer aritmetic always yields integer results.

Do not mix integer and floating point. Floating point is for scientific work where extreme accuracy is not required. Integers are meant for everything else.
Dec 8 '07 #3
Alenik1989
64 New Member
And while we are chatting so pleasantly, remember that the average of 5 and 2 is 3 and is not 3.5.

That is, there are 3 2's in 7. You can't have half a 2. Integers have no decimal portion.

The value 2 represents all values between 2 and less than 3.

Further, the laws of significant figures say that a result cannot be more accurate than the coarsest of its components. That is, starting out with 5 and 2 you cannot have 3.5 as an average unless you start with 5.0 and 3.0.

Remove the cast.

Integer aritmetic always yields integer results.

Do not mix integer and floating point. Floating point is for scientific work where extreme accuracy is not required. Integers are meant for everything else.
what are u saying?
i got it
i just changed the function type from int to float that was the problem!!!!!
and i got 3.5 not 3
Dec 14 '07 #4

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

Similar topics

13
2909
by: Peter | last post by:
Can anyone tell me how to change the data type of a field in a table created with a make table query? The field is a binary and must be changed to text. alternately does anyone know how to specify the field type when running a make table query? Thanks, Sven
16
2551
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why is this happening, and how do I get rid of it? (using Dev-C++
2
12176
by: Griff | last post by:
When attempting to change the datatype for a datatable column with the following code: dtap.Columns(4).DataType = system.Type.GetType"System.String") I keep getting an error that says that I can't change the datatype because there is data in the table. However, I know there is no data in the table, as I have cleared the table and tested the results. Please help.
1
1422
by: Savas Ates | last post by:
I have a column in my table BizdekiFiyat . The datatype = float length =8 (to save money values).. It is impossible to change these attributes for some reasons. It has records like This BizdekiFiyat 110 24 29.5
7
9379
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE (nvarchar) Use: Without adding new field the user can add new properties to the companies just by adding a new property in table PROPERTY and mapping the
4
1576
by: sandyboy | last post by:
Hi All, I have a database that contains 25 tables. In these tables the character data was stored as nvarchar datatype. Since i am not using any unicode data and the space occupied by nvarchar is more , i want to change the datatype from nvarchar to varchar. I cannot do one by one since there are 25 tables and each table has atleast 10 columns of datatype nvarchar. How can i change the datatype from nvarchar to varchar in all the tables....
1
2455
by: cnixuser | last post by:
I am posting this on behalf of a friend of mine who is trying to write a simple program that calcuates pi. He is trying to get as many decimal points as possible down in the calculation ;however, no matter what data type he seems to try, the greatest level of precision he can get is 6 decimal places. Below is a copy of his source code, can someone make a suggestion as to why float is only giving him 6 decimal places of precision? Any help would...
8
1455
by: ganesh22 | last post by:
Hi, in my database i gave a datatype as float so i entered a value as 3.10 but it taking as 3.1 and also i entered as 3.999 but it will taking as 4 how its possible which datatype store the actuall value ex : if i entered as 3.999 means it will as 3.999 only again if i given as 3.10 means it will take as 3.10 only
10
6459
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class MyObject<T: ChangeObject { ... public MyObject(string name, T value)
0
8486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
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,...
0
8931
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
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...
1
8608
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,...
0
8680
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 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...
0
7446
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, 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...
0
5705
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();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.