Find prime numbers and traverse an array in a circular manner.
We'll cover the following
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 as the first prime number at the index .
- Keep checking every following integer by dividing it by every prime value in the array.
- Check by taking the remainder after dividing it by all the prime values in the array. The only value in the current array is . Since the remainder is non-zero, is stored in the array.
- Then, check in the same way. Since the remainder of divided by is , it’s not stored in the array.
#include <iostream>using namespace std;int main(){int n = 5; // The number of prime numbers to be storedif (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 5int pcount = 0;int v = p[pcount] + 1; // The value to be testedint r, i, pflag;while (pcount < n-1){ // The loop for number of generated valuesr = 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 testedpflag = 1; // Flag to stop the inner loop}else{i += 1; // Move the index forward}}if (pflag == 0){ // Check flag after finishing the inner looppcount += 1; // Increment pcount we found next prime numberp[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 ofn
shouldn’t be less than1
. - 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.
#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 traversalint 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 zerocout << "Now PROCESSING the index " << i << endl;else{if (i != j) // If i is not equal to jcout << "Now skipping the index " << i << endl;elsecout << "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 valueint k = 0;int ii = i; // Storing value of i in iiout << "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 iaddneg = 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 jallneg = 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 and , resulting in a matrix .
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
#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
andb
. - We create variables (
aROWS
,aCOLS
,bROWS
,bCOLS
,cROWS
, andcCOLS
) and assign them values (3
,4
,4
,2
,3
, and2
, respectively). - We create a matrix,
c[3][2]
, initialized with zeros. - We calculate the product of matrices
a
andb
and store the result inc
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.