Question

In: Computer Science

a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it...

a) Write a function drawShape() that accepts a parameter n, and:

  • If n is odd, it constructs a pattern of a diamond of height n
  • If n is even, it constructs an hourglass of length n

b) What is the time complexity of the drawShape() function you created?

C++ language with for loop

Solutions

Expert Solution

#include <iostream>

using namespace std;

void drawShape(){

int n;

cout<<"Enter the Parameter N"<<endl;

cin>>n;

if(n%2!=0){

cout<<"The Number you entered is ODD"<<endl;

int c, k, count=1;

count=n-1;

for (k=1; k<=n; k++)

{

  for(c=1; c<=count; c++)

  {

    cout<<" ";

  }

  count--;

  for(c=1; c<=(2*k-1); c++)

  {

    cout<<"*";

  }

  cout<<"\n";

}

count=1;

for(k=1; k<=(n-1); k++)

{

  for(c=1; c<=count; c++)

  {

    cout<<" ";

  }

  count++;

  for(c=1 ; c<=(2*(n-k)-1); c++)

  {

    cout<<"*";

  }

  cout<<"\n";

}

// getch();

}

else if(n%2==0){

cout<<"The Number you entered is EVEN"<<endl;

int i,j,k=9,s=0,l;

// clrscr();

for(i=0;i<5;i++,k-=2,s+=1)

{

for(l=s;l>=0;l--)

cout<<' ';

for(j=k;j>0;j--)

cout<<"#";

cout<<endl;

}

for(i=0;i<5;i++,k-=2,s-=1)

{

for(l=s-1;l>=0;l--)

cout<<' ';

for(j=k;j<0;j++)

cout<<"#";

cout<<endl;

}

// getch();

}

}

int main() {

drawShape();

return 0;

}

PLEASE GIVE A THUMBS UP!!!!!!!!!!!


Related Solutions

Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd Binary numbers // 00000000, 0101, 111111, etc. Show: Finite Automaton Definition, Graph, Table
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT