-8

How to print two digits after decimal point in cpp:

#include<iostream>  

using namespace std; 

int main() 
{ 
    int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 
    cout<<"Enter total number of processes(maximum 20):"; 
    cin>>n; 
    cout<<"\nEnter Process Burst Time\n"; 
    for(i=0;i<n;i++) 
    { 
        cout<<"P["<<i+1<<"]:"; 
        cin>>bt[i]; 
    } 
    wt[0]=0;    //waiting time for first process is 0 
    //calculating waiting time 
    for(i=1;i<n;i++) 
    { 
        wt[i]=0; 
         for(j=0;j<i;j++) 
             wt[i]+=bt[j];
    } 
    cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time"; 
    //calculating turnaround time 
    for(i=0;i<n;i++)
    { 
         tat[i]=bt[i]+wt[i]; 
         avwt+=wt[i]; 
         avtat+=tat[i]; 
         cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i]; 
    } 
    avwt/=i; 
    avtat/=i; 
    cout<<"\n\nAverage Waiting Time:"<<avwt; 
    cout<<"\nAverage Turnaround Time:"<<avtat; 
    return 0; 
 } 
Sadique
  • 22,572
  • 7
  • 65
  • 91
shubham
  • 9
  • 5

2 Answers2

2

Use setprecision:

std::cout << std::setprecision(3) << std::fixed ;
std::cout << avwt;
Sadique
  • 22,572
  • 7
  • 65
  • 91
2

you can use printf

printf("%.2f", avwt);

note that you can also specify a number of digits to be printed before the .

Chris Maes
  • 35,025
  • 12
  • 111
  • 136