Solved

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

Posted on 2004-03-22
5
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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