C
C#3w ago
aj

Need help writing specification for a task

It's a little confusing with the tools i've been provided
17 Replies
Buddy
Buddy3w ago
$details
MODiX
MODiX3w ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code! (see $code for more information on how to paste your code)
aj
ajOP3w ago
this is the task at hand
No description
aj
ajOP3w ago
Input: Output: Precondition: Postcondition: and have test cases put out for it and we have to use specific patterns Like Counting, and Decision
Buddy
Buddy3w ago
We will not do it for you, we only help with certain questions (Q&A) and not entire tasks. Start by making a console application and go your way forward. Split up the task into small tasks
aj
ajOP3w ago
okay so first i declare everything in the input like so In: n ∈ N, m ∈ N, oxy ∈ N[1..n] nitro ∈ N[1..n], danger ∈ R[1..n, 1..m] and its pretty obvious i need to use a counting pattern so i came up with a pattern but im having issues getting it to work $code
MODiX
MODiX3w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
aj
ajOP3w ago
In: n∈N, m∈N, planetData∈Data[1..n], Data = (ox:N x nt:N), dangerous∈R[1..n,1..m] Out: livablePlanets∈N Pre: 1≤n≤100 and 1≤m≤10 and ∀i∈[1..n]:((1≤planetData[i].ox≤99) and (1≤planetData[i].nt≤99) and ∀j∈[1..m]:(0 ≤ dangerous ≤ 2)) Post: livablePlanets=COUNT(i=1..n, 19≤planetData[i].ox≤24 and 76≤planetData[i].nt≤81 and ∀j∈[1..m]:( dangerous ≤ 0.5)) this is what i came up with
SleepWellPupper
Great! Put it into code. As Buddy says, get a console application running and start playing around with your proposed ruleset in the code
aj
ajOP3w ago
alr! lets give it a shot i figured it all out so i decided to make a structogram aswell and thats when it started clicking
aj
ajOP3w ago
No description
aj
ajOP3w ago
and i followed along with code $code
MODiX
MODiX3w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
aj
ajOP3w ago
// using System;
using System.Globalization;

namespace algorithm
{
internal class Program
{
struct Planet
{
public int ox;
public int nt;
public double[] d;
}

static void Main(string[] args)
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

string[] firstLine = Console.ReadLine().Split();
int n = int.Parse(firstLine[0]);
int m = int.Parse(firstLine[1]);

Planet[] data = new Planet[n];
for (int i = 0; i < n; i++)
{
string[] line = Console.ReadLine().Split();
data[i].ox = int.Parse(line[0]);
data[i].nt = int.Parse(line[1]);

data[i].d = new double[m];
double sum = data[i].ox + data[i].nt;

for (int j = 0; j < m; j++)
{
data[i].d[j] = double.Parse(line[j + 2]);
sum += data[i].d[j];
}

if (data[i].ox < 1 || data[i].ox > 99 ||
data[i].nt < 1 || data[i].nt > 99)
{
continue;
}

bool allInRange = true;
for (int j = 0; j < m; j++)
{
if (data[i].d[j] < 0 || data[i].d[j] > 2)
{
allInRange = false;
}
}

if (!allInRange || Math.Abs(sum - 100.0) > 0.0001)
{
continue;
}
}

int planets = 0;

for (int i = 0; i < n; i++)
{
bool valid = true;

if (data[i].ox >= 19 && data[i].ox <= 24 &&
data[i].nt >= 76 && data[i].nt <= 81)
{
for (int j = 0; j < m; j++)
{
if (data[i].d[j] > 0.5)
{
valid = false;
}
}

if (valid)
{
planets++;
}
}
}

Console.WriteLine(planets);
}
}
}
// using System;
using System.Globalization;

namespace algorithm
{
internal class Program
{
struct Planet
{
public int ox;
public int nt;
public double[] d;
}

static void Main(string[] args)
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

string[] firstLine = Console.ReadLine().Split();
int n = int.Parse(firstLine[0]);
int m = int.Parse(firstLine[1]);

Planet[] data = new Planet[n];
for (int i = 0; i < n; i++)
{
string[] line = Console.ReadLine().Split();
data[i].ox = int.Parse(line[0]);
data[i].nt = int.Parse(line[1]);

data[i].d = new double[m];
double sum = data[i].ox + data[i].nt;

for (int j = 0; j < m; j++)
{
data[i].d[j] = double.Parse(line[j + 2]);
sum += data[i].d[j];
}

if (data[i].ox < 1 || data[i].ox > 99 ||
data[i].nt < 1 || data[i].nt > 99)
{
continue;
}

bool allInRange = true;
for (int j = 0; j < m; j++)
{
if (data[i].d[j] < 0 || data[i].d[j] > 2)
{
allInRange = false;
}
}

if (!allInRange || Math.Abs(sum - 100.0) > 0.0001)
{
continue;
}
}

int planets = 0;

for (int i = 0; i < n; i++)
{
bool valid = true;

if (data[i].ox >= 19 && data[i].ox <= 24 &&
data[i].nt >= 76 && data[i].nt <= 81)
{
for (int j = 0; j < m; j++)
{
if (data[i].d[j] > 0.5)
{
valid = false;
}
}

if (valid)
{
planets++;
}
}
}

Console.WriteLine(planets);
}
}
}
canton7
canton73w ago
Not a bad start. Note that you've got a lot of logic in your first loop to validate that the levels are all within the given ranges, but if they're not, you don't do anything different. So over half of your first loop makes no difference to anything. Also, note that validating stuff is all well and good (although in this case I'd question whether you do need to validate that the levels are in the expected ranges: nothing in the task says you need to), but if you just silently reject bad data that leads to very confusing problems where stuff doesn't work and you don't know why. So if you're going to do validation, and something fails validation, at least output a message saying what failed and why Programming languages also don't have a tax on characters. DangerousElements is a lot clearer than d, for example. Code gets read far more often than it's written, so making your code clear to the reader is one of the most important things you can do
canton7
canton73w ago
Isn't this a bit easier to read? https://paste.mod.gg/zgfxbxnteczp/0
BlazeBin - zgfxbxnteczp
A tool for sharing your source code with the world!
many things
many things3w ago
never heard of structograms before

Did you find this page helpful?