Hello Readers,
So this post will be about the new things/ old concepts that I learned yesterday.
1) Pointers and references.
I haven't been able to understand this topic much.
Pointers are datatypes that store the location of the variable/ entity rather than the value stored inside it. The key takeaways were how to declare a pointer and its values, how to change its values, its uses in arrays, basic mathematical operations on pointers, etc.
But as I haven't coded much with pointers, I haven't understood its full potential, need to work over there.
References are datatypes like pointers that store memory location rather than the value itself but differ with respect to their operations. The key difference is that when we perform any operation in pointers, the location of the variable gets changed and not the internal value itself, whereas, in references, the internal value, along with any past declarations with links to that variable gets changed together.
2) OOPs
This is one thing whose importance is realized every time I code in C++. The idea of such code reusability to such an extent is revolutionary.
The key takeaways from my yesterday's learnings were - Getting to know basic OOPs structure, Basic terms such as encapsulation, abstraction, sub-classes, etc, how to form classes and their member functions, how to call its functions and create a new object & how to declare variables and the return type of the functions.
I wrote the following program just to have a bit of practice while learning.
The idea here was to create a menu-driven program to do functions such as calculate factorials, find out if it is a prime number, etc using functions defined in a class. The major realization was that when I normally code using just the user-defined functions, I always keep some return type other than that void. Here I realized that we should use any other return type when we have to store the value in some variable or use that same value as an input in another function. We could calculate/carry out our needs just by using void. Also, I found one new thing. If variables are declared in public in class, we do not have to declare them again in main().
One more thing, we end the class declaration with a semicolon. I think so because when we declare a class, no memory as such is allocated like in the case of a user-defined function. So the compiler takes it as a really big statement. The better explanation which I found on StackOverflow is linked below.
3) Range Claus Switch Statement
Our teacher gave a practice problem that we have to make a code for giving output as grades with respect to the marks we obtain as input. So these grades had a marks range like 75-100 will get A and so on. The conditions were that we had to use less than 10 switch statements and completely avoid if-else statements.
So in the dilemma of how to write a logic/algorithm which will take input as marks and return a single integer for a range, I wrote the following program.
The thought process behind the logic is - We need just an output change in our range of inputs. For eg., We just need the output change from x at 75 marks to y at 76 marks. We can have as many outputs in the range itself. So for doing this I implemented that we need to shorten our number line by the exact amount the divisions are made and then assign a number to the input based on the shortened number line. So the bigger numbers like eg 100 would have a greater "points" available to itself as compared to any other number like 56. So my first division was [1,35];[36,55];[56,75];[76,100]. I ran the first 3 parts of the code on for loop for verifying my technique and looking at what output we are getting next. The technique was to first reduce the number line by 35%, i.e. reducing every number by 35%. Then for assigning points, I divided the number by 1/10th of the original number line. The same logic was applied for the next 20% and the further 20% reduction of the number line. The code would have run more efficiently if I had used double instead of int and then converted the double to int data type along with dividing the new number line by 1/10th of the previous number line.
The only problem I further encountered was that I was getting 15 different outputs and I couldn't use more than 10 switch cases. Hence I used the same logic again, this time for range [0,15] by getting divisions [0,4];[5,7];[8,11];[12,15] for my grades. I further encountered a problem that the output wasn't changing at 8 but rather at 9. This error might be due to the real to int conversion. Hence I used a nested switch case and the problem was complete.
Thank you for reading this post.!