Solved

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

Posted on 2004-03-22
5
198 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

785 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