GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Sep-2004, 05:21
cIdiot cIdiot is offline
New Member
 
Join Date: Apr 2004
Posts: 4
cIdiot is on a distinguished road

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:
int array[10];           
for (j = 1 ; j <= 10 ; j++)
{
   cout << array[j] << endl;
}


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  
Old 06-Sep-2004, 08:18
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Something like this?

CPP / C++ / C Code:
	int a[12];
	int old = 0;

	for (int i = 0; i < 12; i++)
		a[i] = i;

	for (int it = 3; it <= 12; it += 3)
	{
		for(int j = old; j < it; j++)
			cout << a[j] << " ";
		cout << endl;
		getchar();
		old = it;
	}
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 06-Sep-2004, 09:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by LuciWiz
Something like this?

CPP / C++ / C Code:
	int a[12];
	int old = 0;

	for (int i = 0; i < 12; i++)
		a[i] = i;

	for (int it = 3; it <= 12; it += 3)
	{
		for(int j = old; j < it; j++)
			cout << a[j] << " ";
		cout << endl;
		getchar();
		old = it;
	}


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:
#include <iostream>

using std::cout;
using std::endl;

int main()
{
  cout << "Simple investigation of the modulo operator:" << endl << endl;
  for (int i = 0; i < 12; i++) {
    cout << i << " % 3 = " << i % 3 << endl;
  }
  return 0;
}

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  
Old 06-Sep-2004, 22:31
cIdiot cIdiot is offline
New Member
 
Join Date: Apr 2004
Posts: 4
cIdiot is on a distinguished road
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:
const pageSize = 3;
int array[10]; 
char key;          
for (j = 1 ; j <= 10 ; j++)
{
   cout << array[j] << endl;

   if ( j % pageSize == 0 )
   cin >> key;
}


Regards,
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
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

All times are GMT -6. The time now is 16:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.