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;
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';
(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]")
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 = newint*[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] = newint[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';