-2

[ EDIT: Code has been corrected & output examples added invalidating older comments below. Next time will be better. ]

been trying this cs50 mario more problem for awhile now but i cant seem to find a solution, the right pyramid seems to print out way more than it is supposed to for some reason, my code is below, appreciate any help i can get. thanks

#include <cs50.h>
#include <stdio.h>
int h;
int k;
int j;
int main(void)
{
    while (h<=0 || h>8)
    {
        h = get_int ("input pyramid height:");
    }

    for(j=0; j<h; j++)
    {
        for( k=0; k<=h; k++)
        {
            if (j+k<h)
                printf(" ");
            else
                printf("#");
        }
        printf("  ");

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

this is what im supposed to get for height = 4. (sorry for the lack on info earlier)

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

what i get is this:(

   #  #
  ##  ###
 ###  ######
####  ##########
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • 1
    Why do you define your variables as global? That's not really recommended. – Some programmer dude Sep 12 '22 at 09:43
  • As for your problems, first begin by using your editors built-in functionality to format your code nicely and consistently. All decent code-oriented editor will have such a feature. It will make it easier to read and understand you code Then learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code statement by statement, while monitoring variables and their values. – Some programmer dude Sep 12 '22 at 09:46
  • 3
    I think that `for (int y=0; y<=x; y++)` is not required. – Ferenc Deak Sep 12 '22 at 09:47
  • Link to the desired result needed – Sequoya Sep 12 '22 at 10:05
  • 2
    How does "print out way more than it is supposed to" look like? Can you share the output you get in contrast to the one you expect? – Yunnosch Sep 12 '22 at 10:07
  • For loop analysis like this, applying consistent indentation is very helpful, if not 90% of the solution. – Yunnosch Sep 12 '22 at 10:07
  • I vote to close for being caused by a "typo/thinko", being the existence of `for (int y=0; y<=x; y++)`. Do some rubberducking and explain to yourself the reason for that innner loop. If you can, [edit] the question with that reason and if it is sound I retract the close vote. If you need help with reasoning add a `printf(".");` after that inner loop, to see its effect and to judge whether you want it. Do that in properly indented code. – Yunnosch Sep 12 '22 at 10:14
  • 1
    We're not in your class and don't know what "cs50 mario more problem" means or what the expected and actual outputs are. – interjay Sep 12 '22 at 10:20
  • @Yunnosch sorry for not adding my desired output earlier i just edited my post would appreciate if you could take a look – clarencebutterbroom Sep 12 '22 at 16:47
  • @interjay my bad i did not attach a photo of the desired output would really appreciate if you could take a look – clarencebutterbroom Sep 12 '22 at 16:48
  • @Sequoya i just linked the output so sorry for leaving it out – clarencebutterbroom Sep 12 '22 at 16:49
  • I do not see the desired output. Only broken syntax in wrong formatting, possibly meant to be link to picture. Please instead provide textual information as text. – Yunnosch Sep 12 '22 at 16:52
  • 1
    @Fe2O3 The primary goal of any old-timer is of course to demonstrate superiority over newcomers, via various shibboleths and put-downs, subtle or overt. Long-term consistency is of no consequence: if today's put-down is, as you seem to suggest, the opposite of yesterday's, so what? And what's the aggrieved newcomer going to do, *complain* about it? No, newcomers are to be put in their place, not paid attention to. – Steve Summit Sep 12 '22 at 21:48
  • @SteveSummit "Social media" at its best sometimes... "There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand the world. There is no such thing as a dumb question." -- Carl Sagan... U+1F44D <- Unicode "thumbs up" `:-)` – Fe2O3 Sep 12 '22 at 22:06
  • @clarencebutterbroom The worst state is to be obstructed. The usual pushing harder against an immovable object benefits no-one. It's sometimes good to take a step back, possibly even starting from scratch... You were almost there, but seemed to think "I just need MORE variables!"... The best code is the simplest code that does its job well... Cheers and good luck with your studies. `:-)` – Fe2O3 Sep 13 '22 at 07:36

1 Answers1

0

Without giving away too much, "examine the problem"...

0th row: print n-0 spaces, the center '#' and LF
1st row: print n-1 spaces, 1x'#', the center again, and 1x'#' (and LF)
2nd row: print n-2 spaces, 2x'#', the center again, and 2x'#'...
3rd row: print n-3 spaces, 3x'#', the center again, and 3x'#'...

Do you see a simple pattern forming here.

Now flip the row numbering vertically:

8th row: print (maybe) 8 spaces, then 1 '#'
7th row: print...
6th row: pr...

Too many variables counting up and down and adding together makes a person's head swim...

Simplify the problem and the pieces should fall into place.

Start fresh. Print a 'pyramid' that is only on one row, then extend that to two rows.

EDIT:

The foregoing was based on my erroneous understanding of the desired output. The "hollow core" of the desired output now makes the title "mario" less random and more significant.

Now that you have posted a 'tidier' version of code, and (with some polishing) the desired/actual output, it is appropriate (on SO) to help with the difficulty you are experiencing...

The 2nd inner for() loop should be the mirror image of the good 1st for() loop. Just count down.

for( k=0; k<=h; k++) // ascending left
{
    if (j+k<h)
        printf(" ");
    else
        printf("#");
}

printf("  ");

for( k=h; k>0; k--) // descending right side
{
    if (j+k<h)
        printf(" ");
    else
        printf("#");
}

printf("\n");

This outputs needless spaces, but the code is straightforward becoming "obvious" to the reader.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20