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;
}
Printing patterns in Freebasic
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: Printing patterns in Freebasic
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
Re: Printing patterns in Freebasic
Thanks BasicCoder2!
Still coming to grips with converting c code to FB code.
Still coming to grips with converting c code to FB code.
Re: Printing patterns in Freebasic
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
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
-
- Posts: 3906
- Joined: Jan 01, 2009 7:03
- Location: Australia
Re: Printing patterns in Freebasic
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
Re: Printing patterns in Freebasic
Thanks once again BasicCoder2!
Re: Printing patterns in Freebasic
A speed improved version, which also corrects *out of range* user input.@ganache,
plse. put posted code in future, inside code-tags. Use the code-button above 'text-entry-box'.
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
plse. put posted code in future, inside code-tags. Use the code-button above 'text-entry-box'.