In this article, we will discuss the Program to print V and inverted-V patterns in C++ with its implementations, time complexity, and space complexity.
- Inverted V pattern: Print the pattern inverted V given the value n.
Example 1:
Let us take an example to illustrate the inverted-V pattern in C++ .
#include <iostream>
using namespace std;
// Function to generate and print the pattern
void generatePattern(int size)
{
int row, outerGap, innerGapCounter = 0;
// Variables for row iteration, outer gaps, and inner gap count.
for (row = size - 1; row >= 0; row--)
{
// Printing leading spaces (outer gaps).
for (outerGap = size - 1; outerGap > innerGapCounter; outerGap--)
{
cout << " ";
}
// Printing the first character in the row.
cout << char(row + 65);
// 65 is the ASCII value of 'A'.
// Print spaces between the characters (inner gaps).
for (outerGap = 1; outerGap < (innerGapCounter * 2); outerGap++)
{
cout << " ";
}
// Printing the second character if not the first row.
if (row < size - 1)
{
cout << char(row + 65);
}
// Moving to the next line
cout << "\n";
// Increment the inner gap counter for the next row.
innerGapCounter++;
}
}
int main()
{
// Variable to store the size of the pattern.
int patternSize;
// Taking input from the user.
cout << "Enter the size of the pattern: ";
cin >> patternSize;
// Validate the input to ensure it's positive.
if (patternSize <= 0)
{
cout << "Please enter a positive integer for the pattern size." << endl;
return 1;
// Exit the program.
}
// Call the function to generate and print the pattern.
generatePattern(patternSize);
return 0;
}
Output:
Enter the size of the pattern: 9
I
H H
G G
F F
E E
D D
C C
B B
A A
Explanation:
A symmetric alphabetical inverted-V pattern is generated and printed by the given C++ program according to the user-specified size. The inner loops manage spacing and character printing, while the outer loop establishes the rows. It is an example of nested loops. Beginning with the letter that represents ASCII value 65 ('A'), the pattern declines in alphabetical order with each row. The characters are printed with spaces on both sides to give the impression of a pyramid. By confirming that the size entered is positive, the program verifies the accuracy of the input and gracefully ends if it is not. In order to create symmetry, each row dynamically modifies the inner gap. The code is easy to understand due to the usage of comments and descriptive variables .
- V pattern: Print the V pattern given the value of n.
Example 2:
Let us take an example to illustrate the V pattern in C++.
#include <iostream>
using namespace std;
// Function to generate and print the pattern.
void generatePattern(int patternSize)
{
int currentRow, outerSpace, innerSpace;
// Loop through each row of the pattern.
for (currentRow = patternSize - 1; currentRow >= 0; currentRow--)
{
// Printing leading spaces (outer gaps).
for (outerSpace = patternSize - 1; outerSpace > currentRow; outerSpace--)
{
cout << " ";
}
// Printing the first character in the row.
cout << char(currentRow + 65);
// 65 is the ASCII value of 'A'.
// Printing spaces between the characters (inner gaps).
for (innerSpace = 1; innerSpace < (currentRow * 2); innerSpace++)
{
cout << " ";
}
// Printing the second character if not the first row.
if (currentRow >= 1)
{
cout << char(currentRow + 65);
}
// Move to the next line.
cout << "\n";
}
}
int main()
{
// Variable to store the size of the pattern.
int userInputSize;
// Taking input from the user.
cout << "Enter the size of the pattern: ";
cin >> userInputSize;
// Validate the input to ensure it's positive.
if (userInputSize <= 0)
{
cout << "Please enter a positive integer for the pattern size." << endl;
return 1;
// Exit the program.
}
// Call the function to generate and print the pattern.
generatePattern(userInputSize);
return 0;
}
Output:
Enter the size of the pattern: 9
I I
H H
G G
F F
E E
D D
C C
B B
A
Explanation:
The program uses user input to generate an alphabetical pyramid pattern. After validation of the pattern size, it prints the pattern row by row. Each row starts with the ASCII character that corresponds to the current row index after a specified number of leading spaces (outer gaps) for alignment. In order to create the pyramid structure, spaces (inner gaps) are printed, if applicable, between two identical characters in the same row. Inner loops handle spaces and characters, while the outer loop iterates over rows to create the pattern. The ASCII value of 'A', 65, is used by the program to dynamically calculate the characters. After that, input validation improves the program by ensuring that the user enters a positive number.
Complexity Analysis:
- Time Complexity: O(n²) The program's nested loops result in O(n^2) complexity because the inner loops iterate proportionally to the needs of the current row while the outer loop runs n times. As a result, the relationship between the total number of operations and the input size n is quadratic.
- Auxiliary Space: O(1) The program for variables like currentRow, outerSpace, and innerSpace uses a fixed amount of extra memory. No matter the amount of input, the auxiliary space remains the same because no additional data structures are allocated.