Thread: exercise: Print out the sign 'X' with the size depending on an input-value.

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    exercise: Print out the sign 'X' with the size depending on an input-value.

    Hi!

    I want to print out a 'X' with as many rows, as an input-value would have.
    This picture will describe hopefully better, what I want to do:

    Screenshot - 0781ad21c51043868573c8ecc3b4b448 - Gyazo

    I have broken this task down in
    a.) at first I only consider for n being an odd integer value.
    b.) At first I do row 1 - 4 (basically a 'V' sign, we could say).

    My code so far:

    Code:
    #include <stdio.h>
    
    int main() {
        int i, j, n;
        scanf("%d", &n);
    
    
        for (i = 0, j = n - 2 ; i <= (n/2), j >= -1; i++, j = j - 2) {
            printf("%*s", i, "");
            printf("*");
            printf("%*s", j, "");
            printf("*\n");
        }
    
    
         return 0;
    }

    Also I have another fundamental question:
    When I would nest a for loop in another one, then the outer loop makes one iteration and arrives in the inner loop.
    The inner loop now finishes all its iterations (until cancel condition is reached) and goes back to the outer loop which now would start its second iteration.

    But what tool we use, instead of nested for loops, when we want a loop with lets say five iterations iterate only one and then want a following for loop also iterate only one-time, before it goes back to the first loop for its second iteration, after from there to second loop for its second iteration and so on...?

    Also, can someone point me out what is happening in this code:

    Code:
    #include <stdio.h>
    
    int main() {
        int i, j, n;
        scanf("%d", &n);
    
    
        for (i = 0; i <= (n/2); i++) {
            printf("%*s", i, "");
            printf("*");
            for(j = n-2; j >= 0; j = j - 2) {
                printf("%*s", j, "");
                printf("*\n");
            }
        }
    
    
         return 0;
    }

    Regards,
    Placebo

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    > for (i = 0, j = n - 2 ; i <= (n/2), j >= -1; i++, j = j - 2)
    Instead of alphabet soup, try
    Code:
    for ( row = 0 ; row < n ; row++ ) {
        int initialIndent = /* your calculation */;
        int spacesBetween = /* your calculation */;
        printf("%d %d\n", initialIndent, spacesBetween);
    }
    Make this output the sequence
    0 5
    1 3
    2 1
    3 0
    2 1
    1 3
    0 5


    When you can output that successfully, adding the right printf statements is easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Hi!

    Thanks for your initial help.
    Before I try again to print out an X, I want to understand and finally execute your exercise (printing out the choosen sequence).

    I post here my first attempt and will afterwards describe my problems, so that you can see them:

    Attachment 15735


    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main() {
    int row, n = 7, initialIndent, spacesBetween = n;
    
    
        for ( row = 0 ; row < n ; row++ ) {
        int initialIndent = row;
        int spacesBetween = spacesBetween - 2;
        printf("%d %d\n", initialIndent, spacesBetween);
        }
    }
    1.) After declaration, I have provided the variable n the value of 7.
    Now I have provided to the variable spacesBetween the same value of n, so it should be 7.
    My calculation for spacesBetween is per loop:
    spacesBetween = spacesBetween - 2;

    Why is the first output 6 and not 5?

    2.) With your code-hint?
    How can I reverse both sequences after the first 4 loops?
    For instance, when we look at initialIndent:
    I can do 0, 1, 2, 3;
    And the in a separate loop 2, 1, 0;
    But I cannot do this switch in one loop and its one calculation - is this possible?

    Although, even with separating the increasing sequence form the decreasing sequence and by using "spaghetti" code, I cannot execute your sequence
    What is happening in regards to the red-highlighted output?

    exercise: Print out the sign 'X' with the size depending on an input-value.-sequences2-png

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    It would help if you could put your code as text we can copy/paste.

    But you might want to compile with
    -Wshadow
    Using the GNU Compiler Collection (GCC): Warning Options
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by Salem View Post
    It would help if you could put your code as text we can copy/paste.

    But you might want to compile with
    -Wshadow
    Using the GNU Compiler Collection (GCC): Warning Options
    Hey, I did put the relevant code in text using the CODE-tags - is this what you mean?
    The second code can be ignored.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    Ah, so you did, sorry.

    > int initialIndent = row;
    > int spacesBetween = spacesBetween - 2;

    vs

    > int row, n = 7, initialIndent, spacesBetween = n;

    You're redeclaring the variables INSIDE the loop.

    Just delete the word 'int' from lines 9 and 10.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    Code:
    int spacesBetween = spacesBetween - 2;
    That line doesn't use the spacesBetween variable that was defined 5 lines above it. Rather, it defines a new variable called spacesBetween and initializes it with its own value minus 2. Since that spacesBetween was not initialized before its value is used, it it set to a garbage value.

  8. #8
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Thanks salem!
    I will post in a bit my new code with the new adjustment (still cannot excel your given task before, fully).

    @christop:
    Thanks for the explantation - makes sense to me.
    The garbage value would have been in this case 8 (8 -2 = 6) - was this a random value which was taken from the stack or where did it even come from?

    Now, my new code after your hints:
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main() {
    int row, n = 7, initialIndent, spacesBetween = n;
    
    
    
    
        for ( row = 0 ; row < n ; row++ ) {
        initialIndent = row;
        spacesBetween = spacesBetween - 2;
        printf("%d %d\n", initialIndent, spacesBetween);
        }
    }

    Output:
    exercise: Print out the sign 'X' with the size depending on an input-value.-output3-png


    I have really no idea how to reverse the sequence within the for-loop without using a second for loop and basically breaking your task down in two separate tasks:
    1.) one for-loop
    0 5
    1 3
    2 1
    3 0

    2.) second for-loop
    2 1
    1 3
    0 5

    Is there a way to do the reversing in one loop or how you would go about your task?

    edit:
    Also the first part of your task is still wrong with my latest adjustment, since at row 4 within the second column, it should outprint 0 and not -1 - also here I have no clue how modify it correctly.
    Last edited by Placebo; 04-21-2019 at 05:55 AM.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Start at the first and last elements of the array and work your way to the middle swapping them over.

    As an exercise, open a spreadsheet and make a1=1, a2=2,... a5=5.

    Use b1 like a temp variable

    Using 'copy and paste' Swap the values of a1 and a5, making sure that you store the value of one in b1 so you don't lose it.

    Next a2 and a4, and then a3 doesn't need to be swapped. However, if there is an even amount of elements, there will be no centre one.

    This is the algorithm that you will need to use in your code.

    When you are struggling to conceptualise an algorithm, it helps to go through it manually. Spreadsheets are great for this, notepad for pseudo code, or just the humble pen and paper

  10. #10
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Somehow this thread does not bring me closer on topic tbh - I will try your exercise advice and the last one (pen and paper is anyways my default start), but can someone please concretely post here his code-solution for:

    "Make this output the sequence
    0 5
    1 3
    2 1
    3 0
    2 1
    1 3
    0 5
    "

    So that I can see, where I was wrong?

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    but can someone please concretely post here his code-solution for...
    You may want to read the homework policy...
    Homework Policy


    I think that once you get the algorithm down, the code will be easier to write and debug.

    When something doesn't work, the first thing that you need to ask is, "what is it supposed to do?"; closely followed by, "what is the code doing differently from what I want it to do?" - When you have an algorithm you can answer those questions.

    The copy/paste exercise that I told you to do is a dead give-away for the algorithm. Give it a go!


    Another helpful thing that I noticed is your indentation is a bit off - The good news is I also see that you are using code::blocks - In the title menu (where file/edit/view/... is) click "plugins", "source code formater (AStyle)" - You're welcome.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    I mean, it's not that I have not been trying to solve the given exercise (number-sequences) myself - then I have posted my results where I can see what the code does different than what I want it to do, posted some follow up questions and did not hear back.
    I just do not want to jump right away to your exercise while the first one is not done yet.
    Anyways, thanks for the hint in regards to style and codeblocks...


    btw., lol - I somehow managed to finish the very original exercise (printing out an X-sign - mentioned it in my first post), but the code looks terrible and it feels that the learning-effect was low, since it feels that I have missed core strategies there:

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main() {
        int z, s, leer_li, leer_re, n;
        printf("Please enter the number of rows: ");
        scanf("%d", &n);
    
    
        if (n % 2 != 0) {
                for (z = 1; z <= n/2; z++) {
                    printf("%*s", z - 1, "");
                    printf("*");
                    for (s = 1; s <=1; s++) {
                        printf("%*s" , n - 2*z, "");
                        printf("*\n");
                    }
                }
                    printf("%*s", z - 1, "");
                    printf("*\n");
    
    
                    for (z = n/2; z >= 1; z--) {
                        printf("%*s", z - 1, "");
                        printf("*");
                        for (s = 1; s <=1; s++) {
                            printf("%*s" , n - 2*z, "");
                            printf("*\n");
                        }
                    }
                } else {
                    for (z = 1; z < n/2; z++) {
                        for (leer_li = 1; leer_li <= 1; leer_li++) {
                            printf("%*s", z - 1, "");
                            printf("*");
                        }
                        for (leer_re = 1; leer_re <= 1; leer_re++) {
                            printf("%*s", n - 2*z, "");
                            printf("*\n");
                        }
                    }
    
    
                    for (leer_li = 1; leer_li <= 2; leer_li++) {
                        printf("%*s", z - 1, "");
                        printf("**\n");
                    }
    
    
                    for (leer_li = n/2 - 2; leer_li >= 0; leer_li--) {
                        printf("%*s", leer_li, "");
                        printf("*");
                        for (leer_re = 1; leer_re <= 1; leer_re++) {
                            printf("%*s", (n - 2) - 2 * leer_li, "");
                            printf("*\n");
                        }
    
    
                    }
    
    
                }
    
    
       return 0;
    }
    It somehow works, but feels still off.
    Would still like to know how this one works (in dependence of a number-variable n) :
    "Make this output the sequence
    0 5
    1 3
    2 1
    3 0
    2 1
    1 3
    0 5
    "

    I feel that being able to put out this sequence would have currently a large learning impact.
    After that, I might wanna try your exercise, but need to reread and try to understand it and/or get familiar with spreadsheets at first...


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-20-2016, 07:45 AM
  2. Print out all input values
    By DecoratorFawn82 in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2013, 11:42 AM
  3. 3 input to print 3 outputs at the same time
    By jason007thomas in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2010, 05:48 AM
  4. How do I input and print out a list of names
    By artistunknown in forum C Programming
    Replies: 3
    Last Post: 02-09-2010, 08:46 PM
  5. TI 89 Input routine with comma, backspace and sign
    By overspray in forum C Programming
    Replies: 1
    Last Post: 01-14-2003, 05:51 AM

Tags for this Thread