Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now