• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

poweld

Member
It will, but he has quite a few options.

Code:
var x=75;
var output;
switch (true)
{
  case (x < 70):
    output="D";
    break;
  case (x < 80):
    output="C";
    break;
  ....
}

I think that switch statement is unintuitive, but sure, that'd work as well.
 

Godslay

Banned
I need help with a program. I need to Write a function that takes one numeric parameter as students grade and returns letter grade value for that grade. These are the grades i need to do it with:

>93 "A"
90-93 "A-"
87 - 93 "B+"
84 - 87 "B"
80 - 84 "B-"
75 - 80 "C+"
70 - 75 "C"
<70 "F"

Here's what I have based on these requirements. Kind of odd that it skips around on the C portion. If you are going to copy/paste it, just make sure you understand what it is doing and check it.

Code:
var x = prompt("Enter a value");

if (x >= 93)
{
	console.log("A");
}

else if (x < 93 && x >= 90)
{
	console.log("A-");
}

else if (x < 90 && x >= 87)
{
	console.log("B+");
}

else if (x < 87 && x >= 84)
{
	console.log("B");
}

else if (x < 84 && x >= 80)
{
	console.log("B-");
}

else if (x < 80 && x >= 75)
{
	console.log("C+");
}

else if (x < 75 && x >= 70)
{
	console.log("C");
}

else
{
	console.log("F");
}

Probably would be a bit more concise with a switch.
 
I think that switch statement is unintuitive, but sure, that'd work as well.

For sure...but when it comes to testing and maintenance, I'd rather that approach to reduce the number of magic values.

All neither here nor there for the example. Ifs would probably be the way to go.
 
Here's what I have based on these requirements. Kind of odd that it skips around on the C portion. If you are going to copy/paste it, just make sure you understand what it is doing and check it.

Code:
var x = prompt("Enter a value");

if (x >= 93)
{
	console.log("A");
}

else if (x < 93 && x >= 90)
{
	console.log("A-");
}

else if (x < 90 && x >= 87)
{
	console.log("B+");
}

else if (x < 87 && x >= 84)
{
	console.log("B");
}

else if (x < 84 && x >= 80)
{
	console.log("B-");
}

else if (x < 80 && x >= 75)
{
	console.log("C+");
}

else if (x < 75 && x >= 70)
{
	console.log("C");
}

else
{
	console.log("F");
}

Probably would be a bit more concise with a switch.

Yea i am definitely going to go over this. Thanks a lot, i appreciate the help.
 

shintoki

sparkle this bitch
Okay, this week's help

#include <iostream>
#include <math.h>
#include <string>
#include <cmath>
using namespace std;

int main()
{
double test, test1, test2, test3;
cout<< " Hi I will help you troubleshoot this diesel engine.\n \n" ;
cout<< " First check the color of the Check status light.\n \n" ;
cout<< " If the light is Green press: 1\n\n If the light is Red press: 2\n\n If the light is Amber press: 3 \n\n" ;
cin>> test;
if (test==1)
{
cout<< " Do restart precedure.\n" ;
system ("pause") ;
}
else if (test==2)
{
cout<< " Shut-off all input lines check meter #3\n\n" ;
cout<< " What is the value of the meter?" ;
cin>> test1 ;
}
else if (test==3)
{
cout<<" Check the fuel line service routine." ;
system ("pause") ;
}
else if (test1 < 50)
{
cout<< " Check main line for test pressure.\n\n";
cout<< " Please enter 1 if the pressure is normal\n\n";
cout<< " Please enter 2 if the pressure is low or high. \n\n";
cin>> test2;
}
if (test1 >= 50)
{
cout<< " Measure the flow velocity at inlet 2-B";
cout<< " Please enter 1 if the pressure is normal\n\n";
cout<< " Please enter 2 if the pressure is low or high \n\n";
cin>> test3;
}
if ( test2 ==1)
{
cout<< " Refer to motor service manual\n\n";
system ("pause");
}
if ( test2 ==0)
{
}
if ( test2 ==2)
{
cout<< " Refer to main line manual\n\n ";
system ("pause");
}
if (test1 >=50)
{
cout<< (" Measure the flow velocity at inlet 2-b\n\n\n");
cout<< (" 1) Normal \n\n 2) High or low \n\n");
cin>> test3;
}
if (test3 ==1)
{
cout<< " Refer to inlet service manual \n\n";
system ("pause");
return 0;
}
if (test3 ==2)
{
cout<< " Refer to unit for factory service\n";
system ("pause");
}
else if (test ==3)
{
cout<< " Check the fuel line service routine\n";
}

// end program
return 0;
}

