Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* Write a function that fills a dynamic, n x n array with integers from 1 to n

ID: 3578135 • Letter: #

Question

/*
Write a function that fills a dynamic, n x n array with integers from 1 to n x n exactly.
The array contains no duplicates.Use the array you created in problem 1 to create the array.
The function's prototype is
void randomFillUnique(int** a, int n);
Write a program to test your function.
*/

the program should generate random numbers without duplicates...

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int ** gen2Array(int n);
void randomFillUnique(int **a, int n);

int main()
{
int n;
cout << "Enter a N number for size of the n x n array: ";
cin >> n;
int** result = gen2Array(n);

randomFillUnique(result, n);

cout << "Generated array : " << endl;

for (int i = 0; i<n; i++)
{
  for (int j = 0; j<n; j++)
  {
   cout << result[i][j] << " ";
  }
  cout << endl;
}
system("pause");
return 0;
}
int ** gen2Array(int n)
{
int **p;
p = new int*[n];
for (int i = 0; i < n; ++i)
{
  p[i] = new int[n];
}

return p;
}

void randomFillUnique(int **a, int n)
{
srand((unsigned)time(0));
for (int i = 0; i<n; i++) {
  for (int j = 0; j<n; j++)
  {
   a[i][j] = (rand() % 100) + 1;
  }
}

}

Explanation / Answer

Program is correct and giving correct output. Function randomFillUnique is given in this program at the end.

main.cpp

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int ** gen2Array(int n);
void randomFillUnique(int **a, int n);

int main()
{
int n;
cout << "Enter a N number for size of the n x n array: ";
cin >> n;
cout<<endl;
int** result = gen2Array(n);

randomFillUnique(result, n);

cout << "Generated array : " << endl;

for (int i = 0; i<n; i++)
{
for (int j = 0; j<n; j++)
{
   cout << result[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}
int ** gen2Array(int n)
{
int **p;
p = new int*[n];
for (int i = 0; i < n; ++i)
{
p[i] = new int[n];
}

return p;
}

void randomFillUnique(int **a, int n)
{
srand((unsigned)time(0));
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++)
{
   a[i][j] = (rand() % 100) + 1;
}
}

}

Output:-