If you have the values in a vector or an array, just use std::accumulate
from <numeric>
:
std::vector<double> vec; // ... fill vec with values (do not use 0; use 0.0) double average = std::accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
Related Posts:
- What is a segmentation fault?
- Floating point exception( core dump
- Passing Arrays to Function in C++
- Return array in a function
- Regular cast vs. static_cast vs. dynamic_cast
- What is an unsigned char?
- C++ std::priority_queue uses the lambda expression
- When to use reinterpret_cast?
- Expression must be a modifiable lvalue
- How to read a file line by line or a whole text file at once?
- Split a string using C++11
- error: expected unqualified-id error: Meaning and fix? [duplicate]
- How to track down a “double free or corruption” error
- undefined reference to `WinMain@16′
- What causes a SIGABRT fault?
- Graphics Library for C++
- ‘cout’ was not declared in this scope
- What is the difference between const int*, const int * const, and int const *?
- TranslateName() function doesn’t work on win10
- How to use the PI constant in C++
- How to concatenate two strings in C++?
- How to get current time and date in C++?
- where is the official c++ documentation
- wntdll.pdb not loaded – Can’t see the exception
- Networking with C++
- Error a function-definition is not allowed here before ‘{‘ token
- invalid new-expression of abstract class type error
- How can I create objects while adding them into a vector?
- warning: ISO C++ forbids variable length array
- cin >> “no operator matches these operands”
- C++ Structure Initialization
- expression preceding parentheses of apparent call must have (pointer-to-) function type
- Enum to String C++
- Random float number generation
- C++ terminate called without an active exception
- MinGW: “gcc is not recognized as an internal or external command”
- Convert Python program to C/C++ code?
- How to debug ‘Stack smashing detected’?
- basic_string::_M_construct null not valid after constructing subvector of strings
- Does C++ have a Garbage Collector?
- Is “argv[0] = name-of-executable” an accepted standard or just a common convention?
- How to fix ‘std::logic_error’ what(): basic_string::_M_construct null not valid error?
- warning: control reaches end of non-void function [-Wreturn-type]
- Program received signal SIGSEGV, Segmentation fault
- gcc/g++: “No such file or directory”
- Error: expected type-specifier before ‘ClassName’
- General way of solving Error: Stack around the variable ‘x’ was corrupted
- error MSB3073: How do I fix this?
- timestamp of time(0) at multiple places in a C++ program
- What’s the most efficient way to erase duplicates and sort a vector?
- Error “system” is ambiguous?
- What does “Permission denied” “Id returned 1 exit status” mean?
- When does an Incomplete Type error occur in C++
- std::cin input with spaces?
- extended initializer lists only available with
- how to initialize an empty integer array in c++
- What does |= mean in c++
- Error: cannot declare variable ‘c’ to be of abstract type ‘circle’
- Static linking vs dynamic linking
- What is object slicing?
- C: using strtol endptr is never NULL, cannot check if value is integer only?
- use of class template requires template argument list
- Why would this give a Use of uninitialised value of size 8
- C++ Error: Type Name is Not Allowed
- Is Python faster and lighter than C++?
- Case insensitive string comparison C++
- C++ Exception thrown: read access violation. this was nullptr
- C++ getInt() function (have a java equivalent attached)
- Simple string parsing with C++
- member access within null pointer of type ‘struct ListNode’
- How does c++ represent negative value
- Remove last character from C++ string
- plotting package for c++
- Passing as const and by reference – Worth it?
- Understanding Recursion to generate permutations
- In C++ what does template<> mean?
- Incrementing in C++ – When to use x++ or ++x?
- Use of undefined type [duplicate]
- Warning : overflow in implicit constant conversion
- C++ multiline string literal
- Getting a bunch of crosses initialization error
- c++ a heap has been corrupted error when running the program
- Expected unqualified-id before ‘[‘ token
- Using cin to input a single letter into a char
- C++ calling base class constructors
- Why am I getting an ‘Else without previous if’ error within a for loop?
- Dynamic and static Scoping program differences
- How to fix the error “Windows SDK version 8.1” was not found?
- Make / gcc cryptic error 2: how to have more information?
- REGSVR32: the module “xxxxx.dll” failed to load … dependent assembly could not be found
- Error: expression cannot be used as a function?
- What is a .h.gch file?
- MinGW .exe requires a few gcc dll’s regardless of the code?
- Converting bool to text in C++
- Is it still safe to delete nullptr in c++0x?
- Declaration is incompatible with type
- One or more multiply defined symbols found
- I’m getting the error “stoi is not a member of std” in myprogramminglab [duplicate]
- Template constructor in a class template – how to explicitly specify template argument for the 2nd parameter?
- C++ float array initialization