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, 6:28 PM   #21
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

how do I get it to index where the values are located?
Simplesouljah is offline   Reply With Quote
Old Dec 4th, 2007, 6:44 PM   #22
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
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
my outfile should print

The maximun value is ---- found at array ----
The minimum value is ---- found ar array ----
There are --- values---

I'm not understanding what I'm doing wrong....
what do I use to read the input infile?
I think your program is already doing that.

Quote:
Originally Posted by Simplesouljah View Post
how do I get it to index where the values are located?
Don't understand that question.
Ancient Dragon is offline   Reply With Quote
Old Dec 4th, 2007, 6:50 PM   #23
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: read integers from file into an array

Quote:
Originally Posted by Simplesouljah View Post
how do I get it to index where the values are located?
Instead of defining min and max as the value, define it as each index. Then refer to the value by indexing the array, and refer to the index as your min/max.

    ...

    if( myArray[i] > myArray[max])         
        max = i;     

    else if( myArray[i] < myArray[min])         
        min = i;  

    ++i;
}     

outFile << "The maximum value is " << myArray[max] << " found at array index position " << max << "." << endl;
Sane is offline   Reply With Quote
Old Dec 4th, 2007, 7:04 PM   #24
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: read integers from file into an array

Listen to Sane regarding using the index. You don't need to track the maximum value. You only need to track its position within the array, and this allows you to get the value from the array, since you know where it's stored.

Anyways, while other people were replying to you, I whipped up this. It's exactly what you need, but don't just use this for your homework. I'm doing it so you can see how it's done, and I'm only posting code because you've shown you're trying, but if you hand in my work rather than understanding it enough to do it yourself, it'll only hurt you in the long run.
c++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main(void)
  6. {
  7. std::ifstream inFile;
  8. std::ofstream outFile;
  9. int myArray[100],
  10. maximumValuePosition = 0,
  11. minimumValuePosition = 0,
  12. count = 0;
  13.  
  14. inFile.open("inputNums.txt");
  15. if(!inFile) // test if stream is valid, ie is file open?
  16. {
  17. std::cerr << "Unable to open input file, aborting..."
  18. << std::endl;
  19. return 1;
  20. }
  21.  
  22. outFile.open("finalResults.txt");
  23. if(!outFile)
  24. {
  25. std::cerr << "Unable to open output file, aborting..."
  26. << std::endl;
  27. inFile.close();
  28. return 2;
  29. }
  30.  
  31. while((count < 100) && (inFile >> myArray[count]))
  32. ++count;
  33.  
  34. for(int x=0; x<count; ++x)
  35. {
  36. if(myArray[x] > myArray[maximumValuePosition])
  37. maximumValuePosition = x;
  38. if(myArray[x] < myArray[minimumValuePosition])
  39. minimumValuePosition = x;
  40. }
  41.  
  42. if(count == 0)
  43. {
  44. outFile << "There were no values in the input file." << std::endl;
  45. }
  46. else
  47. {
  48. outFile << "The maximum value is "
  49. << myArray[maximumValuePosition]
  50. << ", found at array index " << maximumValuePosition
  51. << ".\n";
  52. outFile << "The minimum value is "
  53. << myArray[minimumValuePosition]
  54. << ", found at array index " << minimumValuePosition
  55. << ".\n";
  56. outFile << "There are " << count << " values." << std::endl;
  57. }
  58.  
  59. inFile.close();
  60. outFile.close();
  61. return 0;
  62. }
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Dec 4th, 2007, 7:07 PM   #25
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: read integers from file into an array

Quote:
It's exactly what you need, but don't just use this for your homework.
Heh. Yeah right.

He'll look to make sure he fully understands it, then echo it into his course's dropbox.

But it's not my problem.

P.S. Your code tag really glitched out on my side for a second. The wrapping was broken, the font was different, and the background was white. Not sure what that was about.
Sane is offline   Reply With Quote
Old Dec 4th, 2007, 7:19 PM   #26
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: read integers from file into an array

Quote:
Originally Posted by Sane
Heh. Yeah right.

He'll look to make sure he fully understands it, then echo it into his course's dropbox.
Yeah, probably. But like you say, that's his problem.

I'd rather he ask questions if he doesn't understand, but hey- it's his grade.
Quote:
Originally Posted by Sane
P.S. Your code tag really glitched out on my side for a second. The wrapping was broken, the font was different, and the background was white. Not sure what that was about.
Yeah, I got that too, when I first posted, so I fixed it really quick. I also got it when I edited the code a bit (changed the output from 'x values in the file' to 'x values', as it won't read the entire file if there are more values than elements in the array). I think it's something with the context-highlighted code tags; if you use one of them, you'll notice after you preview your code, the edit box has the code, but not the start/end code tags. Weird.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Dec 4th, 2007, 8:37 PM   #27
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

thanks for all your comments, yes I want a good grade, and I think I understand the help you've all given me. I will post my new code in a few...please let me know if I've understood all the advice that was given...

thank you all!!!!
Simplesouljah is offline   Reply With Quote
Old Jan 24th, 2008, 1:08 PM   #28
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

how do i post a new thread?
Simplesouljah is offline   Reply With Quote
Old Jan 24th, 2008, 1:11 PM   #29
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: read integers from file into an array

Quote:
Originally Posted by Simplesouljah View Post
how do i post a new thread?


That button is visible at the top of every forum index. The one I included in this post is from the C++ forum.
Sane 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 2:34 AM.

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