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
Enzo formed a study group for an upcoming geography test. To start off each session, they go over past notes and look at maps. T
lesya692 [45]

<u>Answer:</u>

My suggestions are given below:

<em>Method 1:</em> Can create series of questions from book-inside and take an open-book test. Open-book test method is one of the best way to learn

<em>Method 2:</em> Written test is always necessary to really understand how much they remember the concept

<em>Method 3:</em> Participate in online quiz related to the learning outcome given in the question

<em>Method 4: </em>Answer questions from the reference books

<em>Method 5:</em> Go to library or review online books for further study.

8 0
3 years ago
An identifier that is prefixed with an @ and allows you to use code written in other languages that do not have the same set of
Nina [5.8K]

Answer:

Verbatim Identifier

Explanation:

  • Verbatim Identifier contains @ symbol as a prefix by which enables you to use reserved words of a programming language as identifier. For example the keywords like int, double, goto, char, else, while, for, float etc can be used as strings or variable names if @ symbol is added before these words e.g. @char, @while, @void, @int etc.
  • The compiler of a language will recognize such identifiers as verbatim identifiers and compiles them without giving an error that these are reserved words.
  • Verbatim identifier is used for program that is written in other languages and those languages don't have same reserved words.
  • For example: cout<<"use of verbatim identifier";<<@for;   In this statement, for keyword which is used in for loop can be used as an identifier with @ in the prefix.
  • The escape sequences if used with @ symbol in prefix then they are interpreted in a different way. For example in C#

                              string a = "\\C:\torrent\new\file";

                              Console.WriteLine(a);

This statement will give the following output:

                              \C:          orrent

                               ewfile

This means that the \t in the start of torrent and \n in the start of new word is taken as an escape sequence and output displayed is giving tab space because of \t and prints the rest of the words in new line because of \n escape sequence.

Now lets use this with the @ symbol

                               string a = @"\\C:\torrent\new\file";

                                Console.WriteLine(a);

The output will now be:

                                \\C:\torrent\new\file

\t and \n are not taken as escape sequences by the compiler because of @ symbol.

4 0
3 years ago
HELP 99PTS If Answered
Alborosie

You will have to do this as we are not you and we do not know local business/websites. Sorry we could not help.

3 0
3 years ago
Read 2 more answers
Name two components required for wireless networking<br>(answer fastly)​
Luden [163]

Explanation:

User Devices. Users of wireless LANs operate a multitude of devices, such as PCs, laptops, and PDAs. ...

Radio NICs. A major part of a wireless LAN includes a radio NIC that operates within the computer device and provides wireless connectivity.

or routers, repeaters, and access points

5 0
2 years ago
Which section of a personal narrative requires the most development
attashe74 [19]
D. Plot because a,b,and c is consider plot can i have brainlest answer please

5 0
3 years ago
Other questions:
  • What is process equipment?
    12·1 answer
  • Network administrators ____ to set up the browsers on the network to use a proxy server.
    12·1 answer
  • What are the meaning master files<br>​
    8·1 answer
  • What is the importance of eliminating marks that classify a master key
    7·1 answer
  • Katy and her associates are approached by an aged tai chi expert to create a website for him. From her participation in an onlin
    9·1 answer
  • I get such an error when I turn on the computer, how can I fix it?
    7·1 answer
  • The most common types of effects include entrances and exits
    11·1 answer
  • Convert pounds to ounces.
    11·2 answers
  • A system administrator issues the command ps on a server that is running the Linux operating system. What is the purpose of this
    9·1 answer
  • The other term for template document is _______.
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!