#include<fstream.h>
#include<iomanip.h>
#include<iostream.h>
using namespace std;
ifstream f("atestat.in");
ofstream g("atestat.out");
int n,i,nr=0;
float v[100];
void a(int n)
{
for(i=1;i<=n;i++)
f>>v[i];
for(i=1;i<=n;i++)
cout<<v[i]<<" ";
}
int main()
{
f>>n;
a(n);
cout<<endl;
float s=0;
for(i=1;i<=n;i++)
{
if(v[i]<0)
{
s=s+v[i];
nr++;
}
}
cout<<setw(2)<<s/nr<<endl;
}
my "atestat.in" file contains: 6 -56.765 2.3 4.56 -1.2 -1.8 3
The program first displays all the numbers on the second line of the "atestat.in" file, through the use of an array, and then it is supposed to display the arithmetic mean of all the negative numbers inside that array, with a precision of 2 numbers after the decimal mark. For some reason, setw(2) does nothing at all, as my cout<<setw(2)<<s/nr<<endl;
displays "19.9217" instead of "19.92"...could anyone tell me why? Am i using it improperly somehow?