Hash Fellow robcat2075 Posted November 27, 2023 Hash Fellow Share Posted November 27, 2023 It wouldn't be hard to memorize the sizes of standard data types and hard code those when programs needed to use them. But part of the power of C++ is that you will be able to create your own data types... and make arrays of them, which could be any length. sizeof() helps you to bullet proof your code so that even if you change the size of your data type or the length of an array, your code can still operate on them without over-running or under-running the total data. project. Write a program that declares an array of strings without specifying a specific number of elements and initializes the elements with a set of data in that same declaration line. Report the size of the array in bytes, the size of an element in bytes, and the number of elements Print out all the array elements For example if the data in the declaration were: aardvark bat cat dingo eel The program output would be... Report on Array! The array is 200 bytes in size. One element is 40 bytes long. The array has 5 elements. 0 aardvark 1 bat 2 cat 3 dingo 4 ferret End of report. If the initialization data in the declaration line were changed to a different number of values: mockingbird narwhal owl parrot quail serpent tapir ...the program should need no other modification to correctly report... Report on Array! The array is 280 bytes in size. One element is 40 bytes long. The array has 7 elements. 0 mockingbird 1 narwhal 2 owl 3 parrot 4 quail 5 serpent 6 tapir End of report. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.