Help with C# (Methods) 5.2 Practice (Getting A Raise) | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Help with C# (Methods) 5.2 Practice (Getting A Raise)

I don't fully understand the lesson on passing data via reference and was wondering if I could get some sample code of the practice problem that actually works so I could possibly understand it better. The lesson scope is far too small for my tiny brain to comprehend how to use it in such a complex (for me) way.

5th Jun 2021, 10:02 AM
Soak_n_fused
Soak_n_fused - avatar
8 Réponses
0
Hi Lily thanks for commenting on my post. In short I'm not really sure about the out keyword as the lesson did not go over that before giving me the opportunity to try and solve the problem. The basics of the problem is taking a salary and percent as input of datatype int. The program will output the salary, I'm then supposed to call a method, passing the arguments via ref keyword and output the new salary after calculating the increase (I figure it's important to note that in the main method before the increase method is called it's says to output as follows): Console.WriteLine("Before the increase: " + salaryBudget); After calling the increase method the program wants to output is as follows: Console.WriteLine("After the increase: " + salaryBudget); In the increase method I'm supposed to again use the ref keyword to calculate what the new salary is. My goal is to solve this problem via editing the call of the increase method and creating the body of the increase method. Hope this was easy to follow, thanks in advance for any help you provide! I can provide the skeleton of code that the problem gives if it would help you to understand my issue better.
5th Jun 2021, 10:36 AM
Soak_n_fused
Soak_n_fused - avatar
0
i wrote some code on ref keyword..take a look https://code.sololearn.com/c8hqs3Pci1UT/?ref=app
5th Jun 2021, 10:37 AM
durian
durian - avatar
0
This is simple (enough) for me to understand, but I'm not sure how to apply only the ref keyword to accomplish the task at hand. Here's the skeleton of code the site provides as a baseline: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int salaryBudget = Convert.ToInt32(Console.ReadLine()); int percent = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Before the increase: " + salaryBudget); //complete the method call Increase(); Console.WriteLine("After the increase: " + salaryBudget); } //complete the method static void Increase() { } } } I should note I DON'T want you to build off this skeleton and give me the full working product, I just want an explanation on how exactly I should go about doing this while only using the ref keyword in both the calling of the method and in the method definition.
5th Jun 2021, 10:41 AM
Soak_n_fused
Soak_n_fused - avatar
0
pass salaryBudget as ref, and other argument s that u need inside Increase method, then just calculate the new salary and assign it to salaryBudget inside the method
5th Jun 2021, 10:48 AM
durian
durian - avatar
0
Ok so I finally stopped getting errors at compile time, and the program actually works as it should!! My issue was trying to use ref while inside the method body instead of in the arguments. However I had another question if you've got the time to answer it. Here's my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int salaryBudget = Convert.ToInt32(Console.ReadLine()); int percent = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Before the increase: " + salaryBudget); //complete the method call Increase(ref salaryBudget, ref percent); Console.WriteLine("After the increase: " + salaryBudget); } //complete the method static void Increase(ref int salaryBudget, ref int percent) { salaryBudget = (salaryBudget / 100) * percent + salaryBudget; } } } My question is how could I change the logic from: salaryBudget = (salaryBudget / 100) * percent + salaryBudget; To something else such as: salaryBudget = salaryBudget * [percent var as decimal form ie 15% increase = 1.15] Let me know if I'm overcomplicating this problem too much and what I should change in the original code to make things simpler etc. Thanks again for the help you've already provided!
5th Jun 2021, 11:07 AM
Soak_n_fused
Soak_n_fused - avatar
0
I found this post extremely relatable. Specifically, this, "The lesson scope is far too small for my tiny brain to comprehend how to use it in such a complex (for me) way." If there's one critique I have of Sololearn, it's that they so often make the practice exercises much more complex than the learning material that came before them. Like, here's a little kernel of info, now try it when we add a few additional variables you're completely unfamiliar with! It makes it really frustrating. That said, for this exercise, I found you don't even need a method for this, so I think it wasn't a very good example. I solved it this way (below - but I also figured out how to use a method, because that's the point of the exercise). it's going to take me a really long time to come to actually understand all of this stuff. static void Main(string[] args) { int salaryBudget = Convert.ToInt32(Console.ReadLine()); int percent = Convert.ToInt32(Console.ReadLine()); double x = .01; int y = 1; Console.WriteLine("Before the increase: " + salaryBudget); Console.WriteLine("After the increase: " + (percent*x+y)*salaryBudget); }
2nd Dec 2021, 2:14 PM
Jesse Clark
Jesse Clark - avatar
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double salaryBudget = Convert.ToDouble(Console.ReadLine()); double percent = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Before the increase: " + salaryBudget); //complete the method call Increase(ref salaryBudget, percent); Console.WriteLine("After the increase: " + salaryBudget); } //complete the method static void Increase(ref double y, double x) { y = y + (y * (x/100)); } } }
29th Sep 2022, 1:47 PM
Vait Veliasi
Vait Veliasi - avatar
- 1
do u mean ref/out keyword?
5th Jun 2021, 10:16 AM
durian
durian - avatar