C
C#The king of kings

❔ Using methods in Grade Statistics project

Write a method PrintoutGrades that prints the grades. Write a method Statistics that will calculate the number of "A", "C" and "F" grades and the total number of grade points. When the Statistics method is called, the grade statistics must be printed.
A
Angius511d ago
Sounds like a nice and simple exercise, good luck 👌
TKO
The king of kings511d ago
Hello there Well! For me, it's not that simple to implement it, therefore I need your knowledge to put it into code 😉
A
Angius511d ago
What have you tried?
TKO
The king of kings511d ago
Let me show u what I have built so far
using System;
namespace GradeStatisticsApp
{
public class Points_Grades
{
public static int Points { get; set; }
public static char Grades { get; set; }
public Points_Grades(int ponits, char grades)
{
Points = ponits;
Grades = grades;
}

public static void CheckAlphabetical()
{

}

public static void ReadScore()
{


}

public static void ConvertPointsIntoGrades()
{

}
}
}
using System;
namespace GradeStatisticsApp
{
public class Points_Grades
{
public static int Points { get; set; }
public static char Grades { get; set; }
public Points_Grades(int ponits, char grades)
{
Points = ponits;
Grades = grades;
}

public static void CheckAlphabetical()
{

}

public static void ReadScore()
{


}

public static void ConvertPointsIntoGrades()
{

}
}
}
using System;
using System.Text.RegularExpressions;
namespace GradeStatisticsApp
{
class Program
{
static void Main(string[] args)
{
string[] subject = { "Mathematic", "Swedish", "English", "History", "Physic" };
int[] points = new int[5];
char[] grade = new char[6];
Points_Grades.CheckAlphabetical();
Console.Write("\nEnter the student name:\t");
string userInput1 = Console.ReadLine();
Console.WriteLine("");
/*foreach (char name in userInput1)
{
Console.WriteLine();
}
Points_Grades.ReadScore();*/

for (int i = 0; i < 5; i++)
{
Console.Write(subject[i]);

Console.Write($"\nEnter the points: ");
bool condition = int.TryParse(Console.ReadLine(), out int point);
Console.Write("");
if (condition == false)
{
throw new Exception("Invalid user input");
}
if (point <= 100 && point >= 50)
{
Console.Write(Convert.ToChar('A'));
var grade1 = Console.ReadLine();
}
if (point >= 0 && point < 50)
{ Console.Write(Convert.ToChar('F'));
var grad2 = Console.ReadLine();
}
else if (point > 100)
{
throw new Exception("User input out of range");
}
Console.WriteLine("");
Console.Write(subject[0]);
Console.Write(subject[1]);
Console.Write(subject[2]);
Console.Write(subject[3]);
Console.Write(subject[4]);
}
}
}
}
using System;
using System.Text.RegularExpressions;
namespace GradeStatisticsApp
{
class Program
{
static void Main(string[] args)
{
string[] subject = { "Mathematic", "Swedish", "English", "History", "Physic" };
int[] points = new int[5];
char[] grade = new char[6];
Points_Grades.CheckAlphabetical();
Console.Write("\nEnter the student name:\t");
string userInput1 = Console.ReadLine();
Console.WriteLine("");
/*foreach (char name in userInput1)
{
Console.WriteLine();
}
Points_Grades.ReadScore();*/

for (int i = 0; i < 5; i++)
{
Console.Write(subject[i]);

Console.Write($"\nEnter the points: ");
bool condition = int.TryParse(Console.ReadLine(), out int point);
Console.Write("");
if (condition == false)
{
throw new Exception("Invalid user input");
}
if (point <= 100 && point >= 50)
{
Console.Write(Convert.ToChar('A'));
var grade1 = Console.ReadLine();
}
if (point >= 0 && point < 50)
{ Console.Write(Convert.ToChar('F'));
var grad2 = Console.ReadLine();
}
else if (point > 100)
{
throw new Exception("User input out of range");
}
Console.WriteLine("");
Console.Write(subject[0]);
Console.Write(subject[1]);
Console.Write(subject[2]);
Console.Write(subject[3]);
Console.Write(subject[4]);
}
}
}
}
A
Angius511d ago
Well, grades are stored in an array Seems like the Statistics method needs to count how many As are in there, how many Bs, and so on Usually, it's easiest to achieve with a dictionary Dictionary<char, int> for example
TKO
The king of kings511d ago
BlazeBin - ryrklgewvdtc
A tool for sharing your source code with the world!
A
Angius511d ago
You can fill the dictionary, then loop over the array of grades, and increment the dictionary as needed
var dict = new Dictionary<char, int>(){
['A'] = 0,
['B'] = 0,
// ...
};

