11 Replies - 417 Views - Last Post: 17 November 2017 - 06:06 PM Rate Topic: -----

#1 shiftadelete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 04-November 16

What is wrong with my code?

Posted 16 November 2017 - 03:54 PM

Hi, I want to print this star shape:
___*
__**
_***
****


I wrote the codes but my output is totally different
here is my code:

Module Module1

    Sub Main()
        Dim x As Integer
        Dim y As Integer
        Dim i As Integer

        x = Console.ReadLine()
        y = Console.ReadLine()

        For i = 1 To x
            For y = i To x - 1
                Console.Write("_")
            Next

            For y = 1 To i
                Console.WriteLine("*")
            Next
            Console.WriteLine()
        Next
    End Sub

End Module



Is This A Good Question/Topic? 0
  • +

Replies To: What is wrong with my code?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13659
  • View blog
  • Posts: 54,529
  • Joined: 12-June 08

Re: What is wrong with my code?

Posted 16 November 2017 - 03:58 PM

What output are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 shiftadelete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 04-November 16

Re: What is wrong with my code?

Posted 16 November 2017 - 04:01 PM

modi123_1 https://imgur.com/a/Fr0Hy here you go

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13659
  • View blog
  • Posts: 54,529
  • Joined: 12-June 08

Re: What is wrong with my code?

Posted 16 November 2017 - 04:12 PM

What's the obvious issues? First I see too many line breaks. Perhaps get ride of one or stop using the 'write line'. See what that does.
Was This Post Helpful? 0
  • +
  • -

#5 shiftadelete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 04-November 16

Re: What is wrong with my code?

Posted 16 November 2017 - 04:22 PM

modi123_1 It seems it has a problem right there
For y = i To x - 1
                Console.Write("-")
            Next


but when I trade them, there is no problem.

here c codes for it. It is the same. May be the "x - 1" causing to the problem.
#include <stdio.h>

int main()
{
    int i, j, rows;

   
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    
    for(i=1; i<=rows; i++)
    {
       
        for(j=i; j<rows; j++)
        {
            printf(" ");
        }

        
        for(j=1; j<=i; j++)
        {
            printf("*");
        }

        
        printf("\n");
    }
    
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 shiftadelete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 04-November 16

Re: What is wrong with my code?

Posted 16 November 2017 - 04:30 PM

it works with if-else :(

Module Module1

    Sub Main()
        Dim x As Integer
        Dim y As Integer
        Dim i As Integer

        x = Console.ReadLine()
        y = Console.ReadLine()

        For i = 1 To x
            For y = 1 To x
                If y <= (x - i) Then
                    Console.Write(" ")
                Else
                    Console.Write("*")
                End If
            Next
            Console.WriteLine()
        Next
    End Sub

End Module

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13659
  • View blog
  • Posts: 54,529
  • Joined: 12-June 08

Re: What is wrong with my code?

Posted 16 November 2017 - 04:39 PM

I did my fix and this is my output. Only one line needed to be changed.

4
4
___*
__**
_***
****

Was This Post Helpful? 0
  • +
  • -

#8 shiftadelete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 04-November 16

Re: What is wrong with my code?

Posted 17 November 2017 - 10:29 AM

modi123_1 I don't want you to tell me the answer. But I want you to help me to learn algorithm for star pattern questions. I really want to be good at coding. But I think I am an idiot to be good at it. How can I improve myself :( I am trying trying. But getting nothing.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13659
  • View blog
  • Posts: 54,529
  • Joined: 12-June 08

Re: What is wrong with my code?

Posted 17 November 2017 - 10:37 AM

Again.. think of what makes a line break and where you would need it. If you are getting too many then remove one.
Was This Post Helpful? 0
  • +
  • -

#10 shiftadelete  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 04-November 16

Re: What is wrong with my code?

Posted 17 November 2017 - 10:40 AM

modi123_1 I don't want the answer. I want to learn how to approach the question
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13659
  • View blog
  • Posts: 54,529
  • Joined: 12-June 08

Re: What is wrong with my code?

Posted 17 November 2017 - 10:53 AM

That is the approach. Eyeball the output. What do you have too much of? What do you not have enough of? Adjust accordingly.
Was This Post Helpful? 0
  • +
  • -

#12 Radius Nightly  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 207
  • Joined: 07-May 15

Re: What is wrong with my code?

Posted 17 November 2017 - 06:06 PM

I have somewhere too old code, for adding numbers to the right, like you have 00000000 and you add 1, you have 00000001, you add 350 more, then you have 00000351, similar thing to you star, and length can be chosen (like 00 or 00000... to start with) as well as alphabet, numbers and special characters or so, its more like generator used for links, not a VB.NET, but if you think functions like that may help you, just reading the code to give you come clue, i can try to find it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1