the program should take an integer from user and print pattern like below. if n=3, then
333
313
323
333
if n=4, then
4444
4114
4224
4334
4444
if n=5, then
55555
51115
52225
53335
54445
55555
and so on
Here is what is have tried.
#include<iostream>
using namespace std;
int main()
{
int pattern[10][10], i, j, n;
cout << "Enter dimension of square matrix: ";
cin >> n;
for (i = 0;i <= n;i++)
{
pattern[i][0] = n;
pattern[i][n - 1] = n;
}
for (j = 1;j < n - 2;j++)
{
pattern[0][j] = n;
pattern[n][j] = n;
}
for (i = 1;i < n - 1;i++)
{
for (j = 1;j < n - 2;j++)
{
pattern[i][j] = i;
}
}
for (i = 0;i <=n;i++)
{
for (j = 0;j < n;j++)
{
cout << pattern[i][j];
cout << "\t";
}
cout << "\n";
}
return 0;
}
I am getting the right pattern but in some places, there are some garbage value (or something else)
Aucun commentaire:
Enregistrer un commentaire