Print out size of array

I need to write a statement that will print out the size of the array. I have it to where it will print out just 0 but I need the size of the array. I can't change anything above where it says "Write your statement here"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Declarations for Case 1:
				int numberOfElements{ 0 };
				double myArray[20]{};

				//Write the statement below that will CALCULATE the number of elements that are in myArray 
				//and assign the result to the variable numberOfElements.  You must use a formula (not a loop)
				//to receive credit for this problem.
				//Note that  myArray is already declared for you above.

				//Write your statement here:
				/**************************************************************************************************************/
				
				int num_elements = sizeof(myArray) / sizeof(myArray[0]);
				cout << numberOfElements << endl;
numberOfElements and num_elements are two different variables.
If you want to use numberOfElements to store the result, then use that.
Last edited on
Ok I changed num_elements to numberOfElements but it still prints out 0
1
2
3
4
5
6
7
#include <iostream>

int main() {
    double myArray[20]{};
    int num_elements = sizeof myArray / sizeof *myArray;
    std::cout << num_elements << '\n';   // prints 20
}

1
2
3
4
5
6
7
8
9
10
 
#include <iostream>
using namespace std;

int main (){
    int MyArray[] = {1, 10, 23, 50, 4, 9, -4};
    int n = sizeof(MyArray) / sizeof(MyArray[0]); 
    cout<<n;
    return 0;
 }
Last edited on
Topic archived. No new replies allowed.