Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer all the following C++ related questions for positve rate. 17. Desc

ID: 3870630 • Letter: P

Question

Please answer all the following C++ related questions for positve rate.

17. Describe static local variables and explain how they differ in lifetime (duration) but not in scope from automatically-allocated local variables. Describe how the explicitly un-initialized static variable is treated differently from the explicitly un-initialized automatic variable.

18. (Continuing 17) What advantage do static variables have over automatic variables? Where do static and automatic variables “live” during their respective lifetimes?

19. (continuing 17) Define a static char variable named C1 initialized to 'C' and define an automatic char variable named C2 also initialized to 'C'.

20. C++11 introduced a range-based for-statement. What is its advantage over the standard C/C++ for-statement? Re-write the for-statement header using the range-based syntax with and without the auto reserved word. Do you like the auto reserved word? When should you use const auto?

21. T or F? static data members of a class are shared by every “living” object of the class including those “living” in activation records and in the heap.

24. Define a 2-dimensional unsigned long int <array> named twoDieFrequencies that can be used to tabulate the frequency of the occurrences of n 2-die throws, die1, die2 Î [ 1,6 ].

25. (Continuing 24) Write a nested range-based for-statemens with auto keywords to traverse the array twoDieFrequencies column-by-column, row-by-row to output it. Question Why and when should you use the & syntax?

26. (Continuing 24) Define a 3-dimensional arrayC named threeDieFrequencies that can be used to tabulate the frequency of n 3-die rolls, die1, die2, die3 Î [ 1,6 ].

27. How is a <vector> fundamentally different from an <array>? How and when is the fundamental difference an advantage? A disadvantage?

28. Define a <vector> named A that contains 7 long long int elements.

30a. What causes an out_of_range standard exception to occur? Is the index range checking provided by the <vector> class at() member function also provided by the overloaded [] operator?

Explanation / Answer

17. when static keyword applied to a local variable then it gives that variable static storage duration.

                This means that the static local variable's lifetime lasts to the end of the program rather than to the end of the invocation of the function.

                static local variables is only accessible within block or method in which they are defined.

                static local variables are initialized on first call to function where they are declared.

               

                Difference :

                static local variable's lifetime lasts to the end of the program rather than to the end of the invocation of the function.

                but in case of automatically allocated local variable's lifetime lasts till the invocation of the function in which they defined.

               

                when static variable will not initialized explicitly then it will automatically initialized to default value 0 whereas if automatic variable

                not initialized explicitly than it contains indeterminate value. It can be anything.

               

18. Static variables are shared, persistent and initialized to default value.

                static local variable's lifetime lasts to the end of the program but automatic variable's lifetime last only to their scope {} in which they created.

19. static char C1 ='C';

    char C2 ='C';

               

20. The range-based loops used to iterate over all elements in a container like arrays, collections which contains begin() and end() methods

                and also classes which can overload std::begin() and std::end() with same syntax.

                to explain range-based for loop header lets take example:

                std::vector<int> x = {0, 1, 2, 3, 4, 5};

                with auto keyword

                for (auto j : x) // access by value, the type of j is automatically detected

               

                without auto keyword

                for (const int& i : v) // access by const reference

               

                yes auto specifier used for automatic type deduction

                const auto is used when I want to make sure that the values are not allowed to modify but only for read purpose.

               

21. True, because Static data members of a class are not associated with the objects of the given class. Actually

                they are autonomous objects with static storage duration. These static data members have external linkage.