AI, Data Science, & Programming for Small Business Applications & Large Enterprises › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 3: [Algorithms] – Linear Search, Binary Search, Bubble Sort, Selection Sort, Recursion, Merge Sort › To count the number of elements in an array
Tagged: arrays
- This topic is empty.
-
AuthorPosts
-
March 11, 2022 at 8:34 am #255
strlen is the function to count length of a string. Is there a similar function to count no. of elements in an array. It appears there is a difference between size of an array and no. of elements in an array.
The below array has 5 elements.
int lib [] = {5,48,55,55,87};
It is known that 0 will be stored as last element of any array. So, is it correct to state that lib has 6 elements?
Reply
You can use the
sizeof()
function to count the number ofarrayselements in an array. (googlesizeof()
& just try to think how it can be used to find number of elements)
It is known that 0 will be stored as last element of any array. So, is it correct to state that lib has 6 elements?
Not exactly. Since you’ve hard-coded 5 elements in the array & thus the number of elements in this array (not Size!) are 5, you can’tlater store any other element in it (by doing lib[6] = 0) since this array can only store a maximum of 5 ints.
Whenever you declare an array like this int lib [] = {5,48,55,55,87}; Memory for the array is automatically allocated by compiler, thus, it’s fixed. You can’t assign more values in this array later down in your code, though, you can replace elements.
There are several ways you can workaround this issue.
-
Define the size of lib explicitly (int lib[6]) & use a loop to assign values upto 5, this way, your array can store 6 integers but currently it’s only storing 5, later you can hard-code the last element.
Also.
Size of an array in C is calculated by:
size of data type * number of elements
Where size of data type varies from system to system. an int takes 4 bytes, char takes 1 byte, pointer on 64bit systems takes 8 bytes etc.
So if you want to calculate the number of elements in a non-string array think about how you might achieve it?
Hint: It will involve a bit of arithmetic and use of sizeof()!
Query
Yes, I can compute no. of elements.
So, the 0 as last character of array will be placed as last character of lib[4]?
Reply
Um, I don’t understand your question. What do you mean 0 will be placed as the last charcter?
sizeof(lib) / sizeof(lib[0])
means that divide the whole size of whole array by the size of first element of lib array. i.e,
sizeof(lib) = 4 * 5 = 20 / 4 = sizeof(lib[0]) = sizeof(int) = 4
You should have a look here this explains it really well
Query
It appears that \0 is needed to mark end for char type arrays and not integer type arrays. So not needed in this case.
Reply
You’re right. Arrays are not zero terminated, and they can’t be zero terminated since then they wouldn’t be able to store 0 as an element of the array! Strings are stored as an array data type, but with a zero terminator since no valid char is represented by zero. This allows us to be slightly more flexible with strings than general arrays. In particular we could assign a large amount of memory to facilitate storing a long string, but then only use some of that space to store a shorter string which is zero terminated. Then we would be able to still calculate the length of the string even though it has not filled the available space allocated to it.[learn_press_profile]
-
-
AuthorPosts
- You must be logged in to reply to this topic.