![]() |
|
#1
|
|||
|
|||
Page through an Array Help !How can I page through an Array in C++, say with 10 elements and I want to display only first 3 elements, then when I hit ENTER, the next 3 elements will be displayed and so on till the last element of the Array is reached.
For example: CPP / C++ / C Code:
Thanks in advance. Last edited by JdS : 06-Sep-2004 at 08:37.
Reason: Please insert your example C/C++ codes between [c] and [/c] tags
|
|
#2
|
||||
|
||||
|
Something like this?
CPP / C++ / C Code:
__________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
|
Quote:
How would you handle the case where there are 10 elements (as suggested in the original post)? Or any case where the number of elements in the array is not an exact multiple of 3? Think about a single loop: print the values one at a time. Put a space after each value, or format the output any way you want. The first line has a[0] a[1] a[2] Then you want to put out an endl, and let the user press Enter. so the second line has a[3] a[4] a[5] Etc. (You exit the loop when you have printed all of the numbers in the array.) How do you know when to end a line and let the user press Enter? Well, consider the modulo operator (%). Did you wonder why they would put something this bizarre in the C language (carried over to C++)? Well we are doing things three at a time. Here's where you use the modulo operator. Try the following: CPP / C++ / C Code:
Now in your program you want to end the line when i = 2, i = 5, i = 8, etc. What is the value of i % 3 when i = 2, i = 5, etc.? Good Luck and Best Regards, Dave Last edited by davekw7x : 06-Sep-2004 at 10:14.
|
|
#4
|
|||
|
|||
|
Thanks guys, that's was quick and well explained
The modulo method was so helpful. It's much easier than I thought before. Just simply adding an IF statement inside the loop to check the remainder. My final version is: CPP / C++ / C Code:
Regards, |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Speed up C++ code about 3d array! | Truong Son | CPP / C++ Forum | 0 | 16-Mar-2004 21:52 |
| c: array comparison | jack | C Programming Language | 7 | 26-Jan-2004 11:21 |
| Extra null element in an array | samtediou | MySQL / PHP Forum | 2 | 11-Dec-2003 11:52 |
| [script] E-mail webmaster error page | BobbyDouglas | PHP Code Library | 0 | 19-Aug-2003 20:10 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The