-2

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.

bereal
  • 32,519
  • 6
  • 58
  • 104
mick
  • 11
  • 5
    [Please do not post images of text because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Text should be posted directly **as text** in your question. – MikeCAT Mar 07 '23 at 15:18
  • 4
    Also, don't include line numbers in source code. It make it more difficult for others to compile. – dbush Mar 07 '23 at 15:20
  • 1
    Your code prints `X` rather than `*`, AFAICS. If you want to print a random single-digit number instead of `X` or `*`, generate one and print it. The `usleep()` should be in the nested loops and the first loop should go away (or be incorporated into the nested loops — but you won't need the looping part of the first loop). You should be consistent with your indentation; it makes the code much easier to read if you are consistent. – Jonathan Leffler Mar 07 '23 at 15:40
  • 1
    What is the relationship of the output you expect and the random numbers?? – Jabberwocky Mar 07 '23 at 15:57
  • I suggest to [edit] your question and add a specification of the problem(s) you are supposed to solve. Explain what you should do with the random numbers. If you don't even know how to print the expected pattern, concentrate on this task first, then extend/change the program to handle the random numbers. – Bodo Mar 07 '23 at 17:49

1 Answers1

0

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.

NoDakker
  • 3,390
  • 1
  • 10
  • 11