Enclosed my C source code: Need help to modify the program into such the way tha
ID: 3630617 • Letter: E
Question
Enclosed my C source code:Need help to modify the program into such the way that :
only 23 prime number under 100 will randomly generate in output txt file "MatrixA.txt".
Had to be exact 23 Prime number only!!!!!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class RandomMatrix {
int rows;
int cols;
int matrix[20][20];
ofstream MatrixA;
ofstream OutputMatrix;
public:
RandomMatrix(int r, int c) :
rows(r), cols(c),
MatrixA("MatrixA.txt"),
OutputMatrix("OutputMatrix.txt")
{
random();
}
bool isPrime(int) const;
void random();
void output1();
void output2();
};
bool RandomMatrix::isPrime(int num) const
{
if (num % 2 == 0)
return false;
for (int i = 2; i < num; i++)
if (num % i == 0)
return false;
return true;
}
void RandomMatrix::random()
{
srand(time(0));
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = rand() % 100;
}
void RandomMatrix::output1()
{
MatrixA << rows << endl;
MatrixA << cols << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
MatrixA << matrix[i][j] << " ";
MatrixA << endl;
}
}
void RandomMatrix::output2()
{
OutputMatrix << rows << endl;
OutputMatrix << rows << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
if (isPrime(matrix[i][j]))
OutputMatrix << 0 << " ";
else
OutputMatrix << matrix[i][j] << " ";
OutputMatrix << endl;
}
}
int main()
{
int r = 0, c = 0;
cout << "Enter rows and columns between 10 to 20:" << endl;
failed:
cin >> r >> c;
if (r < 10 || r > 20 || c < 10 || c > 20) {
cout << "Error, rows and columns must between 10 to 20." << endl;
cout << "Try again!" << endl;
goto failed;
}
RandomMatrix rm(r, c);
rm.output1();
rm.output2();
}
Explanation / Answer
First of all this looks like C++ rather than C code. One way to control the number of prime numbers you get in your generated matrix is to control the randomness when you're first filling out matrix[i][j] using the random() function before you need to write it into MatrixA.txt
You could just keep track of the prime numbers you've generated so far, and once you've got 23 of them, next time you get one, try generating a new random number until you get a non-prime number to add.
There are some zeros that can pop up and make it seems like the outpur from more than 23 prime numbers, but you should check the matrixA file, you're likely to find that the number is an actual Zero not a prime number.
The sections that were changed are the class declaration/constructor and the random() function. They're highlighted in yello. Hope this helps. If it's not donig what you want then just send me the details to inbox :)
class RandomMatrix {
int rows;
int cols;
int matrix[20][20];
int primesCount;
ofstream MatrixA;
ofstream OutputMatrix;
public:
RandomMatrix(int r, int c) :
rows(r), cols(c),
MatrixA("MatrixA.txt"),
primesCount(0),
OutputMatrix("OutputMatrix.txt"){ random(); }
bool isPrime(int) const;
void random();
void output1();
void output2();
};
bool RandomMatrix::isPrime(int num) const
{
if (num % 2 == 0)
return false;
for (int i = 2; i < num; i++)
if (num % i == 0)
return false;
return true;
}
void RandomMatrix::random()
{
srand(time(0));
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
int num = rand() % 100;
if (isPrime(num))
{
if(primesCount < 23)
{
matrix[i][j] = num;
primesCount++;
}
else
j--;
}
else
{
matrix[i][j] = num;
}
}
}
void RandomMatrix::output1()
{
MatrixA << rows << endl;
MatrixA << cols << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
MatrixA << matrix[i][j] << " ";
MatrixA << endl;
}
}
void RandomMatrix::output2()
{
OutputMatrix << rows << endl;
OutputMatrix << rows << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
if (isPrime(matrix[i][j]))
OutputMatrix << 0 << " ";
else
OutputMatrix << matrix[i][j] << " ";
OutputMatrix << endl;
}
}
int main()
{
int r = 0, c = 0;
cout << "Enter rows and columns between 10 to 20:" << endl;
failed:
cin >> r >> c;
if (r < 10 || r > 20 || c < 10 || c > 20) {
cout << "Error, rows and columns must between 10 to 20." << endl;
cout << "Try again!" << endl;
goto failed;
}
RandomMatrix rm(r, c);
rm.output1();
rm.output2();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.