i need a code for this!!

Z asterisk pattern!! plsssssssss

The program forms the letter Z by printing n input
character symbols on the upper line, n input character symbols on the lower line, and n input
character symbols on the diagonal line. If the value of n is less than or equal to 2, the program
prints “Cannot create the mark.”.
The input and printing process must be repeated until the user presses n, for stop.

Input a number and a symbol: 5 #
#####
#
#
#
#####
Do you want to continue? [y/n] : y
Input a number and a symbol: 12 *
************
*
*
*
*
*
*
*
*
*
*
************
Do you want to continue? [y/n] : n
Goodbye.

Dani AI

Generated

asked for a C++ program that prints a Z-shaped pattern from an input size n and a single character, repeating until the user quits. A common bug (noted by ) is output that looks like a "C" — that happens when the diagonal is put at the left instead of running from top-right to bottom-left. Using zero-based row index r in [0..n-1], the diagonal column is col = n - 1 - r (one-based loops use col = n - r).

Two practical approaches: an n x n buffer (as suggested) is simple to visualize but uses O(n^2) memory; a streaming method prints rows on the fly and uses O(1) extra memory. The streaming method: print the top row of n symbols; for each interior row r = 1..n-2 print (n-1-r) spaces then one symbol and a newline; then print the bottom row. Reject very small sizes (n <= 2) with an error. As recommended, posting a minimal reproducible snippet helps track mistakes; 's nested-loop idea maps directly to the streaming version (outer loop over rows, inner loop for spaces). Input notes: read number and symbol with operator>>, and read the continue reply as a non-whitespace char (accept both 'n' and 'N'); use std::ws if explicit whitespace handling is needed.

#include <iostream>
#include <cctype>

int main() {
    int n;
    char ch, cont = 'y';
    while (cont == 'y' || cont == 'Y') {
        if (!(std::cin >> n >> ch)) break;
        if (n <= 2) {
            std::cout << "Size too small for a Z\n";
        } else {
            for (int i = 0; i < n; ++i) std::cout << ch;
            std::cout << '\n';
            for (int r = 1; r <= n - 2; ++r) {
                int col = n - 1 - r;
                for (int s = 0; s < col; ++s) std::cout << ' ';
                std::cout << ch << '\n';
            }
            for (int i = 0; i < n; ++i) std::cout << ch;
            std::cout << '\n';
        }
        std::cin >> cont;
    }
    std::cout << "Exiting.\n";
    return 0;
}

Recommended Answers

All 4 Replies

Lets see some of your code. Then we will know what areas to help you with.

The program forms the letter Z by printing n input
character symbols on the upper line, n input character symbols on the lower line, and n input
character symbols on the diagonal line.

However, down the line, your output shows the drawing for C, not Z... I don't get your problem. Post your code.

Steps:

- Get ColumnSize
- Create a 2D array ColumnSize x ColumnSize with each element initialized to ' '(space)
- Populate the First and last column of the 2D array '#'
- for i = ColumnSize - 1 to 0
   - make Array2D[ColumnSize][ColumnSize] = '#'

- Print the whole array

Its more easy to grasp( at least I think so ) if you use the above approach, but using can do this without using array

use nested for loop; outer loop will print the "#" symbol and inner loop will print spaces from 2nd line. don't forget to reduce the count of inner loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.