Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   read integers from file into an array (http://www.programmingforums.org/showthread.php?t=14684)

Simplesouljah Dec 4th, 2007 5:09 PM

read integers from file into an array
 
having a problem with a program not sure what I'm doing wrong. I to write this program that
inputs integer data from a file into an array of size 100. We have to create a
variable to keep track of how many numbers were read into the array, then
determine where in the array the highest and lowest value reside.

here's what I have so far...
:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int i, max, min, maxNumber, minNumber;
  11.     string myArray;
  12.     max = 0;
  13.     min = 0;
  14.  
  15.     ifstream inFile;
  16.     ofstream outFile;
  17.  
  18.     inFile.open ("inputNums.txt");
  19.     outFile.open ("finalResults.txt");
  20.  
  21.     inFile >> myArray;
  22.  
  23.     for (i = 0; i <= 100; i++)
  24.         {  inFile >> myArray[i];
  25.  
  26.  
  27.     outFile << "The maximum value is " << max << " found at array index position" << endl;
  28.     outFile << endl;
  29.  
  30.     outFile << "The minimun value is " << min << "found at array index position" << endl;
  31.     outFile << endl;
  32.  
  33.     outFile << "There are  " << endl;
  34.     outFile << endl;
  35.  
  36.     outFile << "Thank You!" << endl;
  37.  
  38.     inFile.close();
  39.     outFile.close();
  40. } 
  41.  
  42.  
  43. system ("pause");
  44. return 0;
  45. }


Ancient Dragon Dec 4th, 2007 5:28 PM

Re: read integers from file into an array
 
line 11:
myArray is not an int array. It needs to be declared like this:
:

int myArray[100] = {0};

line 23: do not make the assumption that the file contains 100 integers -- maybe it only contains 10, maybe it contains a million. The idea is to read the file until end-of-file is reached or your array is filled up, whichever somes forst. So the way to code that loop is like this:
:

while( inFile >> myArray[i])
    ++i;


max. To find the max value, just keep track of the maximum value as they are being read from the file
:

int max = -1000;
int i = 0;
while( inFile >> myArray[i])
{
    if( myArray[i] > max)
        max = myArray[i];
    ++i;
}


Simplesouljah Dec 4th, 2007 5:38 PM

Re: read integers from file into an array
 
what about the min value? do i use the else if statement?

Simplesouljah Dec 4th, 2007 5:39 PM

Re: read integers from file into an array
 
:

  1. , max = min;
  2.         for(i = 1; i < size; ++i)
  3.         {
  4.                 if (nums[i] < min)
  5.                         min = nums[i];
  6.                 else if (nums[i] > max)
  7.                         max = nums[i];
  8.         }

can I do it like this?

Ancient Dragon Dec 4th, 2007 5:42 PM

Re: read integers from file into an array
 
>>do i use the else if statement
No, just use another variable and statement like I did for the max value. I wouldn't use the else statement because maybe the current value just read affects both min and max.
:

int max = 0;
int min = 0;
int i = 0;
while( inFile >> myArray[i])
{
    if( i == 0) // first time throug
    {
        max = min = myArray[i];
    }
    if( myArray[i] > max)
        max = myArray[i];
    if( myArray[i] < min)
        min = myArray[i];
    ++i;
}


And you forgot to add code tags

Simplesouljah Dec 4th, 2007 5:46 PM

Re: read integers from file into an array
 
I'm sorry about the code tags...I'm a newbie, still learning

Simplesouljah Dec 4th, 2007 5:52 PM

Re: read integers from file into an array
 
I'm sorry.....I am so lost....do I need lines 10-13? and where would I place the
:

int max = [-1000];
int i = [0];
while( inFile >> myArray[i])
{
    if( myArray[i] > max)
        max = myArray[i];
    ++i;
}


also what is the code tags?

Ancient Dragon Dec 4th, 2007 6:08 PM

Re: read integers from file into an array
 
>>do I need lines 10-13?
yes. But you need to change line 11 as I said previously.

>>where would I place this
replace lines 23 and 24 with the lines that were in my most post #5

>>also what is the code tags?
Read your PM that I sent you. It shows you how to do it.

Simplesouljah Dec 4th, 2007 6:14 PM

Re: read integers from file into an array
 
didn't get the PM?

Simplesouljah Dec 4th, 2007 6:18 PM

Re: read integers from file into an array
 
I treid using your post #2 about line 11 and I keep getting a error saying invalid initializer...


All times are GMT -5. The time now is 3:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC