
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Latin Square in C++
The Latin square is a matrix that has a special pattern. Let's see different examples to examine the pattern.
1 2 2 1 1 2 3 3 1 2 2 3 1 1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1
The Latin square that you get will be of different size as you notice in the above examples. But, if you carefully observe the above matrices' pattern, you will find out that the last number of the previous row comes as the first element of the next row.
That's the pattern hidden in Latin square. We have to write the program that generates the above matrix for the input n.
Algorithm
- Initialise the n with any number you like.
- Initialise a number with the value n + 1 call it as first_half_end.
- Write a loop that iterates from 1 to n both inclusive.
- Assign the value of first_half_end to a variable called first_half_start.
- Write a loop until first_half_start reaches to the value n.
- Print the iterating variable i.e.., first_half_start.
- Write a loop that iterates from 1 to first_half_end.
- Print the iterating variable.
- Decrement the value of first_half_end by 1.
- Move the next row.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; void generateLatinSquare(int n) { int first_half_end = n + 1; for (int i = 1; i <= n; i++) { int first_half_start = first_half_end; while (first_half_start <= n) { cout << first_half_start << " "; first_half_start++; } for (int second_half_start = 1; second_half_start < first_half_end; second_half_start++){ cout << second_half_start << " "; } first_half_end--; cout << endl; } cout << endl; } int main(void) { generateLatinSquare(2); generateLatinSquare(3); generateLatinSquare(4); return 0; }
Output
If you run the above code, then you will get the following result.
1 2 2 1 1 2 3 3 1 2 2 3 1 1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1
- Related Articles
- Latin Square in Python
- Goat Latin in Python
- Give the Latin name for sodium, potassium, gold, and mercury.
- In the following APs, find the missing terms in the boxes:(i) $2, \square, 26$(ii) $\square, 13, \square, 3$(iii) $5, \square, \square, 9\frac{1}{2}$(iv) $-4, \square, \square, \square, \square, 6$(v) $\square, 38, \square, \square, \square, -22$
- Square and Square root in Arduino
- In the following APs, find the missing terms in the boxes:$-4, \square, \square, \square, \square, 6$
- In the following APs, find the missing terms in the boxes:$\square, 38, \square, \square, \square, -22$
- Program to print Square inside a Square in C
- Count square and non-square numbers before n in C++
- Fill in the blanks:(a) \( 369 \div \square=369 \)(b) \( (-75) \div \square=-1 \)(c) \( (-206) \div \square=1 \)(d) \( -87 \div \square=87 \)(e) \( \square \div -1=-87 \)(f) \( \square \div 48=-1 \)(g) \( 20 \div \square=-2 \)(h) \( \square \div(4)=-3 \)
- Maximal Square in C++
- Difference between sum of square and square of sum in JavaScript
- Mid-Square hashing in C++.
- Polybius Square Cipher in C++
- Matchsticks to Square in C++

Advertisements