How to make a vertical zig zag? That generates random letters ? As a beginner I'm not understanding

105 Views Asked by At

This is the line of code I have as a foundation of what I need to do.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <time.h>
    4 #include <unistd.h>
    5
    6
    7
    8 int main() {
    9
    10     int num;
    11     float test;
    12     printf("Hello World\n");
    13
    14     srand(time(NULL));
    15
    16     for(int x = 0; x<10; x++){
    17     //generate number here:
    18 //  num = (rand() % (higher - lower + 1)) + lower;
    19     num = (rand() % (25-13 +1)) + 13;
    20
    21     printf("Number is %d\n", num);
    22     usleep(100000);
    23 }
    24     const int numRows = 6;
    25 const int numCols = 21;
    26 for (int row = 0; row < numRows; ++row)
    27 {
    28     for (int col = 0; col < numCols; ++col)
    29     {
    30         if (col == row)
    31         {
    32             printf("X");
    33         }
    34         else
    35         {
    36             printf(" ");
    37         }
    38     }
    39     printf("\n");
    40 }
    41
    42
    43
    44     return 0;
    45 }

This is the desired out come i'd like :

enter image description here

Instead of the "*" is it possible to input randomly generated numbers? Im required to include the sleep function and randomly generated numbers but i'm stuck on how to make the vertical zig zag.

Does anyone have any pointers/ solutions?

I created code to generate random numbers and used the sleep function. I also began a zig zag pattern but having difficulty to continue it vertically.

1

There are 1 best solutions below

0
NoDakker On

Noting the good comments and trying out your code, it was evident that a straightforward approach to your desired outcome would be to utilize two "for" loops - one to provide the outward character printing and a second "for" loop to provide the inward character printing. Also, keeping in mind that the program would print a random character, following is a refactored version of the code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int main()
{
    srand(time(NULL));

    const int numRows = 7;
    const int numCols = 21;

    for (int row = 0; row < numRows; ++row)         /* Ascending number of rows */
    {
        for (int col = 0; col < numCols; ++col)
        {
            if (col == row)
            {
                printf("%c", rand() % 26 + 'a');    /* Generate a random character and print it */
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    for (int row = numRows - 2; row >= 0; --row)    /* Descending number of rows - one less than ascending to provide symmetry */
    {
        for (int col = 0; col < numCols; ++col)
        {
            if (col == row)
            {
                printf("%c", rand() % 26 + 'a');    /* Generat a random character and print it */
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Here are some of the highlights.

  • First off, a second "for" loop was added utilizing a decrementing of the row value to provide the outward/inward bow to the printout.
  • Within the printing statement, a random alphabetical character is derived by utilizing the modulus of value "26", and printed - lower case letters are used in this example.

Testing this out, following is a sample of the output at the terminal.

@Vera:~/C_Programs/Console/Zigzag/bin/Release$ ./Zigzag 
s                    
 c                   
  m                  
   c                 
    s                
     k               
      n              
     z               
    k                
   h                 
  b                  
 m                   
x              

Give that a try and see if this meets the spirit of your project.