Question

Hello I need help fixing some things so that the C++ code follows all the requirements listed below. Thanks,

This needed to fix:

Rule 3 (Not Satisfied):

In rule 3 it is asked to use conditional statements only for the base-case and for all other conditional expressions you must use ternary operators. You have used "if", "else" statements multiple times so this rule was not satisfied.

Rule 6 (Not Satisfied):

As per this rule you are permitted to use upto 4 "cout" statements but you've used 6.

Rule 7 (Not Satisfied):

As per this rule, boundary straight line should be right on edge points or one beyond beyond edge but your boundary straight line is two points beyond the edge line.

Requirements:

Here are the required two function declarations for your recursion work: int main() { pattern( 0, 16 ); void pattern int left

Code:

#include<iostream>
#include<iomanip>

using namespace std;

void pattern(int left, int length)
{
if (length == 0) return;

pattern(left, length / 2);

(left*2 ) ? cout << "* " : cout << "" /*do nothing*/ ; //ternary statement

    for (int i = 0; i < left * 2 - 1; i++)
   cout << " ";
  

for (int i = 0; i < length; i++)
cout << "* ";
for (int i = 0; i < left * 2; i++)
cout << " ";

//( left*2 ) ? cout<<"*"<<endl : cout<<" *"<<endl ; //ternary statement instead of if else

cout<<"*"<<endl;

pattern(left + length / 2, length / 2);
}


int main()
{
pattern(0, 16);
}

Desired Output:

My Output:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Solution:

#include<iostream>
#include<iomanip>

using namespace std;

void pattern(int left, int length)
{
if (length == 0) return;

pattern(left, length / 2);

(left*2 ) ? cout << " " : cout << "" /*do nothing*/ ; //ternary statement

for (int i = 0; i < left * 2 - 1; i++)
cout << " ";
  
for (int i = 0; i < length; i++)
cout << "* ";

for (int i = 0; i < left * 2; i++)
cout << " ";

cout<<" "<<endl;

pattern(left + length / 2, length / 2);
}

//Main Function
int main()
{
//Call pattern and pass two integer parameters
pattern(0, 16);
}

SCREENSHOT OF OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Hello I need help fixing some things so that the C++ code follows all the requirements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help solving this basic C++ question. I need help fixing my code.   Question: Design...

    I need help solving this basic C++ question. I need help fixing my code.   Question: Design a class called a box that represents a box. Box classes have variables such as the length of the box, width, and height. 1. Member variables shall be dedicated members. 2. Define the creator of the Box Class. The creator may receive all of the data and may not receive any. 3. Add accessor and creator 4. Add an empty function, which indicates whether...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...

    I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void dump(int ar[], int size) { cout << "\nDUMP [ "; for (int i=0; i<=size; i++) { cout << ar[i] << " "; } cout << "]\n\n"; } void quicksort(int ar[], int start, int end) { cout << "TOP OF SORT ===========================" << endl; dump(ar, start, end); int pivot = ar[end]; int left = start; int right = end - 1; int tmp; cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT