lundi 7 février 2022

Creating a pattern with forward slash and backslash in C++ from an Input Sequence

I'm trying to do a college project where I use C++ to develop something similar to a star pattern. I need help fixing my code. My current one is an attempt to make this pattern upside-down which I also don't know how to invert. Due date: 2/11/22.

This is the original instructions.

 Write a program  that follows the pattern demonstrated with sample input and output below the numbers in the sequence correlate to a column of /s in odd # columns and \s in even columns.

Sample input:
1 1 3 1 2 3 5 2 1 2 2 3 7 2 3 1 2 1 3 5 2 3 1 2 5 7 3 1 3 4 2 5 3 2 4 4 1 2 1 3 3 2 1 2

Sample output:

                                             o                                                                       
                                            /|\                                                                      
                                            < >                                                                      
                                            / \                                                                      
                                           /   \                                                                     
                                        /\/     \              /\                                                    
                                     /\/         \  /\        /  \                                                   
                                /\  /             \/  \      /    \          /\                                      
                               /  \/                   \/\  /      \        /  \                                     
                              /                           \/        \    /\/    \  /\            /\                  
               /\            /                                       \  /        \/  \          /  \                 
              /  \/\  /\    /                                         \/              \    /\  /    \                
       /\    /      \/  \  /                                                           \  /  \/      \/\             
    /\/  \  /            \/                                                             \/              \/\    /\    
   /      \/                                                                                               \  /  \/\ 
/\/                                                                                                         \/      \

This is the code I tried so far. Code:

#include <iostream>
using namespace std;

//Array to print the slashes to create the pattern
void patternPrint(int n)
{
    for (int i = 0; i < n; i++) //For loop to create the pattern upside down
    {
        for (int j = 0; j <= i; j++) {
            if (j % 2 == 0)
            {
                cout << "\\" + "\n" + "  "; //on each next even column there will be 2 spaces + breakline for diagonal line
            }
            else
            {
                cout << "/" + "\n"; //on each next odd column there will be 2 spaces + breakline for diagonal line
            }

            cout << endl;
        }
    }
}

int main()
{
    int rows;
    int patternsize;

    cout << "Please choose how many rows you want your pattern to have";
    cin >> rows;
    cout << "Please enter a sequence of numbers to create your pattern. It will be alternating / and '\'s. Seperate each # with the enter key: ";
    int *array = new int[rows];
    for (int i = 0; i <= rows; i++)
    {
        std::cin >> array[i];
    }
   
    patternsize = sizeof(array)/sizeof(array[0]);
    cout << "Your pattern's amount of rows is = " << patternsize;
   
    patternPrint(patternsize);
   
    delete (array);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire