1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
serious [3.7K]
3 years ago
14

Write a function with two parameters, prefix (a string, using the string class from ) and levels (an unsigned integer). The func

tion prints the string prefix followed by "section numbers" of the form 1.1., 1.2., 1.3., and so on. The levels argument determines how many levels the section numbers have. For example, if levels is 2, then the section numbers have the form x.y. If levels is 3, then the section numbers have the form x.y.z. The digits permitted in each level are always '1' through '9'. As an example, if prefix is the string "BOX:" and levels is 2, then the function would start by printing:

Computers and Technology
1 answer:
Allisa [31]3 years ago
4 0

Answer:

Here is the program:

#include <iostream>  //to use input output functions

#include <string>  // to use functions to manipulate strings

using namespace std;  //to identify objects cin cout

void function(string prefix, unsigned int levels){  // function that takes two parameters, a string, using the string class and levels an unsigned integer

   if (levels == 0) {  //if number of levels is equal to 0

       cout << prefix << endl;  //displays the value of prefix

       return;    }  

  for (int i = 1; i <=9 ; i++){  //iterates 1 through 9 times

       string sections = (levels == 1 ? "" : ".");  //if the number of levels is equal to 1 then empty space in sections variable otherwise stores a dot

       string output = prefix +  std::to_string(i) + sections;   // displays the string prefix followed by the section numbers. Here to_string is used to convert integer to string

       function(output, levels - 1);   } }   //calls function by passing the resultant string and levels-1  recursively to print the string prefix followed by section numbers

int main() {  // start of main function

   int level = 2;  //determines the number of levels

   function("BOX", level);  } //calls function by passing the string prefix and level value

Explanation:

The program has a function named function() that takes two parameters, prefix (a string, using the string class from ) and levels (an unsigned integer). The function prints the string prefix followed by "section numbers" of the form 1.1., 1.2., 1.3., and so on. The levels argument determines how many levels the section numbers have. If the value of levels is 0 means there is 0 level then the value of prefix is printed. The for loop iterates '1' through '9' times for number of digits in each level. If the number of levels is 1 then space is printed otherwise a dot is printed. The function() calls itself recursively to print the prefix string followed by section numbers.

Let us suppose that prefix = "BOX" and level = 1

If level = 1 then the loop for (int i = 1; i <=9 ; i++) works as follows:

At first iteration:

i = 1

i <=9 is true because value of i is 1

string sections = (levels == 1 ? "" : "."); this statement checks if the levels is equal to 1. It is true so empty space is stored in sections variable so,

sections = ""

Next, string output = prefix +  std::to_string(i) + sections; statement has prefix i.e BOX plus value of i which is 1 and this int value is converted to string by to_string() method plus sections has an empty space. So this statement becomes

string output = BOX + 1  

So this concatenates BOX with 1 hence output becomes:

output = BOX1

At second iteration:

i = 2

i <=9 is true because value of i is 2

string sections = (levels == 1 ? "" : "."); is true so

sections = ""

Next, string output = prefix +  std::to_string(i) + sections; statement becomes

string output = BOX + 2  

So this concatenates BOX with 1 hence output becomes:

output = BOX2

At third iteration:

i = 3

i <=9 is true because value of i is 3

string sections = (levels == 1 ? "" : "."); is true so

sections = ""

Next, string output = prefix +  std::to_string(i) + sections; statement becomes

string output = BOX + 3  

So this concatenates BOX with 1 hence output becomes:

output = BOX3

Now at each iteration the prefix string BOX is concatenated and printed along with the value of i. So at last iteration:

At last iteration:

i = 9

i ==9 is true because value of i is 9

string sections = (levels == 1 ? "" : "."); is true so

sections = ""

Next, string output = prefix +  std::to_string(i) + sections; statement becomes

string output = BOX + 9  

So this concatenates BOX with 1 hence output becomes:

output = BOX9

After this the loop breaks at i = 10 because the condition i<=9 becomes false. So the output of the entire program is:

BOX1                                                                                                                                          BOX2                                                                                                                                          BOX3                                                                                                                                          BOX4                                                                                                                                          BOX5                                                                                                                                          BOX6                                                                                                                                          BOX7                                                                                                                                          BOX8                                                                                                                                          BOX9  

