Link to home
Start Free TrialLog in
Avatar of Ikram_Bohra
Ikram_Bohra

asked on

can any body help!!!!I have bleaque knowledge of C++ ...

I require this array to output the average number ..... form a given set of numbers
heres the code which just displays without the average

#include "fstream.h"
#include <string.h>
#include<stdio.h>

int main()
{
    char filenameh[64];
   int i=0;
    char newfilename[128];
    ifstream ifshl; ifshl.open("datafiles.lis");
    float x[1056];  float y[1056];

    while(!ifshl.eof())
    {
      ifshl>>filenameh;
      

      strcpy(newfilename,filenameh);
      strcat(newfilename,".new");

      ifstream ifsh(filenameh);
      if(ifsh.bad())
      printf("Error Opening %s\n",filenameh);


      ofstream ofs(newfilename);
      if(ofs.bad())
      printf("Error opening %s\n",newfilename);



 for (int i=0; i<1056; i++)
      {
ifsh>>y[i] >> x[i];

      
      ofs << x[i] << endl;
      }

ifsh.close();
ofs.close();
}
ifshl.close();
   return 0;
}

this is what i require  :-
"I've got an array of number that I want to add
so ive got x[i] where i is 1 to 1056
so x is a different value for each value of i
need to work out the average of the numbers"


ASKER CERTIFIED SOLUTION
Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Avik Dasgupta
why r u openning and closing files several times. If u have line by line numbers, performing this may do

#include<fstream.h>

void main()
{
 ifstream fl1("datafiles.lis",ios::in);
 // check for file existance
 ofstream fl2("datafiles.new",ios::out);
 int x[1056],count=0,sum=0,avg,i=0;
 while(fl1>>x[i]){ // i don't understand the role of y[i]
  count++;
  sum=sum+x[i]; // cumulate sum
  cout << x[i] << endl;
 }
 avg=sum/count; // compute average
 cout<<avg<<endl;
 fl1.close();
 fl2.close();
}

Hope this works fine !

Avik.

Avatar of Ikram_Bohra
Ikram_Bohra

ASKER

@andrewJB-----

sorry for being a nuisance as i am not very familiar with C++ at all its just that i require is  just part of a module at University.

hers the error i get
--------------------Configuration: change2 - Win32 Debug--------------------
Compiling...
change2.cpp
C:\Documents and Settings\rdudhwal\Desktop\change2.cpp(42) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data

change2.obj - 0 error(s), 1 warning(s)

.........when i compile this

#include "fstream.h"
#include <string.h>
#include<stdio.h>

int main()
{
    char filenameh[64];
   int i=0;
    char newfilename[128];
    ifstream ifshl; ifshl.open("datafiles.lis");
    float x[1056];  float y[1056];

    while(!ifshl.eof())
    {
      ifshl>>filenameh;
      

      strcpy(newfilename,filenameh);
      strcat(newfilename,".new");

      ifstream ifsh(filenameh);
      if(ifsh.bad())
      printf("Error Opening %s\n",filenameh);


      ofstream ofs(newfilename);
      if(ofs.bad())
      printf("Error opening %s\n",newfilename);


      float lSum = 0;

      for (int i=0; i<1056; i++)
      {
            ifsh>>y[i] >> x[i];

      
            ofs << x[i] << endl;
            lSum += x[i];
      }

      float lAverage = lSum / 1056.0;


      ifsh.close();
      ofs.close();
      }
      ifshl.close();
         return 0;
      }
 
It's only a warning. I think because 1056.0 is considered a double, not a float.

So, it doesn't really matter in this case, but
float lAverage = lSum / (float)1056.0;
would sort it.

@andrewJB-----


thank the problem has now been sorted so u can ignore the last post

thank you