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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

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<arraySize; i++)
 {
	 arr1[i];
	 cout << arr1[i];

 }
	

	system("PAUSE");
	return 0;
}
You can try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
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<arraySize; i++)
      {
	     if(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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
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<outputLen; i++)
        cout << pattern[i%patternSize] << ' ' ;
    
    cout << endl;
}


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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

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<arraySize; i++)
 {
	 arr1[i];
	 cout << arr1[i];

 }
	

	system("PAUSE");
	return 0;
}


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