Multiplying an array by 10

closed account (zqMDizwU)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>

using namespace std;

int myArr[1];

int main(){

	for (int j = 0; j < 10000; j++)
	{
		myArr[j] = j + 1;
		cout << myArr[j] << "\n"; //or cout <<myArr[j] << " ";

		system("pause"); //or system("pause>nul"); 
	}
}


I am trying to initialize int x[] = {10, 100, 1000, 10000} into a loop and give me four different numbers for something. I was trying to get creative and just do myArr[j] = j * 10; with (int j = 1; j < 10000; j++) , but that doesn't work. It still gives me an output of 10 11 12... Does anybody know how to make an array punch through a loop and give me 10 Press any key to continue... 100 Press any key to continue... 1000 Press any key to continue... 10000 Press any key to continue.... I'm thinking I must set the for loop to < 10000.1 or < 10001 to output the final value in the array.
myArr has a single element. The only valid index is 0.

1
2
3
4
5
6
7
8
9
10
int main()
{
    const unsigned myArrSize = 4 ;
    int myArr[myArrSize] ;

    const unsigned factor = 10 ;
    unsigned value = 1 ;
    for ( unsigned i=0; i<myArrSize; ++i )
        myArr[i] = (value *= factor) ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>
using namespace std;

int main() {

    int myArr[8] = {10}; // only first value set to 10, rest are 0
    for (int j = 1; j < 8; j++)
    {
        myArr[j] = myArr[j - 1] * 10;
	cout << myArr[j] << "\n";
        
        cin.get();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return 0;
}


Loop only goes to 8 because that is the max power of 10 you can represent with int type
closed account (zqMDizwU)
Ok thank you so much Smac89! Job well done.
Last edited on
Topic archived. No new replies allowed.