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
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 |
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 |
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 |
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 |
Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].
Is multimap sorted c++?
Can I have a reference as a data member of a class? If yes, then how do I initialise it?
What is a memory leak c++?
How many static variables are created if you put one static member into a template class definition?
What are the differences between public, private, and protected access?
12 Answers IBM, Oracle, Wipro,
Explain the scope resolution operator?
What is size of string in c++?
You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. New() and malloc()
write infinite loop in C++ which does not use any variable or constant?
What is RTRT tool?can it be used for automation?can it work on packet PC?
What are the differences between a struct and a class in C++?