How can you quickly find the number of elements stored in a
a) static array b) dynamic array ?

Answers were Sorted based on User's Feedback



How can you quickly find the number of elements stored in a a) static array b) dynamic array ?..

Answer / santhosh kumar sahukari

1.for static array we can find the no.of elements of its by using sizeof() operator and data type of that array.
2.we can find the no.of elements by its subscript in static array.
eg: int arr[10]
we can use sizeof(arr) to find the no.of elements it v l print 20,then we can divide it by 2 as size of int is 2.
2.subscript tells us it has 10 elements.

for dynamic array we cant use sizeof() operator.
we cant say dat how many elements it will have..it will be decided at runtime only.

Is This Answer Correct ?    4 Yes 0 No

How can you quickly find the number of elements stored in a a) static array b) dynamic array ?..

Answer / jaroosh

a) sizeof(array)/sizeof(element)
b) you cant (well, you can, eg. by using realloc, counting
from 0 up, and checking the result etc. but this is absurd),
its best to keep track somehow how many elements are there
in the array, but the BEST solution is...do NOT use dynamic
arrays at all, use std containers instead like vector.

Is This Answer Correct ?    3 Yes 0 No

How can you quickly find the number of elements stored in a a) static array b) dynamic array ?..

Answer / murali

for static array start from highest index to lowest (
normal conditions apply*)
if dynamic array is a Container type, size() / length()
will do. if not process the nodes.

Is This Answer Correct ?    0 Yes 1 No

How can you quickly find the number of elements stored in a a) static array b) dynamic array ?..

Answer / brainless

b) use pointer of last element - pointer of first element + 1

However, we should know the last element's pointer...

Is This Answer Correct ?    0 Yes 2 No

How can you quickly find the number of elements stored in a a) static array b) dynamic array ?..

Answer / guest

static array

Is This Answer Correct ?    1 Yes 5 No

Post New Answer

More C++ General Interview Questions

What is microsoft c++ redistributable 2013?

0 Answers  


Explain how to initialize a const data member.

0 Answers  


How are virtual functions implemented in c++?

0 Answers  


What is the use of main function in c++?

0 Answers  


When the constructor of a base class calls a virtual function, why doesn't the override function of the derived class gets called?

0 Answers  






If a function doesn’t return a value, how do you declare the function?

0 Answers  


What is ctime c++?

0 Answers  


5. Can inline functions have a recursion?

4 Answers  


What are static variables?

0 Answers  


What is the correct syntax for inheritance a) class aclass : public superclass b) class aclass inherit superclass c) class aclass <-superclass

0 Answers  


Find the second maximum in an array?

12 Answers   HCL,


How many ways can a variable be initialized into in C++?

0 Answers   HCL,


Categories