![]() |
|
#1
|
|||
|
|||
Keep track of the last index usedHow do you keep track of the last index use in two vectors/arrays.
For example: CPP / C++ / C Code:
Last edited by admin : 12-Aug-2012 at 04:12.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Keep track of the last index usedQuote:
I mean, couldn't you just create an int variable with a human-readable significant name for each array (lastReadFirstArray or lastReadSecArray or whatever...) Initialize each to a value of -1. You can read from an array by incrementing the corresponding bookkeeping variable (before reading) and making sure it won't take you beyond the end of the array. Or am I missing something? (It wouldn't be the first time.) Regards, Dave Footnote: Alternatively, your bookeeping variable could indicate the next available index. Initialize it to zero and increment it each time after you read from the array. You should check each time (before you read) to make sure it won't be reading beyond the end of the array. __________________
Sometimes I just can't help myself... Last edited by davekw7x : 12-Aug-2012 at 09:47.
|
|
#3
|
|||
|
|||
Re: Keep track of the last index usedHere is my code so far:
CPP / C++ / C Code:
Output from code above Code:
Can you point out from my code where I went wrong? Last edited by admin : 13-Aug-2012 at 01:58.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#4
|
|||
|
|||
Re: Keep track of the last index usedSo I changed to get rid of the segmentation fault but it's still giving me the wrong output
Code:
Output: Code:
|
|
#5
|
|||
|
|||
Re: Keep track of the last index usedQuote:
Quote:
Here's a way to get to the bottom of things: Put meaningful, labeled debugging print statements at each point in the program where things happen. Maybe something like: CPP / C++ / C Code:
Or some such thing. Regards, Dave __________________
Sometimes I just can't help myself... |
|
#6
|
|||
|
|||
Re: Keep track of the last index usedHere's my test program:
Example: read.txt contains 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 1 CPP / C++ / C Code:
MAIN CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Keep track of the last index usedQuote:
I hate to repeat myself but: Put print statements at strategic points in the program so that you can see what the program is seeing: For starters, CPP / C++ / C Code:
I mean, you call the function four times. Each time you open the file and read its values so that the zero and one vectors are reinitialized each time. Right? Regards, Dave Footnotes about style: You have hard-coded values of the sums of the elements. In addition to making it hard to maintain, you actually have a wrong value for one of them. Let the program calculate the sums for you. Really. You have also hard-coded the size of the arrays inside the function. What if you want to change the size of one or more of the arrays? Always pass size of arrays as arguments into functions that need to know the size. In general, it might be better to use vectors (or some other data structure that carries the size of the object with it) instead of arrays. But if you do use arrays (and it's usually OK to use arrays in cases like this), be sure to pass the size to functions that need to know. But I said that already. Your method of calculating average after replacing array values with random values is wrong. Either calculate a new average after replacement or recalculate the average of fewer elements based on the elements that are going to be replaced or whatever the heck it is that that number is supposed to represent. __________________
Sometimes I just can't help myself... |
|
#8
|
|||||
|
|||||
Re: Keep track of the last index usedQuote:
that's why I gave my test program Quote:
Quote:
Quote:
but thanks you for the advice Quote:
I'm not calculating the average after. I'm calculating it before (or maybe I misunderstood something? Please feel free to correct me if ever). And I manually calculated to make sure the result of the average is correct. and thanks for your suggestions I will try them |
|
#9
|
|||
|
|||
Re: Keep track of the last index usedQuote:
Quote:
Quote:
My key points are in the text above the footnotes: Put in print statements that are verbose enough to tell you what the program is seeing at points where it could be coming up with incorrect results. Regards, Dave __________________
Sometimes I just can't help myself... |
|
#10
|
|||
|
|||
Re: Keep track of the last index usedalready got it
|
Recent GIDBlog
Match IP in CIDR by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NetBeans - Inventory Program | biglez123 | Java Forum | 1 | 23-Jan-2011 19:48 |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 15:41 |
| Having a problem | Chuckles | Computer Hardware Forum | 19 | 13-Sep-2004 12:17 |
| hashing help | saiz66 | C++ Forum | 1 | 06-Jul-2004 06:16 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The