C
C#10mo ago
cj

i want to assign the grades with the grade points how can i do that?

string[] grades = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","F"};
float[] gradePoints = { 4.0f,4.0f,3.7f,3.3f,3,2.7f,2.3f,2,1.7f,1.3f,1,0 };
string[] grades = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","F"};
float[] gradePoints = { 4.0f,4.0f,3.7f,3.3f,3,2.7f,2.3f,2,1.7f,1.3f,1,0 };
30 Replies
cj
cjOP10mo ago
example i want to assign A+ to 4.0
Sossenbinder
Sossenbinder10mo ago
Sounds like you want a Dictionary<string, float>, right?
cj
cjOP10mo ago
what is best method to do that yeah how can i do that ?
Angius
Angius10mo ago
Like any dictionary
var grades = new Dictionary<string, float>() {
["A+"] = 4.0,
["A"] = 4.0,
["A-"] = 3.7,
// ...
}
var grades = new Dictionary<string, float>() {
["A+"] = 4.0,
["A"] = 4.0,
["A-"] = 3.7,
// ...
}
Sossenbinder
Sossenbinder10mo ago
var gradeMapping = new Dictionary<string, float>(){
["A+"] = 4.0f,
["A"] = 4.0f,
}
var gradeMapping = new Dictionary<string, float>(){
["A+"] = 4.0f,
["A"] = 4.0f,
}
Ah, beat me to it
cj
cjOP10mo ago
so i don't need the second array? because i can already assign it with this way right?
Angius
Angius10mo ago
Yeah
Sossenbinder
Sossenbinder10mo ago
Depends on whether you need access to an array of the potential grades outside of the dictionary lookup, but you could also get the Values of the dictionary in case you need that
MODiX
MODiX10mo ago
Angius
REPL Result: Success
var grades = new Dictionary<string, float>() {
["A+"] = 4.0f,
["A"] = 4.0f,
["A-"] = 3.7f,
// ...
};

grades["A"]
var grades = new Dictionary<string, float>() {
["A+"] = 4.0f,
["A"] = 4.0f,
["A-"] = 3.7f,
// ...
};

grades["A"]
Result: float
4
4
Compile: 301.948ms | Execution: 31.578ms | React with ❌ to remove this embed.
cj
cjOP10mo ago
nice
Hannsen
Hannsen10mo ago
Have a look at https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.zip?view=net-8.0 maybe it is also suitable for you if you want to keep two value sources initially
Enumerable.Zip Method (System.Linq)
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
cj
cjOP10mo ago
string[] grades = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","F"};

var grades = new Dictionary<string, float>()
{

["A+"]=4.0f,
["A"]=4.0f,
["A-"]=3.7f,
["B+"]=3.3f,
["B"]=3,
["B-"]=2.7f,
["C+"]=2.3f,
["C"]=2,
["C-"]=1.7f,
["D+"]=1.3f,
["D"]=1,
["F"]=0,
};
grades["B+"];



Console.ReadLine();
string[] grades = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","F"};

var grades = new Dictionary<string, float>()
{

["A+"]=4.0f,
["A"]=4.0f,
["A-"]=3.7f,
["B+"]=3.3f,
["B"]=3,
["B-"]=2.7f,
["C+"]=2.3f,
["C"]=2,
["C-"]=1.7f,
["D+"]=1.3f,
["D"]=1,
["F"]=0,
};
grades["B+"];



Console.ReadLine();
what is wrong here?
cj
cjOP10mo ago
No description
Sossenbinder
Sossenbinder10mo ago
- You're reusing the variable name "grades", that's not valid C# - The grades["B+"]; is a noop, you are not using it for anything
cj
cjOP10mo ago
i want to display it it doesn't
Console.WriteLine(grades["B+"]);
Console.WriteLine(grades["B+"]);
Sossenbinder
Sossenbinder10mo ago
I think what you want to do is, removing or renaming either the array or the dictionary, removing the grades["B+"] line, read the input and then look up the result and output it again Ah, or that
cj
cjOP10mo ago
mayybe yeah
Sossenbinder
Sossenbinder10mo ago
I mean I could just drop you the code but we can also step through Did you remove the string[] grades for now? Or do you still need it for your sample?
cj
cjOP10mo ago
nvm i amm so stupid xd
string[] grades = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","F"};

var grade = new Dictionary<string, float>()
{

["A+"]=4.0f,
["A"]=4.0f,
["A-"]=3.7f,
["B+"]=3.3f,
["B"]=3,
["B-"]=2.7f,
["C+"]=2.3f,
["C"]=2,
["C-"]=1.7f,
["D+"]=1.3f,
["D"]=1,
["F"]=0,
};




Console.WriteLine(grade["B+"]);
Console.ReadLine();
string[] grades = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","F"};

var grade = new Dictionary<string, float>()
{

["A+"]=4.0f,
["A"]=4.0f,
["A-"]=3.7f,
["B+"]=3.3f,
["B"]=3,
["B-"]=2.7f,
["C+"]=2.3f,
["C"]=2,
["C-"]=1.7f,
["D+"]=1.3f,
["D"]=1,
["F"]=0,
};




Console.WriteLine(grade["B+"]);
Console.ReadLine();
it worked
Sossenbinder
Sossenbinder10mo ago
Nice 👍
cj
cjOP10mo ago
i can remove the string array now i don't need it so i want to read the grade
var grade = new Dictionary<string, float>()
{

["A+"]=4.0f,
["A"]=4.0f,
["A-"]=3.7f,
["B+"]=3.3f,
["B"]=3,
["B-"]=2.7f,
["C+"]=2.3f,
["C"]=2,
["C-"]=1.7f,
["D+"]=1.3f,
["D"]=1,
["F"]=0,
};




Console.ReadLine(grade);
Console.WriteLine(grade);
var grade = new Dictionary<string, float>()
{

["A+"]=4.0f,
["A"]=4.0f,
["A-"]=3.7f,
["B+"]=3.3f,
["B"]=3,
["B-"]=2.7f,
["C+"]=2.3f,
["C"]=2,
["C-"]=1.7f,
["D+"]=1.3f,
["D"]=1,
["F"]=0,
};




Console.ReadLine(grade);
Console.WriteLine(grade);
cj
cjOP10mo ago
No description
cj
cjOP10mo ago
string v = Console.ReadLine(grade);
string v = Console.ReadLine(grade);
even if i did this
Angius
Angius10mo ago
Console.ReadLine() does not take any arguments You want to use it to take user input Then use that input to look up the grade Then write that grade out
cj
cjOP10mo ago
grade = Console.ReadLine();


Console.WriteLine(grade);
grade = Console.ReadLine();


Console.WriteLine(grade);
?
Angius
Angius10mo ago
Not whatsoever, no
cj
cjOP10mo ago
i got it ngl i asked chatgpt
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
cj
cjOP10mo ago
i don't use it to generate code i use it to explain errors and bugs
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?