In: Computer Science
code screenshot:
output screenshot:
code:
#include<bits/stdc++.h>
using namespace std;
void generateGroups(int n)
{
//creating array and filling all entries with 0
int a[n*n]={0};
int i,j,st;
//Now traversing the array , we just have to find the
//position where a particular number comes first time i.e
(st)
//then after that, this number is repeatedly be assigned to
//every (st+n) positions
for(i=0;i<n;i++)
{
st=n-i-1 + i*n;
for(j=st ;j<n*n;j+=n)
{
a[j]=i+1;
}
}
for(i=0;i<n*n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
//examples
generateGroups(3);
generateGroups(2);
generateGroups(4);
}
please reformat the code according to the screenshot given
Please upvote if you like the answer!