Manin intheworld
Manin intheworld

Reputation: 7

want to use a variable in one (for) loop in the next in C

I am a beginner, and am trying to do cs50 mario left half of a pyramid something like this ( imaging s is just empty space)

ssss#
sss##
ss###
s####

after a lot of thinking i figured this formula should work

 #include<cs50.h>
#include<stdio.h>

int main(void)

{
 int n = get_int("Height: ");
 for (int i = 0; i < n; i++)
 {

  // s stands for spaces
    for (int s = 0; s < n - 1 - i; s++)


       printf(" ");

  // h stands for hashes
    for (int h = 0; h < n - s; h++)
       printf("#");
       printf("\n");

 }

printf("\n");
}

as you see i want to use the variable s value which is changing and use it in the next variable h i know that its value is only withing the () when i tried to add { } like this

#include<cs50.h>
#include<stdio.h>

int main(void)

{
 int n = get_int("Height: ");
 for (int i = 0; i < n; i++)
 {

  // s stands for spaces
    for (int s = 0; s < n - 1 - i; s++)

{
       printf(" ");


  // h stands for hashes
    for (int h = 0; h < n - s; h++)
       printf("#");
       printf("\n");
       }

 }

printf("\n");
}

it avoids the printf(" ") line and prints # first which result in some thing like this

 
####
###
##
####
###
####

so how can i get it to first print spaces by the number of ( INput-1-line) then print hashes (Input- variable hashes) so that in the 2nd line of a 4 lines pyramid i get ( 4-1-1=2 spaces) then hashes(input- spaces) ( 4-2= 2) ss## 3rd line (4-1-2=1)(4-3=1) s### and so on.

Upvotes: 1

Views: 229

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 754280

If I copy and format the code from the question, it looks like this:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n = get_int("Height: ");
    for (int i = 0; i < n; i++)
    {
        // s stands for spaces
        for (int s = 0; s < n - 1 - i; s++)
        {
            printf(" ");
            // h stands for hashes
            for (int h = 0; h < n - s; h++)
                printf("#");
            printf("\n");
        }
    }

    printf("\n");
}

The loop structure is not what was intended; the h loop can access s, but it shouldn't be able to. The i loop counts from zero up to (but not including) the number entered. You want to print i + 1 hashes, so there's really no need to access s after the loop.

As I noted in a comment, at the end of the s loop, the value of s would be n - 1 - i; you can use that in the limit expression of the next loop: for (int h = 0; h < n - (n - 1 - i); h++), but then you can simplify it to for (int h = 0; h < i + 1; h++), so you really don't need to be able to access s after its loop ends.

The output from this jumbled loop code for height 7 looks like:

Height: 7
 #######
 ######
 #####
 ####
 ###
 ##
 #######
 ######
 #####
 ####
 ###
 #######
 ######
 #####
 ####
 #######
 ######
 #####
 #######
 ######
 #######

If you fix the code so that the h loop is not inside the s loop and fix the limit on the h loop as discussed, you end up with code like:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n = get_int("Height: ");
    for (int i = 0; i < n; i++)
    {
        // s stands for spaces
        for (int s = 0; s < n - 1 - i; s++)
            printf(" ");
        // h stands for hashes
        for (int h = 0; h < i + 1; h++)
            printf("#");
        printf("\n");
    }

    printf("\n");
}

The corresponding output for height 7 is:

Height: 7
      #
     ##
    ###
   ####
  #####
 ######
#######

This looks like what is wanted.

Upvotes: 1

Stephan Lechner
Stephan Lechner

Reputation: 35154

A variable declared in a for-loop is not accessible outside the scope of this loop. If you want to access a variable outside the loop, define/declare it outside of the loop; for example:

// s stands for spaces
int s;
for (s=0; s < n - 1 - i; s++) {
   printf(" ");
}

// h stands for hashes
for (int h = 0; h < n - s; h++) { 
   ...

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49873

You can declare the index variable (s) outside the loop, so that it will retain its value once the loop ends for you to do with whatever you want.

Upvotes: 1

Related Questions