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
Serga [27]
3 years ago
12

A file named numbers.txt contains an unknown number of lines, each consisting of a single integer. Write some code that computes

the sum of all these integers, and stores this sum in a variable name sum.
Computers and Technology
1 answer:
Schach [20]3 years ago
3 0

Answer:

The code is given below with necessary comments for understanding

Explanation:

#Open the input file.

infile = open("numbers.txt", "r")

#Declare and initialize the required variable sum

#to store the sum of integers read from the file.

sum = 0

#Declare and intialize a variable to store the current

#number read from the file in the integer form.

curNum = 0

#Traverse the input file using for loop.

for curLine in infile:

   #Assign the number read from the file to the

   #variable curNum in the integr form.

   curNum = int(curLine)

   #Add the numbers read from the file to the

   #variable sum to get the sum of all the

   #numbers read from the file.

   sum += curNum

# Display the value of the variable runsum.

print(sum)

You might be interested in
Create a lottery game application. Generate three random numbers (see Appendix D for help in doing so), each between 0 and 9. Al
Pachacha [2.7K]

The question is not complete! Here is the complete question and its answer!

Create a lottery game application. Generate three random numbers (see Appendix D for help in doing so), each between 0 and 9. Allow the user to guess three numbers. Compare each of the user’s guesses to the three random numbers and display a message that includes the user’s guess, the randomly determined three-digit number, and the amount of money the user has won as follows:

no matches: 0

any one matching: 10$  

two matching: 1000$  

three matching: 100000$  

Code with Explanation:

#include <iostream>

using namespace std;

int main()

{

int matches=0;

int guess_1, guess_2, guess_3;

int num_1, num_2, num_3;

// get 3 digits from the user

cout<<"Enter first guess digit 0 to 9"<<endl;

cin>>guess_1;

cout<<"Enter second guess digit 0 to 9"<<endl;

cin>>guess_2;

cout<<"Enter third guess digit 0 to 9"<<endl;

cin>>guess_3;

// every time program runs srand() generates new random numbers and rand()%10 makes sure that number is single digit 0 to 9

srand(time(NULL));

num_1=rand()%10;

cout<<"First lottery digit: "<<num_1<<endl;

num_2=rand()%10;

cout<<"Second lottery digit: "<<num_2<<endl;

num_3=rand()%10;

cout<<"Third lottery digit: "<<num_3<<endl;

// store random generated numbers and guess numbers in arrays to compare them

int num[3]= {num_1,num_2,num_3};

int guess[3]={guess_1,guess_2,guess_3};

// compare the arrays to find out how many are matching

   for(int i=0; i<3; i++)

   {

 for(int j=0; j<3; j++)

 {

  if(num[i]==guess[j])

  {    

   matches = matches + 1;

  }

 }

}

cout << "Total Matches are: " <<matches << endl;

// display reward according to the number of matches

if (matches==0)

cout<<"you won: $0"<<endl;

if (matches==1)

cout<<"you won: $10"<<endl;

if (matches==2)

cout<<"you won: $1000"<<endl;

if (matches==3)

cout<<"you won: $100000"<<endl;

return 0;

}

Output:

Enter first guess digit 0 to 9

7

Enter second guess digit 0 to 9

1

Enter third guess digit 0 to 9

5

First lottery digit: 3

Second lottery digit: 7

Third lottery digit: 2

Total Matches are: 1

You won: $10

Enter first guess digit 0 to 9

7

Enter second guess digit 0 to 9

3

Enter third guess digit 0 to 9

4

First lottery digit: 5

Second lottery digit: 4

Third lottery digit: 7

Total Matches are: 2

You won: $1000

5 0
3 years ago
What was the name of the first computer (machine) language?
Angelina_Jolie [31]

In 1957, the first of the major languages appeared in the form of FORTRAN. Its name stands for FORmula TRANslating system. The language was designed at IBM for scientific computing. The components were very simple, and provided the programmer with low-level access to the computers innards.


8 0
3 years ago
Read 2 more answers
QUESTION 6 Which of the following is a class A IPv4 address? a. 118.20.210.254 b. 183.16.17.30 c. 215.16.17.30 d. 255.255.0.0
shepuryov [24]

Answer:

a. 118.20.210.254

Explanation:

Here are the few characteristics of Class A:

First bit of the first octet of class A is 0.

This class has 8 bits for network and 24 bits for hosts.

The default sub-net mask for class A IP address is 255.0.0.0

Lets see if the first bit of first octet of 118.20.210.254 address is 0.

118 in binary (8 bits) can be represented as : 1110110

To complete 8 bits add a 0 to the left.

01110110

First bit of the first octet of class A is 0 So this is class A address.

For option b the first octet is : 183 in the form of bits = 10110111 So it is not class A address

For option c the first octet is 215 in the form of bits = 11010111 So it is not class A address

For option d the first octet is 255 in the form of bits = 11111111. The first bit of first octet is not 0 so it is also not class A address.

3 0
3 years ago
Which of these are part of the CPU?<br> O peripheral<br> O HDMI cord<br> O core<br> O keyboard
Makovka662 [10]
Hiii I think the best option would be C. Core
7 0
3 years ago
Why was Windows 1.0 considered an operating environment rather than an operating system? because users were unable to use a mous
Hitman42 [59]

Answer:

Because it was only a shell; MS-DOS was still the operating system

<u><em>Hope this helps!</em></u>

<em>-Isa</em>

7 0
2 years ago
Other questions:
  • 1. What conversion factor should be used to convert from Gigaliters to liters?
    14·1 answer
  • What does f.i.r.s.t stand for in robotics
    15·1 answer
  • Considering current online newspaper editions and the characteristics of digital age media, how do you think newspaper design mi
    7·1 answer
  • Justine was interested in learning how to play the piano. Before she was allowed to even play the piano, she has had to learn
    15·2 answers
  • Which of the following is NOT part of the URL for an Internet web site?
    13·1 answer
  • Write a program that uses two input statements to get two words as input. Then, print the words on one line separated by a space
    11·1 answer
  • HTML question please help
    5·1 answer
  • 1. Write an if statement that assigns 20 to the variable y, and assigns 40 to the variable z
    10·1 answer
  • Can you please help me
    9·1 answer
  • High-level languages must be translated into machine language before they can be executed. _________________________
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!