Find prime numbers and traverse an array in a circular manner.

Store the first n prime numbers in the array

The following program stores the first n prime numbers to an array. The value of n is chosen by the user, and any number can be used. Here’s how to solve this problem:

  • Start with an empty array of n elements.
  • Store 22 as the first prime number at the index 00.
  • Keep checking every following integer by dividing it by every prime value in the array.
    • Check 33 by taking the remainder after dividing it by all the prime values in the array. The only value in the current array is 22. Since the remainder is non-zero, 33 is stored in the array.
    • Then, check 44 in the same way. Since the remainder of 44 divided by 22 is 00, it’s not stored in the array.
Press + to interact
#include <iostream>
using namespace std;
int main()
{
int n = 5; // The number of prime numbers to be stored
if (n < 1) // Checks the validity of input
{
cout << "It seems the number of desired prime numbers has got an invalid value:" << endl;
}
else
{
int p[n] = {2,0,0,0,0}; // Generating an array of size 5
int pcount = 0;
int v = p[pcount] + 1; // The value to be tested
int r, i, pflag;
while (pcount < n-1){ // The loop for number of generated values
r = pcount + 1;
i = 0;
pflag = 0;
while(i <= pcount && pflag == 0){ // Loop for testing v against each prime number p[i]
if (v % p[i] == 0){
v += 1; // Current v is not a prime number generate next value to be tested
pflag = 1; // Flag to stop the inner loop
}
else{
i += 1; // Move the index forward
}
}
if (pflag == 0){ // Check flag after finishing the inner loop
pcount += 1; // Increment pcount we found next prime number
p[pcount] = v; // Store the next prime number to the next position in the array
}
}
cout << "{ ";
for (int i = 0; i < n; i++) // Printing the array
{
cout << p[i] << " ";
}
cout << "}" << endl;
}
return 0;
}

In the code above:

  • The variable n shows the total number of prime numbers.
  • The first if statement specifies that the value of n shouldn’t be less than 1.
  • We create an array of n values, p[n] = {0,0,0,0,0} .
  • We use two nested while loops.
    • The outer loop counts the number of generated prime numbers.
    • The inner loop keeps generating sequential integers until the next prime number is found.
  • We use the following variables:
    • n stores the total number of prime numbers.

    • p stores the generated prime numbers.

    • pcount keeps track of the most recently generated prime number.

    • v stores the next number to be tested.

    • i iterates through the array of prime numbers generated so far

    • pflag indicates whether the current number is prime.

The comments in the program itself will help explain the rest of the code.

Circular traversal

The following program demonstrates circular moves in an array using the positive numbers in that array. The program makes the value of the cell that is shown negative and ends when all values are negative.

To solve this problem, we first need to understand circular traversals.

Press + to interact
#include <iostream>
using namespace std;
void print (int arr [], int size) // Function to print an array
{
cout << "{ ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << "}";
cout << endl;
}
int main()
{
int a[] = { 2 , 8 , 3 , 15 }; // Enter the array for circular traversal
int i = 0;
int j = 0;
int size = sizeof(a)/sizeof(a[0]);
int allneg = 0;
int addneg = 0;
while (allneg == 0) // This loop will terminate when the value of allneg is zero
{
if (a[i] > 0) // If the value of a is greater than zero
cout << "Now PROCESSING the index " << i << endl;
else
{
if (i != j) // If i is not equal to j
cout << "Now skipping the index " << i << endl;
else
cout << "Now STOPPING at the index " << i << endl;
}
cout << i << " ";
print(a,size);
if (a[i] > 0) // If the value of a is greater than zero
{
a[i] = -a[i]; // Converting value to negative value
int k = 0;
int ii = i; // Storing value of i in ii
out << "Moving " << -a[i] << " steps" << endl;
while (k < -a[i]) // This loop will terminate when k is not less than -a[i]
{
ii = (ii + 1) % size;
k += 1;
cout << ". " << ii << " ";
print(a,size);
}
i = ii; // Storing the updated value of ii to i
addneg = 1;
}
else // If the value of a is not greater than zero
{
if (addneg == 1) // If the value of addneg is 1
{
j = i;
addneg = 0; // Change the value of addneg to 0
}
else
{
if (j == i) // If i is equal to j
allneg = 1;
}
i = (i + 1) % size; // Update value of i
}
}
cout << " *** DONE *** " << endl;
return 0;
}

Calculate the product of two matrices

Write a program to display the product of these two matrices a[3×4]a[3\times4] and b[4×2]b[4\times2], resulting in a matrix c[3×2]c[3\times2].

Sample input

a: {{1,2,3,4}, 
     {5,6,7,8},
     {9,10,11,12}}    

b: {{10,20},
     {30,40}, 
     {50,60}, 
     {70,80}}

Sample output

Displaying in matrix form: 
500	    600	
1140	1400	
1780	2200
Press + to interact
#include <iostream>
using namespace std;
int main()
{
int a[3][4] = {{1,2,3,4} , {5,6,7,8} ,{9,10,11,12}};
int b[4][2] = {{10,20}, {30,40}, {50,60}, {70,80}};
int aROWS = 3;
int aCOLS = 4;
int bROWS = 4;
int bCOLS = 2;
int cROWS = 3;
int cCOLS = 2;
int c[3][2] = {{0,0},{0,0},{0,0}};
for (int i = 0; i < aROWS; i++)
{
for (int j = 0; j < aCOLS; j++)
{
for (int k = 0; k < bCOLS; k++)
{
c[i][k] += a[i][j] * b[j][k];
}
}
}
cout << "Displaying in matrix form: " << endl;
for (int i = 0; i < cROWS; i++) // Printing a 2-dimensional array
{
for (int j = 0; j < cCOLS; j++)
{
cout << c[i][j] << "\t";
}
cout << endl;
}
return 0;
}

In the program above:

  • We create two matrices, a and b.
  • We create variables (aROWS, aCOLS, bROWS, bCOLS, cROWS, and cCOLS) and assign them values (3, 4, 4, 2, 3, and 2, respectively).
  • We create a matrix, c[3][2], initialized with zeros.
  • We calculate the product of matrices a and b and store the result in c using three nested loops.
  • We display the resultant matrix, c, directly (as a nested array) using loops.

Get hands-on with 1400+ tech skills courses.