-4

The program my prof asked me to make, is to take the inputs of height(h) and width(w). width is within the range of 10 - 20. and height is 3 - 61.

i have to print amount random lines such that k = rand() % (w-1) + 2

my issue is that I have to compare whether the random number K is closer to "w" or zero. how can I make a comparison with a non-fixed integer?

What i have so far

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

int main(void)
{
int h,w,k, x,y,z;
char l;
srand(time(NULL));

z=1;
x = rand();
while(z>0){

printf("Enter Width: ");
    scanf("%d", &w);
printf("Enter height: ");
    scanf("%d", &h);

    if(w >= 10 && w <= 20){
        if(h>=3 && h <= 61)
            z= -1;
    }
}

for(z=0;z < h; z++){
    printf("%d: ",k);
    k = (rand() % (w-1)) + 2;
    if(~ ~ ~){              // <--   <--   <--   <--   <--   ****
    for(y=0;y < k; y++){
    printf(" ");
    for(y=0;y < (k-1) ;y++){
        printf("j");
    }}}
    else{
    for(y=0;y < (k-1) ;y++){
        printf("j");
    }}
    printf("\n");
}
return 0;
}

In my if statement. I want to compare if K is closer to my value W than 0. it will decide if I print K amount of zero's first or just print my characters.

->Edits have been made. still working on this. I will post an answer when I am done. if I figure it out.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Elfonz
  • 1
  • 1
  • Why not do `int x = srand();` and then use `x` in the equation `k = x * (w-1) + 2`. I am sure you can check the value of `x` before doing the computation. – Sudheesh Singanamalla Nov 20 '17 at 04:01
  • What about (k -w > w- 0)? – Yossi Nov 20 '17 at 04:47
  • I have rollbacked your changes to an actual question. Please find [your solution in the revision history](https://stackoverflow.com/revisions/33d53808-0e73-4fe0-a7ef-36411db8f772/view-source) and post it as an answer of its own. – Cœur Jun 18 '18 at 17:05

1 Answers1

-1

I'm not sure I understand your question... It might be that you just want your numbers to be right-aligned? In this case, try:

printf("%*d", w, k)

This prints k with enough spaces in front, so that the width of the entire text printed is w.

See this answer for more details.

Sjlver
  • 1,227
  • 1
  • 12
  • 28