The program along with the output is attached.

You might be interested in
Please help lots of points worth
Artemon [7]

Answer:

i dont rlly know the first one i mean i guess you cud say the last one? i dunno  srry but the 2nd one is definitively    is Laura woke up to the alarm clock screaming

Explanation:

6 0
3 years ago
What happens during the production stage
PSYCHO15rus [73]

Answer:

Production is where the principal photography for the movie or TV show takes place.

Explanation:

During rehearsals and camera blocking, Stand-Ins work with the Director, Assistant Director, camera crew, and other crew members to block out actor movements and lighting set-ups for a scene.

4 0
3 years ago
Write a program that asks length,breadth and height of a room and calculate its volume​
marta [7]

Answer:

cls

Input"enter length";l

Input"enter breadth";b

Input"enter height";h

v=l*b*h

print"the volume is";v

end

Explanation:

hope you got it

6 0
3 years ago
Why does my Office Computer send packet #1? What device responds by sending back packet #2? What information does my Office Comp
AleksandrR [38]

Answer:

The office computer is part of a network.

The server responds with a packet when a request packet is sent by a client.

The computer confirms the IP address and port number to retrieve the encapsulated data, for a specific application in the system.

Explanation:

The computer network is the communication of two or more devices. There are two forms of network, there are peer to peer and client/server networks.

The peer to peer network allows for each computer to send and receive data, while the client/server network have a dedicated server to send resources to clients that request for it. Data transferred are encapsulated in the network, transport and data-link layer PDU headers and would be decided in the destination system to retrieve the data.

4 0
3 years ago
JAVA Programming.
jeka94

Answer:

// Java profram to perform Arithmetic operations on square matrices

// Comments are used for explanatory purpose

// Program starts here

import java.util.*;

import java.io;

public class Matrices {

// Multiply Method Here

static void multiply(int mat1[][],int mat2[][], int mulmat[][])

{

int i, j, k;

for (i = 0; i < N; i++)

{

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

{

mulmat[i][j] = 0;

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

mulmat[i][j] += mat1[i][k] * mat2[k][j];

}

}

System.out.println("Result matrix" + " is ");

for (i = 0; i < N; i++)

{

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

System.out.print( mulmat[i][j] + " ");

System.out.println();

}

}

// Subtraction Method here

static void sub(int mat1[][],int mat2[][], int submat[][])

{

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

addmat[i][j]=mat1[i][j]-mat2[i][j]; //

System.out.print(submat[i][j]+" ");

}

}

// Addition Method here

static void add(int mat1[][],int mat2[][], int addmat[][])

{

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

addmat[i][j]=mat1[i][j]+mat2[i][j]; //

System.out.print(addmat[i][j]+" ");

}

}

// Main Method Starts here

public static void main (String [] args)

{

Scanner input = new Scanner (System.in);

// Declare type of square matrix

int N;

// input N

lbl: N = input.nextInt();

if(N>=2 && N<=6)

{

int mulmat[][] = new int[N][N] ;

int addmat[][] = new int[N][N] ;

int submat[][] = new int[N][N] ;

// Multiplication

mulmat(mat1, mat2, mulmat);

// Addition

addmat(mat1, mat2, addmat);

// Subtraction

submat(mat1, mat2,submat);

}

else

{

goto lbl;

}

}

8 0
3 years ago
Other questions:
  • What type of devices are the keyboard and the mouse?
    5·2 answers
  • Select the correct statement below about database services or database instances:
    5·1 answer
  • Web-based e-mail like Hotmail is an example of three-tier client-server architecture that provides access to e-mail messages. Tr
    7·1 answer
  • A technician is troubleshooting a printer connected to a computer via USB. The printer will print using the controls at the prin
    9·1 answer
  • What are some differences between CUI and GUI ?
    7·1 answer
  • Write a function called show_info that takes a name, a home city, and a home state (a total of 3 arguments) and returns a full s
    12·1 answer
  • Most of the revenue that magazine companies generate comes from?
    8·2 answers
  • A closed-source product is typically free.TrueFalse
    6·1 answer
  • In which step is a metaphor used in planning a multimedia presentation
    15·1 answer
  • WILL GIVE A BRAINLIEST!!! PLS HELP!!!
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!