0

Write a program that calculates the average for 5 marks entered by the users using any type of c++ loops

19th Nov 2016, 9:38 AM
Israa Elwan
Israa Elwan - avatar
2 ответов
+ 1
#include <iostream> using namespace std; int main() { double scores=0; for(double d =0; d<6; d++) { cout << "enter score " << d << endl; cin >> scores; scores+=scores; } cout << "avr: "<<scores /5.0<<endl; return 0; }
19th Nov 2016, 12:10 PM
Oluwatosin Akinbobuyi
Oluwatosin Akinbobuyi - avatar
+ 1
//A bit long but works fine: #include<iostream> #include<vector> class Mark { private: int m_mark; public: void setMark(int mark) { m_mark = mark; } int getMark() { return m_mark; } }; void setMarks(int number,std::vector<Mark> &marksVector) { for (int counter = 0; counter < number; counter++) { std::cout << "Enter mark #" << counter + 1 << ": "; int temp; std::cin >> temp; Mark pushThis; pushThis.setMark(temp); marksVector.push_back(pushThis); } } void printAverage(std::vector<Mark> marksVector) { double numerator = 0; double denominator=0; double result; for (int n = 0; n < marksVector.size(); n++) { ++denominator; numerator += marksVector.at(n).getMark(); } result = numerator / denominator; std::cout << "Average mark: " << result << std::endl; } int main() { std::vector<Mark> marksVector; setMarks(5, marksVector); printAverage(marksVector); }
19th Nov 2016, 12:21 PM
Ove Sletta