I keep getting an error, but I have no idea where at. No little red line under neath and it's just not loading

Maybe something to do with " warning C4700: uninitialized local variable 'test1' used"

"1> Creating "Debug\green.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:"
 
I keep getting an error, but I have no idea where at. No little red line under neath and it's just not loading

Maybe something to do with " warning C4700: uninitialized local variable 'test1' used"

"1> Creating "Debug\green.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:"

At the start you declare

double test1;

This reserves a spot in memory and all is well. However late on you try to run a comparison on test1, but it hasn't necessarily been set to anything yet. Hence you get an error.

What you should do is initialise all your test variables to some value or constant. Typically for an application like this I met use a static constant like UNSPECIFIED = -1 and then test1 = UNSPECIFIED. Then you can use this in your logic to know if further tests should be run.

Also logically I think the line:

else if (test1 < 50)

Should just be a stand alone "if"?
 

shintoki

sparkle this bitch
At the start you declare

double test1;

This reserves a spot in memory and all is well. However late on you try to run a comparison on test1, but it hasn't necessarily been set to anything yet. Hence you get an error.

What you should do is initialise all your test variables to some value or constant. Typically for an application like this I met use a static constant like UNSPECIFIED = -1 and then test1 = UNSPECIFIED. Then you can use this in your logic to know if further tests should be run.

Also logically I think the line:

else if (test1 < 50)

Should just be a stand alone "if"?

I'm not quite comprehending what you mean.

I should create more variables for it and use those specifically?

Can you sort of show me what you mean too, I'm a very visual learner lol
 
I'm not quite comprehending what you mean.

I should create more variables for it and use those specifically?

What I'm saying is

double test;
If (test == 0)

Will break because "test" has been created but not initialised to any value. It is kind of just floating around.

double test = 1;
If (test == 0)

Is right to go because we set it to 1 first. Hence the compiler knows how to compare 0 and 1 and can do its magic.
 

poweld

Member
I'm not quite comprehending what you mean.

I should create more variables for it and use those specifically?

Can you sort of show me what you mean too, I'm a very visual learner lol

I'll tell you two things regarding this code; one will make it vastly easier to read and understand, and the other will help you solve the problem.

1. Do not use ambiguous variable names like "test", "test1", "test2", and "test3". Looking at your code I have no idea what each branch is meant to do because of these names. Change them to things like "buttonChoice", "lineCheckValue", "mainLinePressure", and "flowVelocity". This sort of helpful naming will help you a lot here as well as in the future.

2. As toddhunter mentioned, your declared variables begin undefined. This means that their values are not 0 to begin with, the value is unknown until runtime.

Basically, you should never be accessing a variable without either first declaring a default value, or reading in some other value.

  • Since you never read in a value for "test1" (lineCheckValue) before you check what it's value is, you are running into this error.
  • Furthermore, you only read in a value for "test2" (mainLinePressure) when the lineCheckValue is < 50, but you still check its value later whether or not it's been set.
  • Same goes for "test3" (flowVelocity), except you only read that value in when lineCheckValue >= 50, though once again, you check it outside of the scope of when the value is set... Also, you have two nearly identical blocks for when "test1" >= 50

Finally, you probably don't need to use the "double" type for this kind of math, since that type indicates the use of floating point arithmetic (fractions), when you're only using integers. You may want to stick to "int" types when doing simple comparisons like this.
 

Jill Sandwich

