Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 4th, 2007, 4:09 PM   #1
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
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...
c++ Syntax (Toggle Plain Text)
  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. }

Last edited by Ancient Dragon; Dec 4th, 2007 at 4:24 PM. Reason: add code tags
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 4:28 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4 Ancient Dragon is on a distinguished road
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;
}
Ancient Dragon is offline   Reply With Quote
Old Dec 4th, 2007, 4:38 PM   #3
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Re: read integers from file into an array

what about the min value? do i use the else if statement?
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 4:39 PM   #4
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Re: read integers from file into an array

c++'
int i, min = nums[0 Syntax
(Toggle Plain Text)
  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?

Last edited by Ancient Dragon; Dec 4th, 2007 at 4:43 PM. Reason: you forgot the code tags -- for the second time.
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 4:42 PM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4 Ancient Dragon is on a distinguished road
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
Ancient Dragon is offline   Reply With Quote
Old Dec 4th, 2007, 4:46 PM   #6
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Re: read integers from file into an array

I'm sorry about the code tags...I'm a newbie, still learning
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 4:52 PM   #7
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
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?

Last edited by Ancient Dragon; Dec 4th, 2007 at 5:10 PM. Reason: Third time I added code tags
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 5:08 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Dec 4th, 2007, 5:14 PM   #9
Simplesouljah
Newbie
 
Join Date: Oct 2007
Posts: 28
Rep Power: 0 Simplesouljah is on a distinguished road
Re: read integers from file into an array

didn't get the PM?
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 5:19 PM   #10
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: read integers from file into an array

Quote:
Originally Posted by Simplesouljah View Post
didn't get the PM?
Oh, I thought you would have received one. Then read the Read Me threads at the top of this board -- I just finished adding a new one that shows how to use code tags.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
Read and write to one file nnxion C 3 Apr 11th, 2006 5:10 PM
How to read unknown total of int data from text file to a 2-dim array in C ladyscarlet99 C 2 Sep 9th, 2005 2:28 AM
How to read unknown total of int data from text file to a 2-dim array in C++? ladyscarlet99 C++ 2 Sep 9th, 2005 1:01 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:40 AM.

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