5C++ Questions
1. What will the following code print?
num = 8;
cout << --num << " ";
cout << num++ << " ";
cout << num;
2.Since the while loop evaluates the condition before executing the statement(s), it is known as a(n)____ loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n)____ loop.
3.T/F While loops can only be used when a counter is decremented to zero. If the counter is incremented you must use the do-while loop.
4.How many times will the following program print Hello?
counter = 3;
while (counter > 0) {
loop = 5;
while (loop > 0) {
cout << "Hello" << endl;
loop--;
}
counter--;
}
5.If nothing within a while loop causes the condition to become false, a(n) ________ may occur.
6.The -- operator ... (check all that apply)
|
subtracts one from the value of its operand. |
||
|
must have an lvalue, such as a variable, as its operand. |
||
|
can be used in either prefix or postfix mode. |
||
|
is a unary operator. |
7.Which of the following statements will result in an infinite loop.
x = 10; while (x >= 0) {
cout << x;
++x;
} |
||
x = 10; while (x >= 0) {
cout << x; } |
||
x = 0; while (x <= 10) {
cout << x;
x = 10;
} |
||
x = 10; while (x >= 0); {
cout << x;
x--;
} |
8.T/F In C++, a file must be opened before the contents can be read.
9. T/F In C++, a file must exists before it can be written to.
10.Which of the following statements show the proper way to use the increment (++) operator.
|
x++ = 1023; |
||
|
x = 23++; |
||
|
y = ++x; |
||
|
counter--; |
In: Computer Science
In: Computer Science
<Haskell>
Define an action adder :: IO () that reads a given number of
integers from the keyboard, one per line, and displays their sum.
For example: > adder
How many numbers? 5
1 3 5 7 9
The total is 25
Hint: start by defining an auxiliary function that takes the current total and how many numbers remain to be read as arguments. You will also likely need to use the library functions read and show.
In: Computer Science
import kotlinx.coroutines.*
// TODO 1
fun sum(valueA: Int, valueB: Int): Int {
return 0
}
// TODO 2
fun multiple(valueA: Int, valueB: Int): Int {
return 0
}
fun main() = runBlocking {
println("Counting...")
val resultSum = async { sum(10, 10)
}
val resultMultiple = async { multiple(20, 20)
}
// TODO 3
println()
}
TODO 1:
Change it to suspend function with the following conditions:
Has a waiting time of 3 seconds before the next operation
runs.
Returns the value of the sum of valueA and valueB.
TODO 2:
Change it to suspend function with the following conditions:
Has a waiting time of 2 seconds before the next operation
runs.
Returns the value of the multiplication valueA and valueB.
TODO 3:
Add a function to print the deferred values of the resultSum and
resultMultiple variables on the console.
If run, the console will display the text:
Counting ...
Result sum: 20
Multiple results: 400
In: Computer Science
list 3 pros and 3 cons for Virtual reality technology and Augmented Reality technology, then discuss how you think these technologies will take part in out future ?
In: Computer Science
**I need to make this program to make sure that the numbers are unique. In another word, if the number has been generated and appear early, it should continue to find the next number until the number is unique.
Hint: use the array to check whether the number has been generated.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int n, low, high;
cout << "Enter how many random numbers you would like to see: ";
cin >> n;
cout << "Enter the lower limit now: ";
cin >> low;
cout << "And now enter the higher limit: ";
cin >> high;
cout << n << " random numbers:";
for (int i = 0; i < n; ++i) {
cout << " " << low + (rand() % (high-low+1)) << endl;
}
cout << endl;
return 0;
}
Please show me how it can be done and explain code needed if possible. Thanks!
In: Computer Science
fun main() {
val stringResult = getResult("Kotlin")
val intResult = getResult(100)
// TODO 2
println()
}
// TODO 1
fun <T> getResult(args: T): Int {
return 0
}
TODO 1:
Create a new generics function with the following conditions:
The function name is getResult.
Has one type of parameter.
If the attached argument is of type Int, then the value returned is
the value of the argument multiplied by 5.
If the attached argument is of type String, the value returned is a
character length.
If the attached argument is of type other than Int and String, then
the returned value is 0.
TODO 2:
Add functions to print the values of the stringResult and intResult
variables in the console.
In: Computer Science
C++ How do I initialize a clock to 0 and run it constantly throughout the program? THEN, how do I get the time at that moment multiple times? As in id like to set the
clock = 0,
do stuff
do stuff
get time now, store it
do stuff
get time now, store it
then also be able to do arithmetic on it, for instance, last time - current time = 2 seconds. I made it sound complicated but I believe this is very easy.
In: Computer Science
Python code for Multivariable Unconstrained *unidirectional search*(topic:- optimization techniques)
In: Computer Science
python 2.7 Dash
How to draw a bar & line & pie chart for below dict data by using Dash?
The value is the percentage of each month. So total will be 100.
{'Mar': 17.973851593144104, 'Feb': 10.540182187664472, 'Sep': 8.200076731097335, 'Apr': 12.080717324339435, 'Jan': 16.118724221364918, 'Nov': 12.29654875876966, 'Dec': 11.378407574427893, 'Oct': 11.411491609192186}
Please provide the python 2.7 code.
In: Computer Science
// TODO 1
class Cat(private val name: String) {
var sleep: Boolean = false
fun toSleep() {
println()
}
}
fun main() {
// TODO 2
val gippy = Cat("")
gippy.toSleep()
gippy.sleep = true
gippy.toSleep()
}
TODO 1:
Complete the code in the Cat class with the following conditions:
Create a getter setter function for the sleep property in which there is a function to print text:
The getter / setter function is called
Add code to the toSleep () function to print text:
[name], sleep!
if sleep is true and text:
name, let's play!
if sleep is false.
TODO 2:
Complete initialization with Cat class.
If run the console will display text like the following:
The getter function is called
Gippy, let's play!
The setter function is called
The getter function i
In: Computer Science
Determine the computational complexity of the following language:
0n1n
"Computational complexity theory focuses on classifying computational problems according to their inherent difficulty, (how difficult they are to solve) and relating these classes to each other. A computational problem is a task solved by a computer. Some computational problems are impractical as the known algorithm takes too much time and memory to complete."
In: Computer Science
(PLEASE EXPLAIN ANSWER AND SHOW STEPS IF POSSIBLE)
Q)An administrator is working with the 192.168.4.0 network, which has been subnetted with a /27 mask. Which two addresses can be assigned to hosts within the same subnet? (Choose two)
|
Q)The network address of 172.16.0.0/20 provides how many subnets and hosts? |
Q)Given the following IP address from the Class B address range using the default subnet mask: 100.110.0.0. Your network plan requires no more than 64 hosts on a subnet. When you configure the IP address in Cisco IOS software, which value should you use as the subnet mask?
In: Computer Science
This is a Java program that I am having trouble making.
1- Search for the max value in the linked list.
2- Search for the min value in the linked list.
3- Swap the node that has the min data value with the max one. (Note: Move the nodes to the new positions).
4- Add the min value and the max value and insert the new node
with the calculated value before the last node.
I already made a generic program that creates the linked list and
has the ability to add and remove from the list. I am not sure how
to create these generic methods though.
Below is my attempt to make the min finder, but it does not work and gives me an error on the bolded parts. It says "bad operand types for binary operator >".
LList.java below:
package llist;
/**
*
* @author RH
* @param <E>
*/
public class LList<E> {
Node head, tail;
int size;
public LList() {
size = 0;
head = tail = null;
}
public void addFirst(E element) {
Node newNode = new Node(element);
newNode.next = head;
head = newNode;
size++;
if (tail == null) {
tail = head;
}
}
public void addLast(E element) {
Node newNode = new Node(element);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = tail.next;
}
size++;
}
public void insert(int index, E element) {
if (index == 0) {
addFirst(element);
} else if (index >= size) {
addLast(element);
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node holder = current.next;
current.next = new Node(element);
current.next.next = holder;
size++;
}
}
public void remove(int e) {
if (e < 0 || e >= size) {
System.out.println("Out of bounds");
} else if (e == 0) {
System.out.println("Deleted " + head.element);
head = head.next;
size--;
} else if (e == size - 1) {
Node current = head;
Node holder;
for (int i = 1; i < size; i++) {
current = current.next;
}
System.out.println("Deleted " + current.element);
tail.next = current.next;
tail = tail.next;
size--;
} else {
Node<E> previous = head;
for (int i = 1; i < e; i++) {
previous = previous.next;
}
System.out.println("Deleted " + previous.next.element);
Node<E> current = previous.next;
previous.next = current.next;
size--;
}
}
public int largestElement() {
int max = 49;
while (head != null) {
if (max < head.next) {
max = head.next;
}
head = head.next;
}
return max;
}
public int smallestElement() {
int min = 1001;
while (head != null) {
if (min > head.next) {
min = head.next;
}
head = head.next;
}
return min;
}
public void print() {
Node current = head;
for (int i = 0; i < size; i++) {
System.out.println(current.element);
current = current.next;
}
}
}
Node.java below:
package llist;
/**
*
* @author RH
* @param <E>
*/
public class Node<E> {
E element;
Node next;
public Node(E data) {
this.element = data;
}
}
Driver.java below:
package llist;
import java.util.concurrent.ThreadLocalRandom;
/**
*
* @author RH
*/
public class Driver {
public static void main(String[] args) {
LList<Integer> listy = new LList();
// adds 10 random numbers to the linkedlist
for (int i = 0; i < 10; i++) {
listy.addFirst(ThreadLocalRandom.current().nextInt(50, 1000 +
1));
}
System.out.println("");
listy.print();
System.out.println("");
int max = listy.largestElement();
int min = listy.smallestElement();
System.out.println(max);
System.out.println(min);
//listy.remove(1);
//System.out.println("");
//listy.print();
}
}
I am not sure how to get these generic methods made.
In: Computer Science
C++
Drink Machine Simulator
Create a class that simulates and manages a soft drink machine.
Information on each drink
type should be stored in a structure that has data members to hold
the drink name, the
drink price, and the number of drinks of that type currently in the
machine.
The class should have an array of five of these structures,
initialized with the following data.
Drink Name Cost Number in
Machine
Cola 1.00 20
Root beer
1.00 20
Orange soda 1.00
20
Grape soda 1.50 20
Bottled water 1.50
20
The class should have two public member functions,
displayChoices (which displays a
menu of drink names and prices) and buyDrink (which handles a
sale). The class should
also have at least two private member functions, inputMoney, which
is called by buyDrink
to accept, validate, and return (to buyDrink) the amount of money
input, and
dailyReport, which is called by the destructor to report how many
of each drink type
remain in the machine at the end of the day and how much money was
collected.
You may want to use additional functions to make the program
more modular.
The client program that uses the class should have a main
processing loop which calls the
displayChoices class member function and allows the patron to
either pick a drink or
quit the program. If the patron selects a drink, the buyDrink class
member function is
called to handle the actual sale. This function should be passed
the patron’s drink choice.
Here is what the buyDrink function should do:
• Call the inputMoney function, passing it the patron’s drink
choice.
• If the patron no longer wishes to make the purchase, return all
input money.
• If the machine is out of the requested soda, display an
appropriate “sold out” message and return all input money.
• If the machine has the soda and enough money was entered,
complete the sale by updating the quantity on hand and money
collected information, calculating any change due to be returned to
the patron, and delivering the soda.
This last action can be simulated by printing an appropriate
“here is your beverage” message.
Input Validation: Only accept valid menu choices. Do not deliver a
beverage if the
money inserted is less than the price of the selected drink.
TEST CASE1: Drink Machine Menu 1. Cola : $1.00 2. Root Beer : $1.00 3. Orange Soda : $1.00 4. Grape Soda : $1.50 5. Bottled Water : $1.50 6. Quit Drink Machine Please make a selection : 1 How much money has been inserted: $5 Do you still want to make a purchase? (Y / N) : y Here is your Cola, and your change of $4.00 Drink Machine Menu 1. Cola : $1.00 2. Root Beer : $1.00 3. Orange Soda : $1.00 4. Grape Soda : $1.50 5. Bottled Water : $1.50 6. Quit Drink Machine Please make a selection : 2 How much money has been inserted: $6 Do you still want to make a purchase? (Y / N) : y Here is your Root Beer, and your change of $5.00 Drink Machine Menu 1. Cola : $1.00 2. Root Beer : $1.00 3. Orange Soda : $1.00 4. Grape Soda : $1.50 5. Bottled Water : $1.50 6. Quit Drink Machine Please make a selection : 3 How much money has been inserted: $4 Do you still want to make a purchase? (Y / N) : y Here is your Orange Soda, and your change of $3.00 Drink Machine Menu 1. Cola : $1.00 2. Root Beer : $1.00 3. Orange Soda : $1.00 4. Grape Soda : $1.50 5. Bottled Water : $1.50 6. Quit Drink Machine Please make a selection : 4 How much money has been inserted: $8 Do you still want to make a purchase? (Y / N) : y Here is your Grape Soda, and your change of $6.50 Drink Machine Menu 1. Cola : $1.00 2. Root Beer : $1.00 3. Orange Soda : $1.00 4. Grape Soda : $1.50 5. Bottled Water : $1.50 6. Quit Drink Machine Please make a selection : 5 How much money has been inserted: $3 Do you still want to make a purchase? (Y / N) : y Here is your Bottled Water, and your change of $1.50 Drink Machine Menu 1. Cola : $1.00 2. Root Beer : $1.00 3. Orange Soda : $1.00 4. Grape Soda : $1.50 5. Bottled Water : $1.50 6. Quit Drink Machine Please make a selection : 6 Thank you for shopping! Drink Machine Daily Report Cola : 19 Root Beer : 19 Orange Soda : 19 Grape Soda : 19 Bottled Water : 19 Total amount collected : $6.00
In: Computer Science