ITHelpself / CPlusPlus

0 stars 0 forks source link

8. Array #8

Open ITHelpself opened 4 years ago

ITHelpself commented 4 years ago

int arrayOfInts[5];
ITHelpself commented 4 years ago

int arrayOfInts[5] = {10, 20, 30, 40, 50};
ITHelpself commented 4 years ago

int arrayOfInts[] = {10, 20, 30, 40, 50};
ITHelpself commented 4 years ago

int arrayOfInts[5] = {10,20}; // means 10, 20, 0, 0, 0
ITHelpself commented 4 years ago

char arrayOfChars[5]; // declare the array and allocate the memory, don't initialize
char arrayOfChars[5] = { 'a', 'b', 'c', 'd', 'e' } ; //declare and initialize
double arrayOfDoubles[5] = {1.14159, 2.14159, 3.14159, 4.14159, 5.14159};
string arrayOfStrings[5] = { "C++", "is", "super", "duper", "great!"};
ITHelpself commented 4 years ago

// A fixed size raw array matrix (that is, a 2D raw array).
#include <iostream>
#include <iomanip>
using namespace std;
auto main() -> int
{
    int const n_rows = 3;
    int const n_cols = 7;
    int const m[n_rows][n_cols] = // A raw array matrix.
        {
            {1, 2, 3, 4, 5, 6, 7},
            {8, 9, 10, 11, 12, 13, 14},
            {15, 16, 17, 18, 19, 20, 21}};

    for (int y = 0; y < n_rows; ++y)
    {
        for (int x = 0; x < n_cols; ++x)
        {
            cout << setw(4) << m[y][x]; // Note: do NOT use m[y,x]!
        }
        cout << '\n';
    }
}
ITHelpself commented 4 years ago

// Example of raw dynamic size array. It's generally better to use std::vector.
#include <algorithm> // std::sort
#include <iostream>
using namespace std;
auto int_from(istream &in) -> int
{
    int x;
    in >> x;
    return x;
}
auto main() -> int
{
    cout << "Sorting n integers provided by you.\\n";
    cout << "n? ";
    int const n = int_from(cin);
    int *a = new int[n]; // ← Allocation of array of n items.

    for (int i = 1; i <= n; ++i)
    {
        cout << "The #" << i << " number, please: ";
        a[i - 1] = int_from(cin);
    }
    sort(a, a + n);
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << ' ';
    }
    cout << endl;

    delete[] a;
}
ITHelpself commented 4 years ago

int* a = new int[n]; // ← Allocation of array of n items.
delete[] a; // delete after allocation of "a" array
ITHelpself commented 4 years ago

//get size of array
char MyArray[] = { 'X','o','c','e' };
const auto n = std::extent<decltype(MyArray)>::value;
std::cout << n << "\n"; // Prints 4
ITHelpself commented 4 years ago

// Example of std::vector as an expanding dynamic size array.
#include <algorithm> // std::sort
#include <iostream>
#include <vector> // std::vector
using namespace std;
int int_from(std::istream &in)
{
    int x = 0;
    in >> x;
    return x;
}
int main()
{
    cout << "Sorting integers provided by you.\n";
    cout << "You can indicate EOF via F6 in Windows or Ctrl+D in Unix-land.\n";
    vector<int> a; // ← Zero size by default.
    while (cin)
    {
        cout << "One number, please, or indicate EOF: ";
        int const x = int_from(cin);
        if (!cin.fail())
        {
            a.push_back(x);
        } // Expands as necessary.
    }
    sort(a.begin(), a.end());
    int const n = a.size();
    for (int i = 0; i < n; ++i)
    {
        cout << a[i] << ' ';
    }
    cout << '\n';
}