Question

In: Computer Science

1. Write a C++ program, for.cpp, that reads three integers. The first two specify a range,...

1. Write a C++ program, for.cpp, that reads three integers. The first two specify a range, call them bottom and top, and the other gives a step value. The program should print four sequences using for loops:

  • Each value in the range from bottom to top

  • Each value in the range from top to bottom, reverse counting

  • Every second value in the range from bottom to top

  • Every value from bottom to top increasing by step

    a: If bottom is less than top when entered, the program will exchange the two values.
    b: If step is negative, the program will be set to its absolute value (positive equivalent).

    Sample Run 1 (should have same text and spacing as in examples):

    Enter the bottom value in the range: 3
    Enter the top value in the range: 20
    Enter the step value: 6
    

    Values from 3 to 20:
    3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    Values from 20 to 3:
    20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3

    Every second value from 3 to 20:
    3 5 7 9 11 13 15 17 19
    
    Every 6th value from 3 to 20:
    3 9 15
    

Sample Run 2 (demonstrating part a and b):

Enter the bottom value in the range: 72 Enter the top value in the range: 56 Enter the step value: -4

Values from 56 to 72:
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

Values from 72 to 56:
72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56

Every second value from 56 to 72:
56 58 60 62 64 66 68 70 72
Every 4th value from 56 to 72:
56 60 64 68 72

Solutions

Expert Solution


#include <bits/stdc++.h>
using namespace std;

int main()
{
int bottom,top,step;
cout<<"Enter the bottom value in the range: ";
cin>>bottom;
cout<<"Enter the top value in the range: ";
cin>>top;
cout<<" Enter the step value: ";
cin>>step;
step=abs(step);
if(bottom>top)
{
int temp=top;
top=bottom;
bottom=temp;
}
cout<<"Values from "<<bottom<<" to "<<top<<endl;
for(int i=bottom;i<=top;i++)
{
cout<<i<<" ";
}
cout<<endl;
cout<<"Values from "<<top<<" to "<<bottom<<endl;
  
for(int i=top;i>=bottom;i--)
{
cout<<i<<" ";
}
cout<<endl;
cout<<"Every second value from "<<bottom<<" to "<<top<<endl;;
  
for(int i=bottom;i<=top;i=i+2)
{
cout<<i<<" ";
}
cout<<endl;
cout<<"Every "<<step<<"th value from "<<bottom<<" to "<<top<<endl;
  
for(int i=bottom;i<=top;i=i+step)
{
cout<<i<<" ";
}
cout<<endl;
  

return 0;
}


Related Solutions

Write a C++ program that inputs three integers in the range [1..13] that represent a hand...
Write a C++ program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand. In the output, cards will be described as follows: - 2–10 are described by the words for their numeric values: two, three, etc. - 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king. The evaluation of a hand is...
Write a C program that reads three integers and then prints them in the order read...
Write a C program that reads three integers and then prints them in the order read and reversed. Use four functions: main, one to read the data, one to print them in the order read, and one to print them reversed.
Write a program that inputs three integers in the range [1..13] that represent a hand of...
Write a program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand. In the output, cards will be described as follows: - 2–10 are described by the words for their numeric values: two, three, etc. - 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king. The evaluation of a hand is based...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
write these programs in c++ using getch() function Write a program that reads 20 integers from...
write these programs in c++ using getch() function Write a program that reads 20 integers from a user using a while loop and determines and prints whether each of those integers is an even number. Write a program that uses a while statement to read integers from a user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters -1.
a)     Write a program that reads a list of integers, and outputs all even integers in the...
a)     Write a program that reads a list of integers, and outputs all even integers in the list. For example, if the input list is [1, 2, 13, 14, 25], the output list should be [2, 14]. b)    Based on your code for part (a), write a program that reads a list of integers, and reports True if all numbers in the list are even, and False otherwise. Example: For input [1, 2, 13, 14, 25], the output should be False. Your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT