C
C#•4w ago
Archion

C# homework

I don't know if i made a good code or not
25 Replies
Archion
ArchionOP•4w ago
using System;
using System.IO;

namespace Beadando2;
class Program{
static void Main(string[] args){
FileStream fs = new FileStream("be2.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);

int n = Convert.ToInt32(sr.ReadLine());
int[] H = new int[n];
int[] P = new int[n];
int[] Mp= new int[n];

int temp = 0;
while(!sr.EndOfStream)
{
string[] line = sr.ReadLine().Split(' ');
H[temp]=Convert.ToInt32(line[0]);
P[temp]=Convert.ToInt32(line[1]);
Mp[temp]=Convert.ToInt32(line[2]);

temp++;
}
sr.Close();
fs.Close();

int[] ki=new int[2];


temp = 0;
for(int i=1;i<n;i++){
if(H[temp]>3){
temp=i;
continue;
}

if(H[i]<=3){
if(P[i]*60+Mp[i] > P[temp]*60+Mp[temp]){
temp=i;
}
}
}
ki[0]=temp;
temp = 0;
for(int i=1;i<n;i++){
if(H[temp]>3){
temp=i;
continue;
}

if(H[i]<=3){
if(P[i]*60+Mp[i] < P[temp]*60+Mp[temp]){
temp=i;
}
}
}
ki[1] = temp;
Console.WriteLine((ki[0]+1) + " " + (ki[1]+1));
}
}
using System;
using System.IO;

namespace Beadando2;
class Program{
static void Main(string[] args){
FileStream fs = new FileStream("be2.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);

int n = Convert.ToInt32(sr.ReadLine());
int[] H = new int[n];
int[] P = new int[n];
int[] Mp= new int[n];

int temp = 0;
while(!sr.EndOfStream)
{
string[] line = sr.ReadLine().Split(' ');
H[temp]=Convert.ToInt32(line[0]);
P[temp]=Convert.ToInt32(line[1]);
Mp[temp]=Convert.ToInt32(line[2]);

temp++;
}
sr.Close();
fs.Close();

int[] ki=new int[2];


temp = 0;
for(int i=1;i<n;i++){
if(H[temp]>3){
temp=i;
continue;
}

if(H[i]<=3){
if(P[i]*60+Mp[i] > P[temp]*60+Mp[temp]){
temp=i;
}
}
}
ki[0]=temp;
temp = 0;
for(int i=1;i<n;i++){
if(H[temp]>3){
temp=i;
continue;
}

if(H[i]<=3){
if(P[i]*60+Mp[i] < P[temp]*60+Mp[temp]){
temp=i;
}
}
}
ki[1] = temp;
Console.WriteLine((ki[0]+1) + " " + (ki[1]+1));
}
}
the task is: there is race where it is recorded in the first line the number of race he ran H array is the places he got P array is the minutes it needed to finish and Mp array is the seconds i had to get the fastest race first and last index so max and min but the catch is the place must be at least 3 or 2 or 1
10
7 9 6
7 4 29
4 0 12
6 2 30
2 4 50
3 1 39
7 5 6
9 2 18
10 8 6
3 1 5
10
7 9 6
7 4 29
4 0 12
6 2 30
2 4 50
3 1 39
7 5 6
9 2 18
10 8 6
3 1 5
this input file and it should write 4 and 5 but mine is 4 and 9
Pobiega
Pobiega•4w ago
can you explain a bit more about the "catch" and the "fastest race first and last index" parts? also, wouldnt it be easier to represent each race as an object instead of an array of each metric? oh okay I get it now. Yeah this would be a lot easier if you modelled it as an object, like a record
Archion
ArchionOP•4w ago
no because no struct or class sry
Pobiega
Pobiega•4w ago
wdym? are you not allowed to use structs or classes for this particular homework?
Archion
ArchionOP•4w ago
i am lazy to do in specification
Pobiega
Pobiega•4w ago
?
Archion
ArchionOP•4w ago
specification -> structogram -> code
Pobiega
Pobiega•4w ago
silly for a thing like this but to answer your question... this is not good code it can easily be done while reading from the file, as opposed to reading all the values into weird arrays first, or even better and readable with using an object to hold the values
Archion
ArchionOP•4w ago
sir but the code is good semantically?
Pobiega
Pobiega•4w ago
no
Archion
ArchionOP•4w ago
so it doesn't do what it has to?
Pobiega
Pobiega•4w ago
I mean you just said that yourself.
No description
Archion
ArchionOP•4w ago
broooo they are giving me a headache probably the output file is bad
Pobiega
Pobiega•4w ago
your expected value is wrong, yes look at race 5 3 1 39 vs race 9 3 1 5 race 9 is the fastest top 3 race at 65 total seconds race 4 is the slowest top 3 race, at 290 seconds
FusedQyou
FusedQyou•4w ago
Please use proper naming for your variable names :oh: How is anybody supposed to read this What is H? What is P? What is Mp?? I understand you mentioned this in the text below but it should be in the variable name in general
Pobiega
Pobiega•4w ago
like, look at your code, and compare it to this
var i = 0;
var races = File.ReadAllLines(ArchionFileName)
.Skip(1) // skip header
.Select(x => (content: x, index: i++)) // add index, so we can output it later
.Select(x => Race.FromString(x.content, x.index));

var top3 = races.Where(x => x.Placement <= 3).ToList(); // we only care about races where we finished top 3

var fastest = top3.MinBy(x => x.TotalSeconds);
var slowest = top3.MaxBy(x => x.TotalSeconds);

Console.WriteLine($"The fastest race was {fastest.Index} with {fastest.TotalSeconds} seconds");
Console.WriteLine($"The slowest race was {slowest.Index} with {slowest.TotalSeconds} seconds");
var i = 0;
var races = File.ReadAllLines(ArchionFileName)
.Skip(1) // skip header
.Select(x => (content: x, index: i++)) // add index, so we can output it later
.Select(x => Race.FromString(x.content, x.index));

var top3 = races.Where(x => x.Placement <= 3).ToList(); // we only care about races where we finished top 3

var fastest = top3.MinBy(x => x.TotalSeconds);
var slowest = top3.MaxBy(x => x.TotalSeconds);

Console.WriteLine($"The fastest race was {fastest.Index} with {fastest.TotalSeconds} seconds");
Console.WriteLine($"The slowest race was {slowest.Index} with {slowest.TotalSeconds} seconds");
I think mine is a lot easier to read and understand Race is my custom struct that holds the placement, minutes and seconds from the file, and also adds a TotalSeconds property
Archion
ArchionOP•4w ago
I didn't choose the variable names I had to name it like that Um actually 🤓 We musn't use linq because "We didn't learn it"
FusedQyou
FusedQyou•4w ago
?????????????????????????? Your teacher sucks
Archion
ArchionOP•4w ago
Not the teacher The system
FusedQyou
FusedQyou•4w ago
I don't understand what you mean Who forces you to use abysmal naming conventions?
Archion
ArchionOP•4w ago
She teaches what she is being told to do Its my language brah
FusedQyou
FusedQyou•4w ago
Single letter variable is not tied to a language You should honestly just learn to use proper naming, even if you are shown not to. It is a nice learning excercise since you don't directly copy over code that way Surely you can see that your code is practically unreadable yourself?
Pobiega
Pobiega•4w ago
H P Mp are not language, just bad variable names temp is also terrible
FusedQyou
FusedQyou•4w ago
That said, I really can't help you because I can't even read the code myself These things are made trivial with LINQ, but in the very least you should probably look into creating methods for some common tasks like getting the smallest number and such if you can'tuse those. Your code now is just a big wall of code that does something but it's very hard to figure out when so many things happen with so little info in the naming
Archion
ArchionOP•4w ago
this is what i told to call them don't judge me
Want results from more Discord servers?
Add your server