In this lesson, we have a few exercises for you based on different array initializations.

For all the challenges in this lesson, use the code editor below to add the code in the respective function for each challenge. Comment all functions except for the one that you want to call in the main() function.

#include <iostream>
using namespace std;

void allZeros(int A[], int size);
void wholeNum(int A[], int size);
void naturalNum(int A[], int size);
void evenNum(int A[], int size);
void multipleOfFive(int A[], int size);
void multipleOfNum(int A[], int size,int num);
bool isPrime(int N);
void primeNum(int A[], int size);
void randomNum(int A[], int size);
void randomEven(int A[], int size);
void randomRange(int A[], int size,int start,int end);
void printArray(const char Name[], int A[], int size);

int main()
{
    const int capacity = 100;
    int arrayT[capacity], // A has all garbage values
        size, num;
    cout << "Size(Maximum of 100): ";
    cin>>size;

	// cout <<"allZeros:     {0, 0, 0, 0, 0,	...}" << endl;
    // allZeros(arrayT, size);  

	// cout << "wholeNum:     {0, 1, 2, 3, 4,	...}" << endl;
	// wholeNum(arrayT, size);
	
	// cout << "naturalNum:     {1, 2, 3, 4, 5,	...}" << endl;
	// naturalNum(arrayT, size);

	// cout << "evenNum:     {2, 4, 6, 8, 10,	...}" << endl;
	// evenNum(arrayT, size);

	// cout << "multipleOfFive:     {10, 15, 20, 25,	...}" << endl;
	// multipleOfFive(arrayT, size);

	// cout << "multipleOfNum:     {T*0, T*1, T*2, T*3,...} where T is entered by the user" << endl;
	// cout << "Multiple of (starting from 0):";
	// cin >> num;
	// multipleOfNum(arrayT, size,num);
	
	// cout << "primeNum:     {2, 3, 5, 7, 11,	...} Save all the prime numbers" << endl;
	// primeNum(arrayT, size);

	// cout << "randomNum:     {Assigning random values}" << endl;
	// randomNum(arrayT, size);

	// cout << "randomEven:    {Assigning random Even numbers}" << endl;
	// randomEven(arrayT, size);
	
	// cout << "randomRange:    {Assigning random numbers within a range}" << endl;
	// int start, end;
	// cout << "start: "; 
	// cin>>start;
	// cout << "end: ";
	// cin>>end;
	// randomRange(arrayT, size,start,end);

	printArray("A:", arrayT, size);

    return 0;
}

void allZeros(int A[], int size)
{
    // Write your code here
}

void wholeNum(int A[], int size)
{
    // Write your code here
}
void naturalNum(int A[], int size)
{
	// Write code here
}
void evenNum(int A[], int size)
{
	// Write code here
}
void multipleOfFive(int A[], int size)
{
	// Write code here
}
void multipleOfNum(int A[], int size, int num)
{
	// Write code here
}
bool isPrime(int N)
{
	// Write code here
	return false;
}
void primeNum(int A[], int size)
{
	// Write code here
}
void randomNum(int A[], int size)
{
	// Write code here
}
void randomEven(int A[], int size)
{
	// Write code here
}
void randomRange(int A[], int size, int start, int end)
{
	// Write code here
}

void printArray(const char Name[], int A[], int size)
{
	// Write code here 
}







Coding playground for all challenges of the lesson

Fill the array with zeros

Task 1: Implement the function

void allZeros(int A[], int size)

Our program should ask for the size of the array and initialize the elements of the array as zeros using allZeros(int A[], int size) and then display it using the printArray() function:

Output:

Size(Maximum of 100): 6
Zeroes array A: {0, 0, 0, 0, 0, 0}

Fill the array with numbers

Task 2: Implement the function

void wholeNum(int A[], int size)

Our program should ask for the size of the array and initialize the size many entries using wholeNum(int A[], int size):

Output:

Size(Maximum of 100): 6 
A: {0, 1, 2, 3, 4, 5}

Fill the array with numbers II

Task 3: Implement the function

void naturalNum(int A[], int size)

Our program should initialize the array with the following sequence:

