double

when do we use double,,,
like there is a program to read prices of 20 items in array and find there sum of all the pricwes, product of all the irems and the average of them:
#include<iostream.h>
void main()
{ double Price[20], sum, avg, prod;
sum=avg=0;
prod=1
for(int i=0;i<20;++i)
{ cout<<"enter price of the item"<<i+1<<";";
cin>>Price[i];
sum +=Price[i];
prod *=Price[i];
}
avg=sum/20;
cout<<"sum of the all prices:":<<sum<<endl;
cout<<"product of all prices="<<prod<<endl;
cout<<"average of all prices="<<avg<<endl;
}
Q.please explain me this program and tell me when to use double and when to use i++ and ++i??
double is a type of how much memory is allocated for that variable and stores numbers with a decimal point. ++ / -- is post and pre operators they are used to either increase or decrease an operand its applied on.
That's a lot of duplicate threads. ;)

First, your code will not compile, for the simple reason that you're not using /*the*/ namespace /*called*/ std;, so you'll have a few scope errors.

Second, use double when you need a greater amount of precision than float can offer you.

Finally, for smaller basic types (like int), it doesn't matter too much whether you use i++ or ++i, however when you deal with larger more complex objects (like some classes), it makes more sense to use ++i, because it will improve performance.

Does this help?

-Albatross
closed account (4Gb4jE8b)
for when to use double,
http://www.cplusplus.com/doc/tutorial/variables/
(see range of fundamental data types)

for when to use i++ or ++i
http://www.cplusplus.com/doc/tutorial/operators/
(see increase and decrease)

don't post your topic more than once.

the program does exactly what you said using an array of doubles, for arrays see
http://www.cplusplus.com/doc/tutorial/arrays/
Topic archived. No new replies allowed.