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
Oksanka [162]
4 years ago
10

For a set of integers stored in an array,calculate the sum of the positive numbers and the sum of the negative numbers. The prog

ram should store these numbers in memory variables: positiveSum and negativeSum. Numbers should be read from the array one at a time with a zero value (0) being used to signal the end of data (the zero value is acting as a "sentinel" value).
Computers and Technology
1 answer:
Radda [10]4 years ago
7 0

Answer: The c++ program to implement the given conditions is shown below.

#include <iostream>

using namespace std;

int main()

{

// used as index of array in the loop for calculating the sum

   int i=0;

// array contains both negative and positive integers

// 0 is used as the sentinel value

   int arr[12]={-9,-8,-3,-12,-78,-10,23,45,67,1,0};

   int positiveSum=0, negativeSum=0;

   do

   {

       if(arr[i]<0)

           negativeSum = negativeSum + arr[i];

       if(arr[i]>0)

           positiveSum = positiveSum  + arr[i];

// after every element is added, index of array represented by i is incremented

       i++;

   }while(arr[i]!=0);

// loop continues till end of array is reached

   cout<<"Sum of positive integers "<<positiveSum<<" and sum of negative integers "<<negativeSum<<endl;

   return 0;

}

OUTPUT

Sum of positive integers 136 and sum of negative integers -120

Explanation: This program declares and initializes an integer array without user input. As mentioned in the question, 0 is taken as the sentinel value which shows the end of data in the array.

   int arr[12]={-9,-8,-3,-12,-78,-10,23,45,67,1,0};

All the variables are declared with data type int, not float. Since, integers can yield integer result only.

The do-while loop is used to calculate the sum of both positive and negative integers using int variable i. The variable i is initialized to 0.

This loop will run till it encounters the sentinel value 0 as shown.

while(arr[i]!=0);

Hence, all the integers in the array are read one at a time and sum is calculated irrespective of the element is positive or negative.

       if(arr[i]<0)

           negativeSum = negativeSum + arr[i];

       if(arr[i]>0)

           positiveSum = positiveSum  + arr[i];

After the element is added, variable i is incremented and loop is continued.

The do-while loop tests positive and negative integers based on the fact whether their value is greater than or less than 0.

The program can be tested using different size and different values of positive and negative integers in the array.

You might be interested in
At the Transport layer of the OSI, what is used to find and communicate with a particular application running on a host?
ivanzaharov [21]

Answer:

Port number

Explanation:

Port numbers are used in the identification of a particular process for forwarding some form of network message such as the internet when it reaches a server. It is usually a 16-bit integer that is unsigned and found in header that is appended to a message unit.  At the OSI transport layer, port numbers are used in searching and communicating with a specific application that is running on a host server.

4 0
3 years ago
Which of the following Web sites would be MOST credible?
pentagon [3]
As of this problem together with the options presented with it, the most probable and the most likely answer for this would be A. a site associated with a local university.

An encyclopaedia site with many contributors can be prone to false information and made-up information. To add to it, some contributors might just be displaying the art of trolling over the internet and some might just be contributing just for the heck of it. A site run by a small, obscure publishing house can become more credible by becoming more renowned. A private site can either be good or bad, depending on the author that set it up. The most credible among the options would be a site associated with a local university.
6 0
3 years ago
Travis sends Suri what purports to be a link to an e-birthday card, but when she clicks on the link, software is downloaded to h
12345 [234]

Answer:

Identity Theft

Explanation:

Identity theft is the act of someone who obtains details about someone else illegally.  This is done to find personal and financial information such  name, address,social security number, passwords, and credit card number, phone number, e-mail, etc. Then the hacker can use this information to control bank accounts, e-mails, computers, portray himself as you are, or sell information to someone else.

3 0
3 years ago
2. You can ___ adjust to NOT sleeping at night.
zhannawk [14.2K]

Answer:

whats the answer chooses

Explanation:

7 0
3 years ago
You defined a book data type.
earnstyle [38]

Answer:

myBook.title('To Kill a Mockingbird')

Explanation:

Correct answer edge 2020

5 0
3 years ago
Other questions:
  • What function does a mobile device’s accelerometer serve?
    8·2 answers
  • On a Linux system, which command allows you to modify settings used by the built-in packet filtering firewall?
    15·1 answer
  • A measuring cylinder is used to measure the volume of an irregular<br>solid object.​
    5·1 answer
  • The gradual wearing away or breaking down of rocks by abrasion is a type of __________________ weathering.
    8·1 answer
  • HELP ME IF YOU KNOW HOW TO CODE ON PYTHON <br> ITS FOR A TEST DUE IN A FEW MINUTES
    12·1 answer
  • A___ is a placeholder where you can enter text to manipulate and give new graphical effects.​
    13·1 answer
  • How is video compression accomplished?
    14·1 answer
  • What would a system unit that is integrated with the display and keyboard would be considered?
    6·1 answer
  • Type the correct answer in the box. Spell all words correctly. Which language should you use to add functionality to web pages?
    5·1 answer
  • Which of the following tiny computer apps is designed to be useful but could cause more harm than good?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!