For loop producing unexpected results in C

Kernel Pource

I just started doing the cs50 course but having trouble with the first problem sheet. The aim is to produce right aligned steps (steps you use in Mario just before finishing the level and jump on the flag pole).

The code I have written produces the steps but not right aligned.

But why does my code produce the steps? Shouldn't the condition for the second for loop always be false since i = j when the second for loop is executed?

Sorry if this has been answered before but I'm having trouble articulating my question.

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

int main(){

    int height = get_int("How tall are the pyramids: ");

    for (int i = 0; i < height; i++){
        printf("##");

        for (int j = 0; j < i; j++){
            printf("#");
        }
        printf("\n");
    }
}
Bwebb

Copy and paste this into codechef.com/ide using GCC 6.3. There are a few sample pryamids, left right center both upside down and right side up. To answer your specific quesiton, j is assigned to 0 in the second (nested) for loop, so j is not = to i at the start of the loop.

#include <stdio.h>
int main(){

    int height = 10;//get_int("How tall are the pyramids: ");

    printf("regular left justified\n");
    for (int i = 0; i < height; i++){
        printf("##");

        for (int j = 0; j < i; j++){
            printf("#");
        }
        printf("\n");
    }

    printf("Upside down left justified\n");
    for (int i = height; i >0 ; i--){
        printf("##");

        for (int j = 0; j < i; j++){
            printf("#");
        }
        printf("\n");
    }

    printf("upside down centered pryamid\n");
    for (int i = 0; i < height; i++){
        //printf("##");

        for (int j = 0; j < i; j++){
            printf(" ");
        }
        for( int j=i; j<height; j++){
            printf("##");    
        }
        printf("\n");
    }

    printf("regular centered pryamid\n");
    for (int i = 0; i < height; i++){
        //printf("##");

        for (int j = height; j > i; j--){
            printf(" ");
        }
        for( int j=height; j>=height-i; j--){
            printf("##");    
        }
        printf("\n");
    }

    printf("regular right justified\n");
    for (int i = 0; i < height; i++){
        //printf("##");

        for (int j = height; j > i; j--){
            printf(" ");
        }
        for( int j=height; j>=height-i; j--){
            printf("#");    
        }
        printf("\n");
    }

    printf("upside down right justified\n");
    for (int i = 0; i < height; i++){
        //printf("##");

        for( int j=height; j>=height-i; j--){
            printf(" ");    
        }

        for (int j = height; j > i; j--){
            printf("#");
        }

        printf("\n");
    }

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python 3.6 Regex Producing Unexpected Results (despite using string literals)

分類Dev

Array.prototype.reduce in recursive function is producing unexpected results

分類Dev

Why Is This Day Counter Producing The Wrong Results?

分類Dev

Using flextable in r markdown loop not producing tables

分類Dev

MultiThreading of Different Instances still producing same results, how to overcome this?

分類Dev

Adding dates in javascript with unexpected results

分類Dev

Why is my print statement producing no output after a while loop?

分類Dev

How to resolve the following program with a for loop into producing an appropriate output?

分類Dev

How to resolve the following program with a for loop into producing an appropriate output?

分類Dev

Loop results in order

分類Dev

Bitwise operation results in unexpected variable size

分類Dev

Testing of spring batch job causes unexpected results

分類Dev

Perl utf8 binmode unexpected results

分類Dev

csv.DictReader giving unexpected results

分類Dev

Python numpy unexpected results in arange and linspace

分類Dev

Binary reading with python gives unexpected results

分類Dev

Alter materialized view in BigQuery results in "Unexpected statement"

分類Dev

Query with several consecutive LEFT JOIN - unexpected results

分類Dev

deepcopying function variable gives unexpected results

分類Dev

Unexpected Results with Copy Command and Excel Files

分類Dev

Why is my program for calculating the woodall numbers producing wrong results after n >47?

分類Dev

C# foreach loop is only displaying 2 results within the entire json file

分類Dev

Collecting IronPython Parallel Loop results

分類Dev

Getting duplicated results in foreach loop

分類Dev

Unexpected T_ENDWHILE in loop.php

分類Dev

Uncaught SyntaxError、Unexpected Identifier in for loop injade

分類Dev

Polymorphism with a dereferenced pointer yields unexpected results...why?

分類Dev

Laravel 5: API routes + wildcard route results in unexpected behavior

分類Dev

UWP Setting RadioButton.IsChecked in codebehind results in strange/unexpected behavior

Related 関連記事

  1. 1

    Python 3.6 Regex Producing Unexpected Results (despite using string literals)

  2. 2

    Array.prototype.reduce in recursive function is producing unexpected results

  3. 3

    Why Is This Day Counter Producing The Wrong Results?

  4. 4

    Using flextable in r markdown loop not producing tables

  5. 5

    MultiThreading of Different Instances still producing same results, how to overcome this?

  6. 6

    Adding dates in javascript with unexpected results

  7. 7

    Why is my print statement producing no output after a while loop?

  8. 8

    How to resolve the following program with a for loop into producing an appropriate output?

  9. 9

    How to resolve the following program with a for loop into producing an appropriate output?

  10. 10

    Loop results in order

  11. 11

    Bitwise operation results in unexpected variable size

  12. 12

    Testing of spring batch job causes unexpected results

  13. 13

    Perl utf8 binmode unexpected results

  14. 14

    csv.DictReader giving unexpected results

  15. 15

    Python numpy unexpected results in arange and linspace

  16. 16

    Binary reading with python gives unexpected results

  17. 17

    Alter materialized view in BigQuery results in "Unexpected statement"

  18. 18

    Query with several consecutive LEFT JOIN - unexpected results

  19. 19

    deepcopying function variable gives unexpected results

  20. 20

    Unexpected Results with Copy Command and Excel Files

  21. 21

    Why is my program for calculating the woodall numbers producing wrong results after n >47?

  22. 22

    C# foreach loop is only displaying 2 results within the entire json file

  23. 23

    Collecting IronPython Parallel Loop results

  24. 24

    Getting duplicated results in foreach loop

  25. 25

    Unexpected T_ENDWHILE in loop.php

  26. 26

    Uncaught SyntaxError、Unexpected Identifier in for loop injade

  27. 27

    Polymorphism with a dereferenced pointer yields unexpected results...why?

  28. 28

    Laravel 5: API routes + wildcard route results in unexpected behavior

  29. 29

    UWP Setting RadioButton.IsChecked in codebehind results in strange/unexpected behavior

ホットタグ

アーカイブ