C++ Array of Ints / Integers

Spread the love

#include <array>

std::array<int,4> myGrades = {86,80,64,91};


Sample c++ Array of ints Code

// std::array usage std::array<int,4> myGrades = {86,80,64,91};
#include <iostream>
#include <array>

using namespace std;
int main(){
    float sum = 0.0f;
    std::array<int,9> myGrades = {86,80,64,91,52,59,88,69,75};
    for ( unsigned int i = 0; i < myGrades.size();i++){
        std::cout << "My grades are: " << myGrades[i] << std::endl;
        sum += myGrades[i];
    }
    float myAverage = sum / 9;
    std::cout << "The average across 9 subjects is: " << myAverage << std::endl;

}


Line 8 is where the definition of the c++ array of int is declared and initialized. std::array<int, 9> myGrades = { list of ints }

Code Sample from Online C++ Compiler

Array of ints / integers

C++ Array of Ints

These are used to store similar type of elements since the data type must be the same for all elements. C++ array of ints can be used to store collection of primitive data types such as int.

To add to it, an array in C or C++ can store derived data types such as the structures, pointers etc. 

c++ array of ints. Image shows an colorful array

Top