Printing patterns in Freebasic

New to FreeBASIC? Post your questions here.
ganache
Posts: 28
Joined: Aug 04, 2016 9:25

Printing patterns in Freebasic

Postby ganache » Jan 07, 2017 4:18

I am writing programs for printing patterns,just for fun.
I want to write a program to output an equilateral triangle(all sides are equal).
Below is the C code. How do I translate the for loops in Freebasic.

#include <stdio.h>
int main()
{
int i, j, n;
//Reads number of rows to be printed
printf("Enter value of n : ");
scanf("%d", &n);

for(i=1; i<=n; i++)
{
//Prints trailing spaces
for(j=i; j<n; j++)
{
printf(" ");
}

//Prints the pyramid pattern
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
BasicCoder2
Posts: 2624
Joined: Jan 01, 2009 7:03

Re: Printing patterns in Freebasic

Postby BasicCoder2 » Jan 07, 2017 4:29

Code: Select all

dim as integer n
'Reads number of rows to be printed
input "Enter value of n : ";n

for i as integer = 1 to n

    'Prints trailing spaces
    for j as integer = i to n-1
        print " ";    'semi colon prevents crlf
    next j

    'Prints the pyramid pattern
    for j as integer = 1 to 2*i-1
        print "*";
    next j
   
    print         'line feed
   
next i

print
print "Tap key to end"

sleep   'wait for key event

ganache
Posts: 28
Joined: Aug 04, 2016 9:25

Re: Printing patterns in Freebasic

Postby ganache » Jan 07, 2017 6:44

Thanks BasicCoder2!
Still coming to grips with converting c code to FB code.
ganache
Posts: 28
Joined: Aug 04, 2016 9:25

Re: Printing patterns in Freebasic

Postby ganache » Jan 10, 2017 5:06

Hi, me again.
I am trying to print different star patterns. BasicCoder2 had helped me with the equilateral triangle.
Here is the c code for printing a hollow square shape and below is my attempt.

' c code
#include<stdio.h>
int main(){
int side, i, j;

printf("Enter side of square\n");
scanf("%d", &side);

/* Row iterator for loop */
for(i = 0; i < side; i++){
/* Column iterator for loop */
for(j = 0; j < side; j++){
/* Check if currely position is a boundary position */
if(i==0 || i==side-1 || j==0 || j==side-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}

' FB code for hollow square
Dim As Integer n, i, j
input "Enter side of square:";n
' Row iterator for loop
for i = 0 to n
'Column iterator for loop
for j = 0 to n-1
' Check if currently position is a boundary position
if (i=0 or i=n-1 or j=0 or j=n-1) then print "*";
next j
print " ";
next i
Sleep
End
BasicCoder2
Posts: 2624
Joined: Jan 01, 2009 7:03

Re: Printing patterns in Freebasic

Postby BasicCoder2 » Jan 10, 2017 5:44

Code: Select all

dim as integer side

input "Enter side of square";side
print

for i as integer = 0 to side-1

    for j as integer =  0 to side-1

        if i=0 or i = side-1 or j=0 or j=side-1 then
            print "*";
        else
            print " ";
        end if
   
    next j

    print

next i

sleep
ganache
Posts: 28
Joined: Aug 04, 2016 9:25

Re: Printing patterns in Freebasic

Postby ganache » Jan 12, 2017 8:46

Thanks once again BasicCoder2!
MrSwiss
Posts: 1702
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Printing patterns in Freebasic

Postby MrSwiss » Jan 12, 2017 14:40

A speed improved version, which also corrects *out of range* user input.

Code: Select all

Dim As UInteger side, j

Input "Enter side of square: ", side    ' get user input
Print   ' CrLf = 'empty' line
' set min. size, with at least one 'empty' field
If side < 3 Then side = 3
' set max. size (22 lines, we've used 2 up to now)
If side > 22 Then side = 22
' pre-calculate variables used by programs 'for-loop'
Var smax  = side - 1    ' = integer (by assignment)
Var empty = side - 2    ' as above

For j = 0 to smax   ' print it: a line at the time (speed)
    If j = 0 OrElse j = smax Then
        Print String(side, "*") ' top and bottom lines only
    Else
        Print "*" + String(empty, " ") + "*" ' inbetween lines
    End If
Next

Sleep
@ganache,

plse. put posted code in future, inside code-tags. Use the code-button above 'text-entry-box'.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 4 guests