foreach (var grade in grades)
{
dict[grade] += 1;
}
var dict = new Dictionary<char, int>(){
['A'] = 0,
['B'] = 0,
// ...
};

foreach (var grade in grades)
{
dict[grade] += 1;
}
Something like this
TKO
The king of kings511d ago
Well! This is an assignment from my course obviously and they want me to use an array to build the project. This is the complete assignment requirements:
Write a method ReadPoints to read the student's grade points in the various subjects.
You should save the points in an array, vector or list.
When the method is called, the user must enter a number of grade points.
The grade points to be entered are controlled by the contents of the subject field.

Write a method that will convert the points into grades according to the scale A-F.
The grade A corresponds to 100 points.
The grade F corresponds to less than 50 points
You must store the grades in a field (array, vector or list).


Write a method printPrintGrades that prints the grades.
Write a method Statistics that will calculate the number of "A", "C" and "F" grades and the total number of grade points.
When the Statistics method is called, the grade statistics must be printed.
Write pseudocode for this method. Also draw flow charts.

You don't need to write pseudo code or draw flow diagrams for the entire program.
Compile and run the program.
Write a method ReadPoints to read the student's grade points in the various subjects.
You should save the points in an array, vector or list.
When the method is called, the user must enter a number of grade points.
The grade points to be entered are controlled by the contents of the subject field.

Write a method that will convert the points into grades according to the scale A-F.
The grade A corresponds to 100 points.
The grade F corresponds to less than 50 points
You must store the grades in a field (array, vector or list).


Write a method printPrintGrades that prints the grades.
Write a method Statistics that will calculate the number of "A", "C" and "F" grades and the total number of grade points.
When the Statistics method is called, the grade statistics must be printed.
Write pseudocode for this method. Also draw flow charts.

You don't need to write pseudo code or draw flow diagrams for the entire program.
Compile and run the program.
A
Angius511d ago
I don't see any mention of dictionaries being disallowed Because, without a dictionary, you're looking at much more much worse code
TKO
The king of kings511d ago
Really! I agree with you. This project seems a bit hard to implement.
A
Angius511d ago
var sums = new int[]{0, 0, 0, 0, 0, 0};

foreach (var grade in grades)
{
switch (grade) {
case 'A':
sums[0] += 1;
break;
case 'B':
sums[1] += 1;
break;
case 'C' //...
}
}
var sums = new int[]{0, 0, 0, 0, 0, 0};

foreach (var grade in grades)
{
switch (grade) {
case 'A':
sums[0] += 1;
break;
case 'B':
sums[1] += 1;
break;
case 'C' //...
}
}
TKO
The king of kings511d ago
Ok! So you recommend me to use dictionary.
A
Angius511d ago
Or maybe you could get the index by using the fact, that chars are integers under the hood
var sums = new int[]{0, 0, 0, 0, 0, 0};
// A B C D E F

foreach (var grade in grades)
{
sums[grade - 'A'] += 1;
}
var sums = new int[]{0, 0, 0, 0, 0, 0};
// A B C D E F

