THREAD HIJACK...for the time being.
I already created a thread about this but i really need any help ASAP, thanks.
Question 1
Part 1
Create a procedure called printSquareStar(), using printNString(string, numTimes), that prints the following using printNString.
****
****
****
****
This is a 4x4 square made up of the star (*) character.
Answer
def printSquareStar():
printNString('*', 4)
printNString('*', 4)
printNString('*', 4)
printNString('*', 4)
NOTE: This part is done
Part 2
Create a procedure printStarRectangle(height, width) (where string is a string and height is an integer) that prints a rectangle height units high and width units wide using '*'s. You should use printNString.
Example: printStarRectangle(5,3) =>
***
***
***
***
***
Answer
def printStarRectangle(height, width):
n = 1
while n <= height:
printNString('*', width)
n = n + 1
NOTE: This part is done
Part 3
Create a method called printTriangle(string, height) (where string is a string and height is an integer) that prints a right angled triangle height units high using string as the character string.
Example:
printTriangle('*', 2) =>
*
**
printTriangle('a', 3) =>
a
aa
aaa
Part 4
The goal of this exercise is to write a function that can draw two different types of shapes. Create a method printShape(shape, string, height) (where shape is a string, string is a string and height is an integer) that prints either a triangle or a square to the console depending upon the shape string. The variable shape will equal to 'square' when you are to print a square and otherwise it will be 'triangle'.
For the purpose of this exercise you may assume a printSquare(string, height) function and a printTriangle(string, height) function exist.
Question 2
Define a procedure p2(n) that takes an integer parameter n. If n is greater than 1, the procedure returns the largest power of two that is less than n; otherwise, it returns 0.
Hint: You will need to use a while loop. Here is an example of a function that adds the numbers from 1 to n
def sumto(n):
sum = 0
m = 1
while m <= n:
sum = sum + m
m = m + 1
return sum
For your problem you will need to take increasing powers of 2 until that power exceeds n.