Xiefux
Xiefux

Reputation: 11

Problem with for loops, trying to make a specific pattern

I am trying to make a V shape. it is almost perfect but i cannot change the ammount of spaces i need to print by changing variables l and r in the very end. im new to programming and im at my wits end here. it should logically work, right?

shape im trying to make

what i actually get

#include<stdio.h>

int main(){

int l=0,r=17,y=9,x=0,z=0;

for(z=0;z<y;z++){


    for(x=0;x<l;x++){
        printf(" "); //first space
    }
    printf("****");

    for(x=0;x<r;x++){
        printf(" "); //second space
    }
    printf("****");
    printf("\n");
    l+1;
    r-2;
}
}

Upvotes: 1

Views: 62

Answers (3)

zyrup
zyrup

Reputation: 717

l+1;
r-2;

Should both be:

l+=1;
r-=2;

Here your script with slight modifications:

#include <stdio.h>
// v-shape
// gcc -Os -Wall -o 53505746 53505746.c

int main() {

    int l = 0;
    int r = 17;
    int y = 9;
    int x = 0;
    int z = 0;

    for (z=0; z<y; z++) {

        for (x=0; x<l; x++) {
            printf(" "); // first space
        }

        printf("****");

        for (x=0; x<r; x++) {
            printf(" "); // second space
        }

        printf("****");
        printf("\n");
        l += 1;
        r -= 2;

    }

    return 0;
}

Upvotes: 0

Abhinav Gyan
Abhinav Gyan

Reputation: 66

The lines

l+1;
r-2;

does not change the values of l and r.

It should be

l = l + 1;
r = r - 2;

or

l += 1;
r -= 2;

Upvotes: 2

Govind Parmar
Govind Parmar

Reputation: 21572

You are very close to solving this problem. Your error is here:

l+1;
r-2;

Think about what happens when you use an operation like + or -... where does the result go?

Upvotes: 0

Related Questions