the turds of Optimus Prime
Trying to learn JavaScript.

Code:
var testObject = {
	bum: "I'm a bum",
	rat: "I'm a rat",
	gibberish: ["Blue", 2, "Cisco"]
};

var passInParameter = function(param){
for (x in param){
	console.log(x)
}
}

passInParameter(testObject);

At the moment this logs to the console the elements of testObject (bum,rat,gibberish). How do I get it to log the values of the elements?
 
for in in JS gets you the attribute names of the object, not the values, you need to index into the object with those names to get them:

Code:
var testObject = {
	bum: "I'm a bum",
	rat: "I'm a rat",
	gibberish: ["Blue", 2, "Cisco"]
};

var passInParameter = function(param){
for (x in param){
	console.log([B]param[x][/B])
}
}

passInParameter(testObject);
 

Jill Sandwich

the turds of Optimus Prime
Fantastic, thanks. Codecademy just threw in for/in loops into a lesson with no explanation of how they work. I've been doing okay until that.
 
If Flex/AS3 function ensureIndexIsVisible isn't the most broken built in function in the history of mankind I don't know what is.

/vent
 
for in in JS gets you the attribute names of the object, not the values, you need to index into the object with those names to get them:
Fantastic, thanks. Codecademy just threw in for/in loops into a lesson with no explanation of how they work. I've been doing okay until that.
It's probably worth getting to know hasOwnProperty now, even if you're unable to type it into CodeAcademy and get the 'right' answer.

EDIT: Just to clarify. You're better off wrapping the printing of param[x] like so:
Code:
for (var x in param) {
    if (param.hasOwnProperty(x)) {
        console.log(param[x]);
    }
}
You should also be declaring x with the var keyword - Javascript will hoist x up onto the global scope otherwise, which is bad news.
 

Seanbob11

Member
I'm having real trouble getting to terms with F# for Uni.

Giving a list of numbers, how do you search for a single number. I can't seem to find any online resource that helps me.

Thanks guys.
 

poweld

Member
I'm having real trouble getting to terms with F# for Uni.

Giving a list of numbers, how do you search for a single number. I can't seem to find any online resource that helps me.

Thanks guys.

What do you mean "search for a single number"? Determine whether that number exists in the list?

Based on my 5 seconds of experience with F# gained by looking at the wikipedia entry, I'd say you'd want something like this:

Code:
someValue = 10
seq {
  for n in numbers do 
    if n == someValue then 
      yield true
}
 
Since F# lets you be functional you should probably use a filter function.

Disclaimer: I too have 5 seconds of googling experience of F# and nothing else.

Maybe something like:
Code:
List.filter (fun n -> n = numberWeWant) numberList

Filter should return a list made up of the values in numberlist that were true in the predicate. In this case the predicate we define is a function that evaluates true if the argument is the number we want, false otherwise. Does this help?
 
This will work if you know that the name will definitely be present:

Code:
let (num, _) = List.nth (List.filter (fun (_, n) -> n = "JAKE") pupils) 0

What we're doing is taking the first value from the list that results from the filter function applied to pupils and binding num to the first element in the tuple. I can go into more detail on the specifics if you'd like.
 
It's because the
Code:
 (n, _)
is destructuring the tuple. I don't know about F# other than for tuples, but in some functional languages (Haskell and Clojure at least) you can pull apart data structures by using the same syntax as constructing them.

So, for example,
Code:
let (a, b) = (1, 2)
Would bind a to 1 and b to 2. In the example I gave you, we don't care about the second variable so we use '_'. This is the convention in Clojure and Haskell, not sure if it is in F#.
 

usea

Member
That threw me so much when I first came across it. Any idea why it's designed this way?
javascript wasn't designed. An evil demigod from the future visited 1986 on brief, 5-year vacation of horror and destruction. When he left, he erased all memories of his reign of terror from the minds of the survivors. However the trauma sustained by those who survived was so severe that it persisted in humanity as a collective. It gained sentience, and around 1994 it thrust itself into being in the form of JavaScript. We've been under siege ever since.
 
