473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Arrays

6
Hey guys hipe you all doing good, I have a program that reads some numbers in a file, it needs to find the highest number, the lowst number, sum of numbers, and teh avferasge. I have coded the follwoing but it is not having the best reslutls. It finds the highest, average and sum but for lowest alwyas shows the first number in the file

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5. int main ()
  6. {
  7.     {
  8.          ifstream inputFile;
  9.          char fileName[20];
  10.          cout << "Enter the filename  ";
  11.          inputFile.open("numbers.txt");
  12.          cout<<"\n";
  13.          const int SIZE=100;
  14.          int numbers [SIZE];
  15.          int count =0;
  16.          while (count<SIZE && inputFile>>numbers[count])
  17.          count++;
  18.          cout<<setprecision(2)<<fixed<<showpoint<<endl;
  19.          cout<<"\nThese are all of the numbers in the file: ";
  20.          for(int index=0;index<count;index++)
  21.          cout<<numbers[index]<<" "<<endl;
  22.          inputFile.close();
  23.     }
  24.     int count_highest=0;
  25.     int greatest=0;
  26.     const int SIZE=100;
  27.     int numbers [SIZE];
  28.     greatest=numbers[0];
  29.     ifstream inputFile;
  30.     inputFile.open("numbers.txt");
  31.     while (count_highest<SIZE && inputFile>>numbers[SIZE])
  32.  
  33.     {
  34.                  count_highest++;            
  35.                  if(numbers[count_highest]>greatest)
  36.                  greatest=numbers[count_highest];
  37.     }
  38.     inputFile.close();
  39.     cout<<"\nThe highest number is: "<<greatest<<endl; 
  40.     inputFile.open("numbers.txt"); 
  41.     int count_lowest=0;
  42.     int lowest;
  43.     lowest = numbers[0];
  44.     while (count_lowest<SIZE && inputFile>>numbers[SIZE])
  45.     {
  46.          count_lowest++;
  47.          if (numbers[count_lowest] < lowest)
  48.          lowest = numbers[count_lowest];     
  49.     }
  50.     cout<<"The lowest numbers is: "<<lowest<<endl;
  51.     int tnum;
  52.     double average, sum = 0;
  53.     for(tnum = 0; tnum < SIZE; tnum++)
  54.             sum += numbers[tnum];
  55.     average = sum / SIZE;
  56.  
  57.     cout<<"The sum of numbers is "<<sum<<" and the average " <<average<<endl;    
  58.     cout<<"There are total "<<tnum<<" numbers in the file."<<endl;
  59.     system("pause");
  60.     return 0;
  61. }
  62.  
let me know if yall can do anything about this
Mar 11 '07 #1
2 1275
horace1
1,510 Expert 1GB
your loop looking for greatest exits when the inputFile>>numbers[SIZE] fails, i.e. you get an error condition. If you are going to use the same ifstream again you need to clear the error condition
Expand|Select|Wrap|Line Numbers
  1.     inputFile.close();
  2.     inputFile.clear();
  3.     inputFile.open("numbers.txt"); 
  4.  
the open() does not automatically do so.
I also think your algorithm for finding greatest and lowest is not correct but you can check this with test data
Mar 11 '07 #2
une
6
the following dont have any effect in the loop, thats just where the loop takes numbers to compare
Expand|Select|Wrap|Line Numbers
  1. inputFile>>numbers[count] 
Mar 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.