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
PSYCHO15rus [73]
2 years ago
12

Write a program that calculates the average of N integers. The program should prompt the

Computers and Technology
1 answer:
r-ruslan [8.4K]2 years ago
5 0

Answer:

def Average(num):

   if num == 0:

       return 0

   val = 0

   trueNum = num

   for i in range(0, num):

       try:

           val += int(input("Enter value (%d out of %d): " % (i+1,num)))

       except Exception as e:

           print ("Error processing value. Non integer detected.")

           try:

               val += int(input("Enter value (%d out of %d): " % (i+1,num)))

           except Exception as e:

               print ("Error processing value. Non integer detected.")

               print ("OMITTING value from average.")

               trueNum -= 1

   return val/trueNum

def main():

   try:

       num = int(input("Enter a value N for amount of items: "))

       if num < 0:

           raise(ValueError)

   except ValueError:

       print ("N must be positive integer.")

       exit(1)

   print("Average: ", Average(num))

   exit(0)

if __name__ == "__main__":

   main()

Explanation:

This program is written in Python to collect some integer value from the user as an upper bound of integers to be input for an average.  Using this upper bound, the program checks to validate it is indeed an integer.  If it not an integer, then the program alerts the user and terminates.  If it is a user, the Average function is called to begin calculation.  Inside the Average function, the user is prompted for an integer value repeatedly up until the upper bound.  Using the sum of these values, the program calculates the average.  If the user inputs a non integer value, the program will alert the user that the value must be an integer and ask again.  If the user again inputs a non integer value, that iteration will be omitted from the final average.  The program this prints the calculated average to the user.

Cheers.

You might be interested in
python Write a program that will take a file named Celsius.dat that contains a list of temperatures in Celsius (one per line), a
jekas [21]

with open('celcius.dat', 'r') as fIn, open('fahrenheit.dat', 'w') as fOut:

   for line in fIn:

       fahrenheit = 9.0 / 5.0 * float(line) + 32

       fOut.write("%.1f\n" % fahrenheit)


You can control the number of decimals in the formatting clause in the write statement.

6 0
3 years ago
Which of the following statements about interpreting colors are true? Select 3 options.
sukhopar [10]

Answer:

B, C and E

Explanation:

B :

White. In Western cultures, white symbolizes purity, elegance, peace, and cleanliness; brides traditionally wear white dresses at their weddings. But in China, Korea, and some other Asian countries, white represents death, mourning, and bad luck, and is traditionally worn at funerals.

C:

Color psychology has a effect for the world's different cultures. Colors evoke various emotions and beliefs, as well as positive and negative connotations. A color may represent happiness and warmth in one culture but is associated with betrayal and jealousy in another.

E:

Blue is one of the most commonly used colors in American marketing, often considered a safe color for a global audience, because it lacks significant negative connotations. Blue is tied to immortality, spirituality, and heaven in Eastern cultures.

Thank You! Please mark me brainliest!

Sorry if I'm wrong.

8 0
3 years ago
Which sentences in the passage correctly describe a function?
quester [9]

Option D is correct.

Explanation :

  1. In option A, it is not mandatory that a function will have arguments and it will return a value.
  2. In option B, no formula begins with =.
  3. In option C, it is not necessary to begin with /.
  4. In option D, MAX function should return maximum value in the set given.

Thus, option D is correct because arguments of a function are always enclosed within parentheses

Example:

SUM (30,70)

8 0
3 years ago
Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, a
GarryVolchara [31]

Answer:

<em>C++</em>

////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <vector>

using namespace std;

int main() {

  vector<int> v;

   int n = 1;

   while (n != 0) {

       cout<<"Enter an integer, the input ends if it is 0: ";

       cin>>n;

       v.push_back(n);

   }

   cout<<endl;

   ///////////////////////////////////////////////////////////

   int sum = 0;

   int num_positives = 0, num_negatives = 0;

   for (int i=0; i<v.size()-1; i++) {

       if (v[i] > 0)

           ++num_positives;

       else

           ++num_negatives;

           

       sum = sum + v[i];

   }

   //////////////////////////////////////////////////////////

   cout<<"The number of positives is "<<num_positives<<endl;

   cout<<"The number of negatives is "<<num_negatives<<endl;

   cout<<"The total is "<<sum<<endl;

   cout<<"The average is "<<(float)sum/(v.size()-1);

   ///////////////////////////////////////////////////////////

   return 0;

}

5 0
3 years ago
A complete traversal of an n node binary tree is a(n)____ "operation if visiting a node is O(1)for the iterative implementation
ch4aika [34]

Answer:

B.O(n).

Explanation:

Since the time complexity of visiting a node is O(1) in iterative implementation.So the time complexity of visiting every single node in binary tree is O(n).We can use level order traversal of a binary tree using a queue.Which can visit every node in O(n) time.Level order traversal do it in a single loop without doing any extra traversal.

8 0
3 years ago
Other questions:
  • Launched in 1995, ________ has become the most popular web browser.
    6·1 answer
  • What type of identity theft occurs when a thief spends another person's money or opens a line of credit in their name?
    8·2 answers
  • What is the benefit of a cloud computing infrastructure? ○ Companies have full control over the data that is hosted. ○ Companies
    11·1 answer
  • A virus or worm can have a payload that installs a(n) __________ door or trap-door component in a system, which allows the attac
    14·2 answers
  • What are features of a product?
    8·2 answers
  • The simplest method to copy information is to first select the information you want to copy, and then use the Copy button and th
    11·1 answer
  • What is the decimal value of 00001111
    14·1 answer
  • This is for career exploration, I need help please! &lt;3 HELPPPP
    8·2 answers
  • What do you call a collection of pre-programmed commands and functions used in programs?
    10·1 answer
  • Pls need this asap
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!