C++ Questions! A few questions i need answered: 1. Can every for loop be impleme
ID: 3830521 • Letter: C
Question
C++ Questions!
A few questions i need answered:
1. Can every for loop be implemented as a while loop? why or why not?
2. Can every if statement be implemented as a switch statement?
5. What does "reducing the problem" mean in regards to solving a computing problem? Give an example.
7. typically, when you have an array variable you also have a corresponding counter that indicates how many values are in the array. Suppose you have to maintain a collection of golf scores when the collection could, at any moment, have between 0 and 18 values in it. Describe a scheme to be able to determine the number of entries in the array without having to maintain a counter?
8. Explain how a variable of type int** is like a two dimensional array of int values.
Explanation / Answer
1. Can every for loop be implemented as a while loop? why or why not?
Yes. All loops are interchangable only thing is in some cases performance can be a bit different.
Example for loop, initialization can be done just outside while loop, increment can be done before checking condition as an and step and condition can be checked in while loop. (Please note initialization can be done at end and hacky code can be written to ensure on break; and continue kind of statement can be made to work)
2. Can every if statement be implemented as a switch statement?
No, switch currently deosn't have support for complex conditions and it can only be for discrete values.
5. What does "reducing the problem" mean in regards to solving a computing problem? Give an example.
Reducing a problem means breaking it into simpler problem so that solution of thsi problems help in building solution of actual problem at hand.
7. typically, when you have an array variable you also have a corresponding counter that indicates how many values are in the array. Suppose you have to maintain a collection of golf scores when the collection could, at any moment, have between 0 and 18 values in it. Describe a scheme to be able to determine the number of entries in the array without having to maintain a counter?
We can put a marker and then traverse array till that marker (e.g., -1 value). This marker can indicate end of array and we can get length using it.
8. Explain how a variable of type int** is like a two dimensional array of int values.
int** can point to an address of an address, so we can make int* to point to an array of int* and then each it* can point to a location of a start element of a 1 d array, essentially giving us a 2D array.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.