Question

//Write a function in C++ that will ask the user to enter the height, width, //and...

//Write a function in C++ that will ask the user to enter the height, width, //and color of 5 rectangles. The function takes as parameters an //array of RECTANGLE and its size.

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

Please find the function below:

void inputRect(int rectangle[][2],string rColor[],int size)
{   
   int i;   
   for(i=0;i<size;i++)   
   {
       cout<<endl<<"Enter height of rectangle "<<i+1<<" : ";   
       cin>>rectangle[i][0];
      
       cout<<endl<<"Enter width of rectangle "<<i+1<<" : ";   
       cin>>rectangle[i][1];
      
       fflush(stdin);
       cout<<endl<<"Enter color of rectangle "<<i+1<<" : ";
       cin>>rColor[i];
   }
   outputRect(rectangle,rColor,size);
}

===========================================================================

In the above function, you may see that I took 2 arrays 1) rectangle 2) rColor

rectangle -> will store the height and width

rColor -> will store color.

2 arrays were essential because color is of type string and height & width are of type int. we cannot store 2 different types of data into single array,

==========================================================================

Please find the program below, which includes the above function:

#include<iostream>                                                                       // include header files
using namespace std;                                                                  

void inputRect(int[][2],string[],int);                                                   // declare functions
void outputRect(int[][2],string[],int);

void outputRect(int rectangle[][2],string rColor[],int size)                           // outputRect(), takes rectangle 2d array, rColor array and size as parameter
{                                                                                       // rectangle array contains height and width, whereas rColor contains color of the rectangle
   int i;                                                                               // create a loop variable i
   cout<<endl<<endl<<"********** OUTPUT **********";
   for(i=0;i<size;i++)                                                                   // create a loop from 0 till size provided as a parameter
   {                                                                                   // display height, width and color of the rectangle
       cout<<endl<<"Rectangle "<<i+1<<" : Height = "<<rectangle[i][0]<<", Width = "<<rectangle[i][1]<<", Color = "<<rColor[i];
   }
}

void inputRect(int rectangle[][2],string rColor[],int size)                               // inputRect(), takes rectangle 2d array, rColor array and size as parameter
{                                                                                       // rectangle is used to store height & width, 1st index to store height, 2nd to store width
   int i;                                                                               // declare loop variable i      
   for(i=0;i<size;i++)                                                                   // create a loop from 0 till size provided as a parameter  
   {
       cout<<endl<<"Enter height of rectangle "<<i+1<<" : ";                           // Ask user to enter height of rectangle
       cin>>rectangle[i][0];                                                           // store the height in the 1st index of the rectangle
      
       cout<<endl<<"Enter width of rectangle "<<i+1<<" : ";                           // Ask user to enter width of rectangle
       cin>>rectangle[i][1];                                                           // store the width in the 2nd index of the rectangle
      
       fflush(stdin);                                                                   // flush the input buffer to get the character/string input
       cout<<endl<<"Enter color of rectangle "<<i+1<<" : ";                           // Ask user to enter color of rectangle
       cin>>rColor[i];                                                                   // store the input in rColor array.
   }
   outputRect(rectangle,rColor,size);                                                   // call outputRect to display the details entered
}


int main()                                                                               // main()          
{
   int rectangle[5][2];                                                               // create a rectangle 2d array to store height and width, with size 5
   string rColor[5];                                                                   // create an array rColor to store the color, size 5
  
   inputRect(rectangle,rColor,5);                                                       // call inputRect(), and pass rectangle, rColor and size as parameter
}

================================================================

Output:

======================================================================

Screenshot:

Add a comment
Know the answer?
Add Answer to:
//Write a function in C++ that will ask the user to enter the height, width, //and...
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
  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Question 4.4: Write an overloaded function of function area with 3 (float) parameters. This function should...

    Question 4.4: Write an overloaded function of function area with 3 (float) parameters. This function should calculate and print out the product of the 3 parameters. Question 4.4: Write the main function to test question 4.1, 4.2, 4.3, 4.4. Your main function should ask if the user want to calculate the area of a rectangle or a circle or a triangle then print out the result accordingly. (Use switch structure). For example: Please enter (r) for rectangle, (t) for triangle,...

  • Problem 2 Width and height of 10 rectangles have been stored in two arrays called W...

    Problem 2 Width and height of 10 rectangles have been stored in two arrays called W and H. Write a C program that will pass these arrays to a custom written function which will calculate the area of each rectangle and store the results in a third array called A. Your function should also print the area for the largest rectangle. W 12 18 5 17 6 6 22 5 8 H 10 23 4 11 2 9 35 7...

  • use C++ to write the following program. needs to ask the user for n The user...

    use C++ to write the following program. needs to ask the user for n The user must put in those 5 values, probably using a for loop. NOTE: You can assume the number of values to be entered is 5. My attempted program below: (not correct, but just a basis) 4. Larger Than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume that the array contains...

  • Write a function to have a user enter some number of integers into an array. The...

    Write a function to have a user enter some number of integers into an array. The integer values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integer array and the size of the array are passed into the function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform...

  • using python3 Write a Python program that will ask the user for: 1. The length of...

    using python3 Write a Python program that will ask the user for: 1. The length of a rectangle. 2. The width of a rectangle and will print out the length of the diagonal of the rectangle. So your program should look like this: Please enter the length of the rectangle: Please enter the width of the rectangle: The diagonal of the rectangle is: So the user enters the length, width and then the answer is printed.

  • IN PYTHON. Write a function that ask the user to (a) enter a string, (b) ask...

    IN PYTHON. Write a function that ask the user to (a) enter a string, (b) ask the number of time to repeat the string and (c) ask user to input a delimiter to separate them. Call the function and append the test run to your solution. Here is a test run of what is wanted: Enter a string: Ted How many repetitions? 10 Separated by? [}(+ Ted[}(+Ted[}(+Ted[}(+Ted[}(+Ted[}(+Ted[}(+Ted[}(+Ted[}(+Ted[}(+Ted

  • Write a program to display the area of a rectangle after accepting its length and width...

    Write a program to display the area of a rectangle after accepting its length and width from the user by means of the following functions: getLength – ask user to enter the length and return it as double getWidth – ask user to enter the width and return it as double getArea – pass width and length, compute area and return area displayArea – pass area and display it in this function. Expected Output: (i) Enter the length: 29 Enter...

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • In python, Write a function that will ask user to enter a number and find if...

    In python, Write a function that will ask user to enter a number and find if it is odd or even. Example run: Please enter a number: 5 5 is an odd number Please enter a number: 10 10 is an even number

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