How to count the number of integers in an array.

So i'm embarrassingly new to C++ and I was just wondering how I can make the simple program below tell me the number of integers in the array I created. When I try to use "sizeof" it tells me there are 32 integers when I am expecting it to tell me that there are 8 integers.

Then how can I prompt a user into dynamically inputting two new integers making the array a [5][2]. Any help would be tremendously appreciated.


#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int ArrNums [4][2]=\
{{365, 343}, {-421, 6534}, {789, 786}, {453, 9876}};

cout << "Number of integers in array: " << sizeof(ArrNums) << endl;



return 0;
}
sizeof reports the number of bytes occupied by an object: http://en.cppreference.com/w/cpp/language/sizeof

In your case, the size of arrNums is eight times the size of int, which is technically platform-dependent, but equals 4 on most common platforms. 8*4 = 32.

You could modify the program as follows to report the value you expect:

cout << "Number of integers in array: " << sizeof ArrNums / sizeof(int) << '\n';
or, to avoid changing it if the element type changes,

cout << "Number of integers in array: " << sizeof ArrNums / sizeof ArrNums[0][0] << '\n';

Also, prefer using std::vector or std::array instead of the C-style arrays, see https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rsl-arrays

(in particular, std::vector is the answer to your question "Then how can I prompt a user into dynamically inputting two new integers making the array a [5][2]")
Last edited on
If you want to use a raw pointers, which I do not recommend then this would be how you would initialize it:
1
2
3
4
5
int **a = new int*[rows]; //Making an array of pointers which will represent the rows
for(int i = 0; i < rows; i++){
     //allocates "cols" amount of ints for each pointer(aka row), and initializes to default value (0).
    a[i] = new int[cols]{};
}


However you must not forget to free/delete the allocated memory, which can be done in this fashion:
1
2
3
4
for(int i = 0; i < rows; i++){
    delete[] a[i]; //deletes the columns in the multidimensional array
}
delete[] a; //deletes the rows 


However this should be only for learning purposes, I do not suggest actually using raw pointers unless you absolutely need to, consider smart pointers or as Cubbi suggested use vectors, the case of vectors you would need to use a vector of vectors
Thank you so much. The only problem is when I use "vector" it doesn't seem to like how my element values are entered. Is there a special way they need to be entered when using "vector"??. The example I saw wants me to put the values in one element at time. For instance array2D[0][1]=898 and array2D[0][2]=9400 which is tedious to enter. Is there a way to enter the values like I did in my program above, in this format: "int ArrNums [4][2]=\
{{365, 343}, {-421, 6534}, {789, 786}, {453, 9876}};"

Right now my code looks like this:


// Practice.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

#define HEIGHT 4
#define WIDTH 2

int _tmain(int argc, _TCHAR* argv[])
{

vector<vector<double> > array2D;

// Set up sizes. (HEIGHT x WIDTH)
array2D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; ++i)
array2D[i].resize(WIDTH);

// Put some values in
array2D[1][1] =
{{898,9400},{-453,4532},{3453,453},{904,4394}};

cout << "Number of integers in array: " << sizeof array2D / sizeof(double) << '\n';



return 0;
}

Thank you for your help
Last edited on
array2D[1][1] is a single int, you can't assign a whole bunch of ints to it

Just initialize the vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>

int main()
{
    std::vector<std::vector<double>> array2D = {{898,9400},{-453,4532},{3453,453},{904,4394}};
    
    std::cout << "Number of integers in array: " << array2D.size() * array2D[0].size() << '\n';

    for(auto& row: array2D) {
        for(int n: row) std::cout << n << ' ';
        std::cout << '\n';
    }
}

demo using Visual Studio (which you seem to be using) http://rextester.com/QJIRZ67809
For some reason that wont run for me in visual studios. Am I doing something wrong?
Probably using a very old version. The oldest one I have is 2013 and it works there.
Topic archived. No new replies allowed.