In this guide, we are going to explore the Program to display V and upside-down V patterns in C++ along with their implementations, time complexity, and space complexity.
- Upside-down V pattern: Output the inverted V pattern based on the input value n.
Example 1:
Let's consider an example to demonstrate 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 mirrored alphabetical V-shaped design is produced and displayed by the provided C++ script, based on the specified size chosen by the user. The inner loops are responsible for handling the spacing and character output, while the outer loop controls the rows. This scenario exemplifies the utilization of nested loops. Starting with the letter corresponding to the ASCII value 65 ('A'), the pattern progresses in reverse alphabetical sequence per row. Deliberate spacing on both sides of the characters is incorporated to create a pyramid-like visual effect. The program ensures the input's validity by confirming that the entered size is a positive value; if not, it gracefully terminates. To maintain symmetry, the inner gap in each row is dynamically adjusted. The script's clarity is enhanced by the presence of comments and well-named variables.
- V pattern: Display the V pattern based on the provided value of n.
Example 2:
Let's consider an example to demonstrate 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 software leverages input from the user to construct a pyramid design based on the alphabet. Once the size of the pattern is verified, it proceeds to display each row of the pattern. Every row commences with the ASCII symbol corresponding to the current row number after a designated count of leading spaces (outer gaps) to maintain alignment. To establish the pyramid shape, spaces (inner gaps) are included, if necessary, between two identical symbols within the same row. The internal loops manage the insertion of spaces and symbols, while the external loop moves through the rows to generate the pattern. By utilizing the ASCII value of 'A', which is 65, the software dynamically determines the symbols to be displayed. Subsequently, input authentication enhances the software by ensuring that a positive integer is provided by the user.
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.