Question

A publisher may allow a reader to select a subset of chapters to purchase. Given 15 integers that are 0 or 1 indicating wheth in C++ please

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

// C++ program to display the selection of pages using shorthand ranges of 3 or more.
#include <iostream>

using namespace std;

int main()
{
int pages[15]; // create array of size 15
int i,j;
// loop to take input for 15 pages (0/1)
cout<<"Enter 0 for not including and 1 for including chapter (for 15 chapters): "<<endl;
for(i=0;i<15;i++)
{
cin>>pages[i];
}

bool pagesIncluded = false;

// loop 15 times
for(i=0;i<15;)
{
// if ith pages is included
if(pages[i] == 1)
{
pagesIncluded = true; // set pagesIncluded to true
// loop from i+1 to maximum 15 or when jth value is 0
for(j=i+1;j<15;j++)
{
if(pages[j] == 0) // if jth value is 0, exit from inner for loop
{
break;
}
}

// if number of 1 entries is 3 or more, then output it as a shorthand range and set i to j+1
if((j-1-i+1) >= 3)
{
cout<<(i+1)<<"-"<<j<<" ";
i = j+1;
}else
{
// if number of 1 entries is less than 2, then output (i+1) position and increment i by 1
cout<<(i+1)<<" ";
i++;
}
}else // if it entry is 0, increment i by 1
i++;
}

// if no pages are included, display None
if(!pagesIncluded)
cout<<"None";

cout<<endl; // display a new line at the end
return 0;
}

//end of program

Output:

Enter o for not including and 1 for including chapter (for 15 chapters): 1111 0 1 0 1 1 0 1 1 1 0 0 1-4 6 8 9 11-13

Enter o for not including and 1 for including chapter (for 15 chapters): 00111 001111 0111 3-5 8-11 13-15

Add a comment
Know the answer?
Add Answer to:
in C++ please A publisher may allow a reader to select a subset of chapters to...
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
  • 12. What is the output of this program? Your answer: 1 def main(): 2 print('The numbers...

    12. What is the output of this program? Your answer: 1 def main(): 2 print('The numbers are:') 3 for i in range (2,25): 4 isone (i) 5 def isone (number): isOne = True i = 2 while i < number and is one: if number % i == 0: 10 isOne = False 11 i += 1 Lou if isOne == True: 13 print("\t', i) 14 main() 12 13. What is the output of this program? Your answer: for i...

  • Homework Assigiment Chapters 1 and 2 Instructions: Read each question carefully, and consult the textbook chapters...

    Homework Assigiment Chapters 1 and 2 Instructions: Read each question carefully, and consult the textbook chapters noted above and the applicable slide handouts to choose the BEST and MOST SPECIFIC answer from the choices provided. Write that answer in the answer blank AFTER you have completed this worksheet, go to the matching quiz on Canwas and submit your answers before 11.59 PM on the due date, or you will receive ZERO points. Don't forget to click the Submit button at...

  • (Write a program, and show the output too) C programming I have a hard time to...

    (Write a program, and show the output too) C programming I have a hard time to get these five questions done. I am thankful if someone can help me with that. I started with something but i couldn't get it done. That's what I've done. #include <stdio.h> typedef struct _profile {    char name[32];    int age; } Profile; Profile prof[3] = {"myName", 19}, {"bob", 12} arr[1] = prof; for (int i = 0; i < 3; i++) {   ...

  • C++ Please Follow Instructions Write a program that displays an inches to centimeters conversion table. Your...

    C++ Please Follow Instructions Write a program that displays an inches to centimeters conversion table. Your input will be the smallest number of inches and the largest number of inches to be converted. The intervals will be in 6 inch increments. One inch is equivalent to 2.54 centimeters. Include the following in your program: 1. Include an initial algorithm that outlines your steps. 2. Include a refined algorithm that details how each step will be executed. 3. Prompt the User...

  • In C please! 4.20 LAB: Countdown until matching digits Write a program that takes in an...

    In C please! 4.20 LAB: Countdown until matching digits Write a program that takes in an integer in the range 20-98 as input. The output is a countdown starting from the integer, and stopping when both output digits are identical. Ex: If the input is: 93 the output is: 93 92 91 90 89 88 Ex: If the input is: 77 the output is: 77 Ex: If the input is: 15 or any number not between 20 and 98 (inclusive),...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • (Write or type in answers, except for problem #15, which should be entered as a small...

    (Write or type in answers, except for problem #15, which should be entered as a small program, tested, and submitted in Repl.it) 1. Write a Python statement to define a list named temps using the following elements, in order: 95, 100, 77, 54, 103, 82 2. a) What is the length of the list temps? b) What Python function can be used to obtain the length of the list? Use it in a statement to obtain the length of temps...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...

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