0

C# 28.2 Getting a Raise (what am I missing here??)

This code clears 4 of the 5 test cases; the one it doesn't clear is a "hidden" one. I'm not sure what I'm doing wrong. Anyone have any ideas? ----------------- 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 //**** This is part of the exercise **** Increase(ref salaryBudget, ref percent); Console.WriteLine("After the increase: " + salaryBudget); } //complete the method //**** This is part of the exercise **** static void Increase(ref int a, ref int b) { double bTemp = Convert.ToDouble("1." + (Convert.ToString(b))); a = Convert.ToInt32(Convert.ToDouble(a) * bTemp); } } }

22nd Feb 2022, 5:18 PM
F M
F M - avatar
4 Answers
+ 2
Much too complicated. Where the actual inaccuracy appears, I don't know. But : * it suffices to pass salaryBudget only as ref since percent does not change * using 1.15 as double is bound to introduce rounding errors too early on. I opted to use int only: //complete the method static void Increase(ref int salaryBudget, int percent) { salaryBudget = (salaryBudget * (100 + percent )) / 100; }
22nd Feb 2022, 6:13 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
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 sB,double p) { sB=sB+(sB*p/100); } }
22nd Feb 2022, 11:56 PM
Jackie
Jackie - avatar
+ 1
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, percent); Console.WriteLine("After the increase: " + salaryBudget); } //complete the method static void Increase(ref int x, int y) { x = (x * (100 + y )) / 100; } } } This Code is working 100% percentage without error in Sololearn compiler.
6th Jun 2022, 12:01 PM
Mohammed Afzal Hussain
Mohammed Afzal Hussain - avatar
0
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 double result = Increase(salaryBudget, percent); Console.WriteLine("After the increase: " + result); } //complete the method static double Increase(double b, double p) { double result = Convert.ToDouble(b + (b * p / 100)); return result; }
11th Mar 2022, 7:29 AM
Khenrab Dorjee Lama
Khenrab Dorjee Lama - avatar