### Help with my array

How can I get the array to repeat itself after it hits 9? So like this 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8 and so on? any hints?

 ``12345678910111213141516171819`` ``````#include using namespace std; int main() { const int arraySize = 20; int arr1[arraySize] = {0,1,2,3,4,5,6,7,8,9}; for(int i=0; i
You can try this:
 ``12345678910111213141516171819`` ``````#include using namespace std; int main() { const int arraySize = 20; int arr1[arraySize] = {0,1,2,3,4,5,6,7,8,9}; for(int i=0; i= 10) cout << arr1[i-10] << " "; else cout << arr1[i] << " "; } cout << endl; system("PAUSE"); return 0; }``````

Last edited on
If you just want to repeat the pattern without storing the repeated part, you could do something like:

 ``1234567891011121314`` ``````#include using namespace std; int main() { const unsigned patternSize = 10; const unsigned outputLen = 25 ; int pattern[patternSize] = {0,1,2,3,4,5,6,7,8,9}; for(int i=0; i

http://ideone.com/1g3mdP

If you're looking to fill an arbitrarily sized array with a repeating pattern of arbitrary length, I would suggest using std::vector.
Last edited on
 ``12345678910111213141516171819`` ``````#include using namespace std; int main() { const int arraySize = 20; int arr1[arraySize] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9}; for(int i=0; i

I copy pasted 0,1,2,3,4,5,6,7,8,9
Last edited on
Topic archived. No new replies allowed.