foreach (var grade in grades)
{
sums[grade - 'A'] += 1;
}
A dictionary would be the most straightforward
TKO
The king of kings511d ago
Ok
A
Angius511d ago
Should a dictionary be forbidden, the way with calculating index from char would be my choice
TKO
The king of kings511d ago
OK The problem is that this project seems more advanced to me and some of the concepts you're using looks a bit confusing to me.
A
Angius511d ago
Which ones?
TKO
The king of kings511d ago
What is this code doing?
A
Angius511d ago
A switch is a more concise way of writing a bunch of if/else statements
TKO
The king of kings511d ago
I haven't used even B grade in my project
A
Angius511d ago
You could write it as
var sums = new int[]{0, 0, 0, 0, 0, 0};

foreach (var grade in grades)
{
if (grade == 'A')
{
sums[0] += 1;
}
else if (grade == 'B')
{
sums[1] += 1;
}
else if (grade == 'C')
{
// ...
}
}
var sums = new int[]{0, 0, 0, 0, 0, 0};

foreach (var grade in grades)
{
if (grade == 'A')
{
sums[0] += 1;
}
else if (grade == 'B')
{
sums[1] += 1;
}
else if (grade == 'C')
{
// ...
}
}
TKO
The king of kings511d ago
I know how the switch works, but why you're using it?
A
Angius511d ago
To count how many of each grade there is..?
var sums = new int[]{0, 0, 0, 0, 0, 0};
// A B C D E F
var sums = new int[]{0, 0, 0, 0, 0, 0};
// A B C D E F
TKO
The king of kings511d ago
Ah! Ok! This one looks more clear. Ok
A
Angius511d ago
The first number is the amount of A grades, the second is the amount of B grades, and so on
TKO
The king of kings511d ago
The thing that most confuses me in this assignment is the B grade. They have mentioned that 100 or less points are A and 50 or less is F so I wonder why u used B and etc..?
A
Angius511d ago
That's usually how grades work So I made the assumption we're talking about normal, regular, American school grades Never heard of a system that would be only using A and F
Write a method that will convert the points into grades according to the scale A-F
From your assignment It is a scale from A to F A is 100 points, F is less than 50 points, the other grades are spread between those
TKO
The king of kings511d ago
You're right. But it feels like there are some mistakes in the assignment. Well! Actually, I'm chatting with you from Sweden, not from the USA.
A
Angius511d ago
Still, you probably have a grade system that consists of more than just "perfect" and "failure"
TKO
The king of kings511d ago
I know you guys in America use this system when it comes to setting grades for the students in your schools and it should be the same here in Sweden. Ok! I get your point. But the question remains, why the hell they didn't specify limited points for the rest of the grades like B, C, D, and E 🤔 this is what confuses me the most 🤨 How the hell am I gonna do this if I don't know what those grades hold a number of points 🤷🏻‍♂️
A
Angius511d ago
A - 100
B - 90
C - 80
D - 70
E - 60
F - 50
A - 100
B - 90
C - 80
D - 70
E - 60
F - 50
Distributes itself quite nicely
TKO
The king of kings511d ago
Ok! It makes sense, although you're talking about even numbers, according to my course teacher the points can be odd numbers too. For ex: he sent me a model that looks like this: The task: You need to enter points for 5 subjects and print the grade. For example: Enter points for math? 80 p Enter points for Swedish? 50 p ...... ..... Then you print out the grade Subject Grade Mathematics B Swedish E Oh! Sorry, I was wrong earlier, you're right. They want just like this form you mentioned. Sorry for taking too long with the project. Just wanna ask you about the methods that are mentioned in the assignment. Do they mean void methods or different types of methods?
TKO
The king of kings511d ago
I'm terrible at void methods, although I have watched many tutorials, but still bad at using them 🤷🏻‍♂️
TKO
The king of kings511d ago
As u c see I created a method called Convert bla bla but didn't need to use it when I did the converting in Program.cs
A
Angius511d ago
Going by the paragraphs... 1st method doesn't mention anything either way 2nd method definitely has to be non-static and probably void 3rd method definitely void 4ths method void as well
TKO
The king of kings510d ago
Ok! Well! The first one is already done. The second one needs some updating by adding these grades B, C, D, E, and F.
TKO
The king of kings510d ago
I got two errors while adding your last code
A
Angius510d ago
Pretty self-explanatory Some variable named grade already exists Use a different name
TKO
The king of kings510d ago
Ok! I changed it to grade1 instead, how about the second error! It's giving me a various solution Well! The second error is gone now too after changing it grade. The program doesn't output any result after adding your code.
A
Angius510d ago
Well, do you tell it to output anything?
TKO
The king of kings510d ago
Yes Look
TKO
The king of kings510d ago
A
Angius510d ago
What is grade?
TKO
The king of kings510d ago
You can see it inside of foreach
A
Angius510d ago
No, I can't
A
Angius510d ago
I see it being used
A
Angius510d ago
I don't see it being declared I don't see what values it has
TKO
The king of kings510d ago
Ok Well! In the beginning, it was grades and I changed it to grade because of the error. May I call you to please if you're available?
A
Angius510d ago
Not available, I'm afraid
TKO
The king of kings510d ago
Ok! It's alright. I don't really understand the code that you provided me I don't know the purpose of this code
A
Angius510d ago
Assuming your array of grades is something like [A, B, A, A, F, C, D, A, D, F], the purpose of this code is to count the occurence of each grade
TKO
The king of kings510d ago
Ok! So you're talking about the third paragraph in the assignment?
A
Angius510d ago
I believe so..?
TKO
The king of kings510d ago
Ok! I see. So how do I do that? Write a method printPrintGrades that prints the grades. Write a method Statistics that will calculate the number of "A", "C" and "F" grades and the total number of grade points. When the Statistics method is called, the grade statistics must be printed. I think the first method is complete, right? Because the project is currently printing out the grades.
A
Accord509d ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ Any modern approach to getting the calling class typename aside from using the stack frame?10 years ago I used to use the stack frame to go backwards and get what class is calling my current ✅ How can I hide buttons inside a tablelayout?When i make the visible false, they still show, and if i make the visible false on both the tablelay❔ How to reverse engineer your simple console app code?Just curious. Is this possible with programs like x64dbg or... ?❔ Ef Core Constructor problemHello its my first big project with ef. I have an class with 3 attributes and one constructor that i❔ many to many database cant update the 3rd tablecant update the 3rd table who had the cust id and drink id✅ rest requests helphello i am trying to send to os command injections with RestSharp to dvwa for pentestering can someo❔ Source generator, generating type script definitions at compile timeI would like to make a automatic type script definition from code, i consider to use source generato❔ Making an async method without anything to async insidelet's say i have following scenario: ```csharp void Foo() { for(int i = 0 ; i < 10000000 ;i++){} /❔ Reading file from the folder where .csproj isHow can i read files from folder where .csproj is? just like webapps allow to do, they do not copy f✅ Interoperability between incompatible typesHi! I'm trying to pass a class back-and-forth from C# to CPP. This class contains some members that ❔ Set item to null inside a listHey! I am trying to remove some items from a JSON object (created like `var model = new ObjectModel{Refactoring long if statement that checks for user rolesHi, I'm trying to find a cleaner way of refactoring this if code block that checks for each user rol❔ IEnumerable and dependency injectionif i want to loop enumerable from DI multiple times ``` var foos = services.GetRequiredService<IEnum❔ How did you learn . NET ?Books /youtube/udemy /other courses, what resources made you learn .NET to use as your job as web de✅ Is it possible to disable these properties on EF6? Web API 2 project, multi-layered, NF472SCENARIO - Solution type is NET Framework 4.7.2 (it is, what it is). - I have this project, I'm cons✅ This EntityFramework query can not be translated for SQLiteThe issue lies within `Intersect`. The point of the query is to fetch the current user, and to popul✅ Detect Network RequestI want make service that detect all connection to specific domain or ip? And if this request contain❔ CORS errorHello, I have Cors error on my project. I add Addpolicy in my configureServices and also add app.Us❔ Ambiguities? How?Idk how i can resolve this i don't see the problem✅ How can I get rid of this annoying pointer thing in the first row, datagridview winforms^