![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
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)
Last edited by Ancient Dragon; Dec 4th, 2007 at 4:24 PM. Reason: add code tags |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: read integers from file into an array
what about the min value? do i use the else if statement?
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: read integers from file into an array
c++' int i, min = nums[0 Syntax (Toggle Plain Text)
Last edited by Ancient Dragon; Dec 4th, 2007 at 4:43 PM. Reason: you forgot the code tags -- for the second time. |
|
|
|
|
|
#5 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: read integers from file into an array
I'm sorry about the code tags...I'm a newbie, still learning
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#8 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Oct 2007
Posts: 28
Rep Power: 0
![]() |
Re: read integers from file into an array
didn't get the PM?
|
|
|
|
|
|
#10 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4
![]() |
Re: read integers from file into an array
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |