C
C#7mo 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
cjOP7mo ago
example i want to assign A+ to 4.0
Sossenbinder
Sossenbinder7mo ago
Sounds like you want a Dictionary<string, float>, right?
cj
cjOP7mo ago
what is best method to do that yeah how can i do that ?
Angius
Angius7mo 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
Sossenbinder7mo 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
cjOP7mo ago
so i don't need the second array? because i can already assign it with this way right?
Angius
Angius7mo ago
Yeah
Sossenbinder
Sossenbinder7mo 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
MODiX7mo 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
cjOP7mo ago
nice
Hannsen
Hannsen7mo 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
cjOP7mo 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
cjOP7mo ago
No description
Sossenbinder
Sossenbinder7mo 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
cjOP7mo ago
i want to display it it doesn't
Console.WriteLine(grades["B+"]);
Console.WriteLine(grades["B+"]);
Sossenbinder
Sossenbinder7mo 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
cjOP7mo ago
mayybe yeah
Sossenbinder
Sossenbinder7mo 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
cjOP7mo 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
Sossenbinder7mo ago
Nice 👍
cj
cjOP7mo 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
cjOP7mo ago
No description
cj
cjOP7mo ago
string v = Console.ReadLine(grade);
string v = Console.ReadLine(grade);
even if i did this
Angius
Angius7mo 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
cjOP7mo ago
grade = Console.ReadLine();


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


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

Did you find this page helpful?