how can i chang a datatype int into float in the middle of a function that uses arrays?
my func is -
int avrg(int x[],int y)
-
{
-
int counter;
-
float average,sum=0;
-
for(counter=0;counter<y;counter++)
-
sum+=x[counter];
-
float counter;
-
average=sum/counter;
-
return average;
-
}
-
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!!!!
3 2438
you could just do: -
avg = sum/ (float)counter; //casts counter into a float
-
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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++
|
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.
|
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
|
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
| |
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....
|
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...
|
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
|
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)
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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();...
| |
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...
| |