Given the availability of a file named numbers write the statements necessary to
ID: 3537845 • Letter: G
Question
Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.
#include<iostream>
using namespace std;
int main()
{
int a,num,sum=0;
cout << " enter no of numbers";
cin>> num;
cout << endl;
for(int i=0; i<num; i++)
{
cout <<" enter number";
cin>> a;
sum = sum+a;
cout << endl;
}
cout << " sum of numbers is " << sum << endl;
return 0;
}
Professor wants these things as well.Your program should start with comments containing your name, the name of your program, and what the program does. You must indent statements according to the standard used in your textbook. Do not use single letter variable names. There should be comments at variable declaration to explain the purpose of each variable. There should be comments at each major point of the program such as the input subsection, looping subsection, output subsection, calculation subsection, and so on.
Explanation / Answer
//created by Tex6483 on 20-06-2013
#include<iostream>
using namespace std;
int main()
{
int number,numbersToRead, total;//declaring variables
ifstream numbersFile;
numbersFile.open("numbers");//opening the file
cin >> numbersToRead;
total=0;
// creating a for statement to calaulate the total
for (int i=0; i<numbersToRead; i++)
{
numbersFile >> number;
total+=number;
}
cout << total;//display total
numbersFile.close();//closing file
return 0;
/*End of the Program*/
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.