javascript wasn't designed. An evil demigod from the future visited 1986 on brief, 5-year vacation of horror and destruction. When he left, he erased all memories of his reign of terror from the minds of the survivors. However the trauma sustained by those who survived was so severe that it persisted in humanity as a collective. It gained sentience, and around 1994 it thrust itself into being in the form of JavaScript. We've been under siege ever since.

I take it you've never had the desire to retrieve just the attribute names then :p
 

D4Danger

Unconfirmed Member
I looked into Coffeescript couple of months ago and it really seems to iron out some of the "WHY ON EARTH WOULD YOU DO IT LIKE THAT"-things out of JS.

every programming language has its quirks. Switching to a completely different one is just throwing out the baby with the bath water.

If you really want a little more sanity in your JavaScript I would recommend something like Microsoft's TypeScript over coffeescript or dart or whatever language people think is going to replace js this week.
 

hateradio

The Most Dangerous Yes Man
every programming language has its quirks. Switching to a completely different one is just throwing out the baby with the bath water.

If you really want a little more sanity in your JavaScript I would recommend something like Microsoft's TypeScript over coffeescript or dart or whatever language people think is going to replace js this week.
TypeScript seems interesting, but I feel it's overkill.

CoffeeScript is okay if you really just want to write code like Python or Ruby. If you don't, then just stick with plain JS.
 

moniker

Member
Here's what I have based on these requirements. Kind of odd that it skips around on the C portion. If you are going to copy/paste it, just make sure you understand what it is doing and check it.

Code:
 ...

Probably would be a bit more concise with a switch.


In this specific example where each conditional just has one statement, shorthand ifs would be the most concise:

Code:
var x = prompt("Enter a value");

x >= 93 ? console.log("A") :
x < 93 && x >= 90 ? console.log("A-") :
x < 90 && x >= 87 ? console.log("B+") :
x < 87 && x >= 84 ? console.log("B") : 
x < 84 && x >= 80 ? console.log("B-") :
x < 80 && x >= 75 ? console.log("C+") :
x < 75 && x >= 70 ? console.log("C") : 
console.log("F");
 

Godslay

Banned
In this specific example where each conditional just has one statement, shorthand ifs would be the most concise:

Code:
var x = prompt("Enter a value");

x >= 93 ? console.log("A") :
x < 93 && x >= 90 ? console.log("A-") :
x < 90 && x >= 87 ? console.log("B+") :
x < 87 && x >= 84 ? console.log("B") : 
x < 84 && x >= 80 ? console.log("B-") :
x < 80 && x >= 75 ? console.log("C+") :
x < 75 && x >= 70 ? console.log("C") : 
console.log("F");

Nice, I tend to shy away from ternary operator out of habit. It is better for one liners for sure.
 
In this specific example where each conditional just has one statement, shorthand ifs would be the most concise:

Code:
var x = prompt("Enter a value");

x >= 93 ? console.log("A") :
x >= 90 ? console.log("A-") :
x >= 87 ? console.log("B+") :
x >= 84 ? console.log("B") : 
x >= 80 ? console.log("B-") :
x >= 75 ? console.log("C+") :
x >= 70 ? console.log("C") : 
console.log("F");

You want it more concise, you get rid of the < comparisons as edited above. They're unnecessary.
 
When you are past the learning step, you should really try coffescript.
every programming language has its quirks. Switching to a completely different one is just throwing out the baby with the bath water.

If you really want a little more sanity in your JavaScript I would recommend something like Microsoft's TypeScript over coffeescript or dart or whatever language people think is going to replace js this week.
TypeScript seems interesting, but I feel it's overkill.

CoffeeScript is okay if you really just want to write code like Python or Ruby. If you don't, then just stick with plain JS.
The bottom line with all of these languages (Dart, CoffeeScript, TypeScript, etc.) is Javascript. If you don't understand JS (what it does right, what it does dangerously wrong), then you don't understand the need for any of these other language offshoots that are being suggested and you probably shouldn't be adding another layer of abstraction.

