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
ANTONII [103]
3 years ago
15

Write a program with a loop that asks the user to enter a series of positive numbers. The user should enter a negative number to

signal the end of the series. After all the positive numbers have been entered, the program should display their sum. Note: Don’t include the negative number in the sum. Pay attention on the fact that, as a programmer you have no idea how many positive numbers user is going to enter. Pick your loop wisely. Design your code your way!!
Computers and Technology
1 answer:
BARSIC [14]3 years ago
3 0

Those kind of loops are well implemented by a do-while loop: you should write your code following this logic (you didn't specify any particular language, so I'll just write down the logic in pseudo code):

sum=0;

do {

num = prompt(number);

if(number>0) {

   sum = sum+number;

}

while (number>0);

print(sum);

So, what we're doing is:

  1. Initialize the final sum as zero
  2. Ask the user for a number
  3. If the number is not negative, add it to the (partial) sum
  4. Loop steps 2-3 untile we get a negative value
  5. Print the final value of the sum
You might be interested in
Recall that within the ArrayBoundedQueue the front variable and the rear variable hold the indices of the elements array where t
Citrus2011 [14]

Answer:

int n = elements.length;

if(rear < n) {

rear = (rear + 1) % n;

elements[rear] = element;

rear = rear + 1;

}

Explanation:

Options are not made available; However, I'll answer the question base on the following assumptions.

Assumptions

Array name = elements

The front and the rear variable hold the current indices elements

Element to enqueue is "element" without the quotes

To enqueue means to add an element to an already existing queue or to a new queue.

But first, the queue needs to be checked if it can accept a new element or not; in other words, if it's full or not

To do this, we check: if rear < the length of the queue

If this statement is true then it means the array can accept a new element; the new element will be stored at elements[rear] and the rear will be icremented by 1 rear by 1

Else if the statement is false, then an overflow is said to have occurred and the element will not be added.

Going by the above explanation, we have

int n = elements.length;

if(rear < n) {

rear = (rear + 1) % n;

elements[rear] = element;

rear = rear + 1;

}

Additional explanation:

The first line calculates the length of the queue

The if condition on line 2 tests if the array can still accept an element or not

Let's assume this statement is true, then we move to liine 3

Line 3 determines the new position of rear.

Let's assume that n = 6 and current rear is 4.

Line 3 will produce the following result;

rear = (rear + 1) % n;

rear = (4 + 1)% 6

rear = 5%6

rear = 5.

So, the new element will be added at the 5th index

Then line 4 will be equivalent to:

elements[rear] = element;

elements[5] = element;

Meaning that the new element will be enqueued at the 5th index.

Lastly, rear is incremented by 1 to denote the new position where new element can be added.

6 0
4 years ago
If a driver who is under the age of 21 is stopped by a law enforcement officer and shown to have a BAL of .02 or greater, he or
n200080 [17]
Its actually BAC (clood Alchohol Content) and  it dependsn on what state you live in, and usually first time offenders with a BAC over 0.15 will have their license suspended for 90 days. but again it depends on the state. and anything below a 0.08% will just result in a warning but what state is it?
5 0
4 years ago
Ben wants to create a Book class that has an instance variable serialNumber. Where should he declare the instance variable
MrMuchimi

Answer:

within the Book class but needs to also be outside of any methods.

Explanation:

If Ben is creating an entire Book class then the instance variable needs to be within the Book class but needs to also be outside of any methods. If Ben places the variable inside a method it can only be used by that method and therefore becomes an instance variable of that method and not the class. By creating it inside the class and outside the methods it can be used every single time a Book object is created. Therefore, creating an instance variable of serialNumber every time.

4 0
3 years ago
HELP ASAP
vladimir2022 [97]

Answer:

B

Explanation:

took the test on egdenuity

7 0
3 years ago
In which situations would a text-to-speech tool be useful? Check all that apply. A reader needs to hear a word pronounced. O A s
ExtremeBDS [4]

Answer:

I'm not sure but I believe it would be A

Explanation:

This answer can't really be accurate as the reason can change for anyone.

8 0
3 years ago
Read 2 more answers
Other questions:
  • Why isn't my rank move from ambitious to virsto i met all the requirements
    8·2 answers
  • Describe two ways in which an organizer can assist in taking notes
    8·2 answers
  • An iPad Is An Example Of Which Type Of Computer?
    11·2 answers
  • A(n) ______________ ________________ attack keeps the target so busy responding to a stream of automated requests that legitimat
    14·1 answer
  • Which characteristic of a company suggests that it has an informal work culture?
    6·1 answer
  • A technician is buying a PC that will host three VMs running at the same time with the current configuration. The technician bel
    5·1 answer
  • PLEASE HELP!!! WILLGIVE BRAINLIEST!!!
    11·2 answers
  • State any three points of importance of local level profession​
    11·1 answer
  • Write a Python function that join the two string and print it ​
    14·2 answers
  • Rosa is building a website for a multimedia company. She wants to add a drop-down menu functionality to the website's main page.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!