Size: 6
A: {1, 2, 3, 4, 5, 6}

Fill the array with even numbers

Task 4: Implement the function

void evenNum(int A[], int size)

Our complete program should initialize the array with even numbers:

Size(<100): 6
A: {2, 4, 6, 8, 10, 12}

Fill the array with multiples of five

Task 5: Implement the function

void multipleOfFive(int A[], int size)

Our complete program should print the following sequence:

Size: 7
A: {5, 10, 15, 20, 25, 30, 35}

Initializing with a multiple of num

Task 6: Implement the function

void multipleOfNum(int A[ ], int size, int num)

Write a program that initializes the array with the multiples of a number num. Our program should print the following sequence using an array:

Multiple of :  6
        Size: 10
{0, 6, 12, 18, 24, 30, 36, 42, 48, 54} 

Fill the array with prime numbers

Task 7: Implement the function

void primeNum(int A[], int size)

Our program should initialize the array with prime numbers (implement the isPrime() function to check for primality):

Size(<100): 6
    Primes: {2, 3, 5, 7, 11, 13}

Fill the array with random numbers

Task 8: Implement the function

void randomNum(int A[], int size)

Our program should be able to print the content of an array; in this case, the array is filled with random numbers.

66 77 222 1111 2222 1

Fill the array with random even numbers

Task 9: Implement the function

void randomEven(int A[], int size)

Our program should print random even numbers using an array:

Size(<100): 5
Random array A: {2, 66, 77, 22, 88}

Fill the array with random numbers in a range

Task 10: Implement the function

void randomRange(int A[ ], int size,int start,int end)

Our program should print random numbers within a range using an array initialization function:

Range (Start-End): 1 - 10
2 8 9 10 1

Menu-based solution

The solution for all the exercise questions is given below.

You should go through the solutions and understand how we created a menu-based program that asks how we should initialize the array. Based on the selected option, call the relevant function for initialization.

using namespace std;

void allZeros(int A[ ], int size);
void wholeNum(int A[ ], int size);
void naturalNum(int A[ ], int size);
void evenNum(int A[ ], int size);
void multipleOfFive(int A[ ], int size);
void multipleOfNum(int A[ ], int size,int num);
bool isPrime(int N);
void primeNum(int A[ ], int size);
void randomNum(int A[ ], int size);
void randomEven(int A[ ], int size);
void randomRange(int A[ ], int size,int start,int end);
void printArray(const char Name[ ], int A[ ], int Size);
void menu();

int main()
{
	const int capacity = 100;
	int num = 10,start = 1, end = 10;
	int Choice; bool Exit = false;
	int arrayT[capacity], size;  //array declaration
	
	do
	{
		menu();        // menu displays the list of initializations options
		cin >> Choice; // choose the option
		cout << "Size(<100): ";
		cin >> size;
		switch (Choice)
		{
		case 1:
			allZeros(arrayT, size); // A[i] = 0
			break;
		case 2:
			wholeNum(arrayT, size); // A[i] = i
			break;
		case 3:
			naturalNum(arrayT, size);// A[i] = i+1
			break;
		case 4:
			evenNum(arrayT, size);   // A[i] = (i*2)
			break;
		case 5:
			multipleOfFive(arrayT, size); // A[i] = (i+1)*5
			break;
		case 6:
			cout << "Multiple of:";
			cin >> num;
			multipleOfNum(arrayT, size,num); // A[i] = i*num
			break;
		case 7:
			primeNum(arrayT, size);        // A[i] to ith prime
			break;
		case 8:
			randomNum(arrayT, size);      // A[i] = rand()
			break;
		case 9:
			randomEven(arrayT, size);    // A[i] = (rand()/2)*2
			break;
		case 10:
			cout << "start: "; 
			cin>>start;
			cout << "end: ";
			cin>>end;                           // range = end-start+1
			randomRange(arrayT, size,start,end);// A[i] = (rand()/range)
			break;
		case 0:
			Exit = true;
			break;
		}
		printArray("A", arrayT, size);
		cout << "\n\nPress 0 to exit...\n\n";
	}
	while (Exit!=true);

	return 0;
}
Menu-based solution for all challenges