Solved

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

Posted on 2004-03-22
5
202 Views
Last Modified: 2010-04-01
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"


0
Comment
Question by:Ikram_Bohra
  • 2
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 500 total points
ID: 10650697
float lSum = 0;
for ( int i = ... )
{
  ... as before

lSum += x[i];
}


float lAverage = lSum / 1056.0


( and, incidentally, x[i] where i is 0... 1055, not 1..1056 )
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10652192
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.

0
 

Author Comment

by:Ikram_Bohra
ID: 10656087
@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;
      }
 
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 10656214
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.

0
 

Author Comment

by:Ikram_Bohra
ID: 10656234
@andrewJB-----


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

thank you
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question