As a general rule, layers of abstraction just make the really dangerous mistakes more obfuscated and less apparent because the programmer who's been living in that walled garden is unaware of the dangers that lurk at lower levels. There are a lot of people who think that memory leaks aren't possible in Java (but... garbage collection?!?), which is downright hilarious.

The initial question was from someone working their way through a CodeAcademy tutorial. So, OP... Please. Ignore the distractions above. Finish the tutorial. Ruminate. Then read Javascript: The Good Parts by Douglas Crockford. Ruminate a whole lot more. Try a few projects. If you're really finding curly braces and variable hoisting to be too much to handle, then you can think carefully about adding some sugar to your Javascript, but that's probably a dangerous indication that you're uncomfortable with other aspects like prototypes and functions as first class citizens.

At the end of the day, though, everything listed above is still Javascript, and if the inherent issues in JS are too much to overcome, you might need to approach things from another paradigm.
 
The bottom line with all of these languages (Dart, CoffeeScript, TypeScript, etc.) is Javascript. If you don't understand JS (what it does right, what it does dangerously wrong), then you don't understand the need for any of these other language offshoots that are being suggested and you probably shouldn't be adding another layer of abstraction.

As a general rule, layers of abstraction just make the really dangerous mistakes more obfuscated and less apparent because the programmer who's been living in that walled garden is unaware of the dangers that lurk at lower levels. There are a lot of people who think that memory leaks aren't possible in Java (but... garbage collection?!?), which is downright hilarious.

The initial question was from someone working their way through a CodeAcademy tutorial. So, OP... Please. Ignore the distractions above. Finish the tutorial. Ruminate. Then read Javascript: The Good Parts by Douglas Crockford. Ruminate a whole lot more. Try a few projects. If you're really finding curly braces and variable hoisting to be too much to handle, then you can think carefully about adding some sugar to your Javascript, but that's probably a dangerous indication that you're uncomfortable with other aspects like prototypes and functions as first class citizens.

At the end of the day, though, everything listed above is still Javascript, and if the inherent issues in JS are too much to overcome, you might need to approach things from another paradigm.

That's why I said "when you are past the learning step" ;)

I understand JS enough to not wanting to go back to it anytime soon. A little syntactic sugar is great when trying to overcome JS problems.
 

xJavonta

Banned
Do you guys think it's possible for me to learn 4 weeks of VBasic lectures in a week? I've been slacking this semester and I don't want to fuck myself over when midterms come around.
 
Do you guys think it's possible for me to learn 4 weeks of VBasic lectures in a week? I've been slacking this semester and I don't want to fuck myself over when midterms come around.

(Today, 04:20 PM)
image.php


Serious answer: Yes, start right now. Ask here if you need help.
 

Jokab

Member
Does anybody have experience with the WindowBuilder plugin editor for Eclipse to design with Java Swing? I want to have a main JFrame, and then put modules in form of JPanels, which I make a separate class, into that window as a custom component. However, it seems that after you've added the component to the main JFrame, you can't edit it. So having to fit the dimensions of the custom JPanel to the main JFrame will be a pain in the ass it seems. Is there a better way to handle this?
 

xJavonta

Banned
(Today, 04:20 PM)
image.php


Serious answer: Yes, start right now. Ask here if you need help.
Thanks! Will do, I've had this thread bookmarked and subbed for a while now but I don't know why I haven't posted in it.

VB or VB.Net? Either way, outside of a few quirks VB is a really easy language to pick up.
Yeah I have no trouble understanding the lectures. But I don't retain the information. I really stopped going to lectures consistently about 2 weeks ago and that's when we started TryParse. Bad idea haha

Yeah as long as you don't rush everything without understanding it.
That's my problem right now, really. I don't spend any time outside of class learning it, and I only open the book in class to do the exercises with the class but when I leave class, I don't do anything with it. I actually like programming too, it's just that I haven't had the motivation to do anything when I'm not really required to.
 
Top Bottom