• 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

Chris R

Member
I strongly recommend LINQPad for quickly running C# code. I can't say it's better than Powershell because I don't know powershell. But it's pretty fantastic. I use it every day it quickly type up and run some C# code.

Yep, this is a great program to have if you are a .Net dev.
 

Nesotenso

Member
ok I am teaching myself UNIX commands and I have a question about the cp command

now the general format for it is: cp file1 file2

what if want to keep the same file name in the same directory

I tried cp science.txt . to make a copy of the text file and keep it in the same directory but it didn't work.
 
ok I am teaching myself UNIX commands and I have a question about the cp command

now the general format for it is: cp file1 file2

what if want to keep the same file name in the same directory

I tried cp science.txt . to make a copy of the text file and keep it in the same directory but it didn't work.

Why do you want to copy the file on itself?
 

Nakazato

Member
Likely not the right place to ask this. But I'm looking for a career change to IT. Anyone know a goos low lvl certification I should go for Thinking about trying A+ and and my network. Any suggestions ?
 

Nesotenso

Member
Use Linux and let it come naturally as you work on other things.

thing is I am looking at a career as a ASIC verification/ design or analog/digital IC engineer and most positions require you to have some competency in C/C++ and Unix shell scripting.
I have used Fedora from Red Hat in school and I am on messing around with Cygwin.

But any book recommendations would be great.
 

Gonff

Banned
I learned Unix by setting up a home Linux server.

The best way to learn is to just use it. Everytime you have an issue, Google it and find a solution.
 
Hi,

I know this probably isn't the right place for this since my question isn't a programming question but I'll post it here anyhow search and did not find a SQL thread.

I have to do a SQL query where I have to output the first and last names of employees who work on the same project and I cannot have duplicates or reversed pairs. I been trying for hours to solve this but the closes I have gotten still results in a Cartesian product.

My output has to appear something like this


1 John Doe - Jane Doe
1 Jonn Doe - Ben Frank
1 Jane Doe - Thomas Jeff

etc
etc

However when I run my current query I get this


1 John Doe - John Doe
1 John Doe - Ben Frank
1 John Doe - Jane Doe
1 Ben Frank - Ben Frank
1 Ben Frank - John Doe
1 Ben Frank - Jane Doe
1 Jane Doe - Jane Doe
1 Jane Doe - John Doe
1 Jane Doe - Ben Frank

etc
etc


My current query looks like this

Code:
Select pno, employee.fname, employee.lname, employee2.fname, employee2.lname
From employee, employee AS employee2, works_on
Where pno = 1 
AND employee.ssn = essn
INTERSECT
Select pno, employee.fname, employee.lname, employee2.fname, employee2.lname
From employee, employee AS employee2, works_on
Where pno = 1 
AND employee2.ssn = essn


The employee table contains the people's first and last names and SSN and the works_on table contains a list of employee SSNs and the project number(pno) that SSN works on.

I will appreciate any help that anyone can give me.
 

nan0

Member
I have to do a SQL query where I have to output the first and last names of employees who work on the same project and I cannot have duplicates or reversed pairs. I been trying for hours to solve this but the closes I have gotten still results in a Cartesian product.

I'd need the full schema to fiddle with it, but off the top of my head this looks very much like a job for an INNER JOIN on ssn.
 

poweld

Member
Code:
SELECT
  works_on.pno,
  e1.fname,
  e1.lname
FROM
  employee AS e1
INNER JOIN
  employee AS e2
INNER JOIN
  works_on
ON
  e1.ssn = works_on.essn
ON
  e1.pno = e2.pno
WHERE
  e1.ssn != e2.ssn

We're joining the employee column on itself, finding all employees that have the same project number while filtering out when we join an employee on himself.
 

maeh2k

Member
Code:
Select pno, employee.fname, employee.lname, employee2.fname, employee2.lname
From employee, employee AS employee2, works_on
Where pno = 1 
AND employee.ssn = essn
INTERSECT
Select pno, employee.fname, employee.lname, employee2.fname, employee2.lname
From employee, employee AS employee2, works_on
Where pno = 1 
AND employee2.ssn = essn

First, I'd recommend that you really think about what your query is doing and maybe try to do it manually (like on paper) with a minimal table. Look at what exactly you are intersecting and why it doesn't do what you want it to.

Then try poweld's solution (with a e1.ssn < e2.ssn instead of !=).
 

poweld

Member
First, I'd recommend that you really think about what your query is doing and maybe try to do it manually (like on paper) with a minimal table. Look at what exactly you are intersecting and why it doesn't do what you want it to.

Then try poweld's solution (with a e1.ssn < e2.ssn instead of !=).

Wouldn't a < cause some of the employees to not be compared with others? I'm not sure I understand.
 
I'd need the full schema to fiddle with it, but off the top of my head this looks very much like a job for an INNER JOIN on ssn.

Code:
SELECT
  works_on.pno,
  e1.fname,
  e1.lname
FROM
  employee AS e1
INNER JOIN
  employee AS e2
INNER JOIN
  works_on
ON
  e1.ssn = works_on.essn
ON
  e1.pno = e2.pno
WHERE
  e1.ssn != e2.ssn

We're joining the employee column on itself, finding all employees that have the same project number while filtering out when we join an employee on himself.

First, I'd recommend that you really think about what your query is doing and maybe try to do it manually (like on paper) with a minimal table. Look at what exactly you are intersecting and why it doesn't do what you want it to.

Then try poweld's solution (with a e1.ssn < e2.ssn instead of !=).

Thank you guys for your help. I have been fiddling around with what you guys have recommended but unfortunately I still have been unable to produce the correct results.
 

maeh2k

Member
Wouldn't a < cause some of the employees to not be compared with others? I'm not sure I understand.

The problem is, that with != you just remove the (A, A) pairs (duplicates), but you still get both (A, B) and (B, A) with B != A (the reverse pairs). So what you do is, you use < so that the predicate is true for only one of those two (A < B, but not B < A).
 

maeh2k

Member
Thank you guys for your help. I have been fiddling around with what you guys have recommended but unfortunately I still have been unable to produce the correct results.

Here is the full database schema


And this is how I am trying to get it to output

Just took a quick look at it. I haven't done SQL in a while and can't quite remember all keywords and the syntax and features&#8230;

The essential information you want is all in Works_On. There's no natural join with employee, employee, works_on. To get the pairs you need to join works_on with itself.

Select A.ESSN, B.ESSN
From Works_On A, Works_On B
Where A.PNO = B.PNO AND A.ESSN < B.ESSN

That gives you the pairs of people as SSNs. If the same two people work together in multiple projects you can use Select Distinct. (if you want to return the PNO too, you can select it here as well)

Then all that's left to do is join it with the employee table to get the names. It's possible to do this with nested queries, temporary tables, or as one query in which you join employee, works_on, works_on, employee.

WITH Pairs AS (Select A.ESSN AS EA, B.ESSN AS EB
From Works_On A, Works_On B
Where A.PNO = B.PNO AND A.ESSN < B.ESSN)

Select E1.Fname, E1.Lname, E2.Fname, E2.Lname
From Employee As E1, Pairs, Employee E2
Where E1.SSN = Pairs.EA AND Pairs.EB = E2.SSN


That should do it (in theory; haven't tried it and might have some syntax errors); If WITH AS doesn't work with your database that feature might work differently there and the easiest (though less clean) solution is to just do everything in one query.
 
Just took a quick look at it. I haven't done SQL in a while and can't quite remember all keywords and the syntax and features…

The essential information you want is all in Works_On. There's no natural join with employee, employee, works_on. To get the pairs you need to join works_on with itself.

Select A.ESSN, B.ESSN
From Works_On A, Works_On B
Where A.PNO = B.PNO AND A.ESSN < B.ESSN

That gives you the pairs of people as SSNs. If the same two people work together in multiple projects you can use Select Distinct. (if you want to return the PNO too, you can select it here as well)

Then all that's left to do is join it with the employee table to get the names. It's possible to do this with nested queries, temporary tables, or as one query in which you join employee, works_on, works_on, employee.

WITH Pairs AS (Select A.ESSN AS EA, B.ESSN AS EB
From Works_On A, Works_On B
Where A.PNO = B.PNO AND A.ESSN < B.ESSN)

Select E1.Fname, E1.Lname, E2.Fname, E2.Lname
From Employee As E1, Pairs, Employee E2
Where E1.SSN = Pairs.EA AND Pairs.EB = E2.SSN


That should do it (in theory; haven't tried it and might have some syntax errors); If WITH AS doesn't work with your database that feature might work differently there and the easiest (though less clean) solution is to just do everything in one query.

Thank you so much. Thanks to your help I was able to do it.

I used this query to get the desired result.

Select A.pno, e1.fname, e1.lname, e2.fname, e2.lname
From Works_On A, Works_On B, employee e1, employee e2
Where A.PNO = B.PNO AND A.ESSN < B.ESSN AND e1.ssn = a.essn AND e2.ssn = b.essn
ORDER BY PNO, e1.fname, e1.lname, e2.fname, e2.lname


Thank you once again.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
My friend asked me this but I honestly have no idea so I'm forwarding it to you guys.

What's the difference between:
Code:
typedef double (*FUNC)(double);
and
Code:
typedef double (FUNC)(double);

From what I know of function pointers, the former is valid syntax while the latter isn't. But both statements work fine in her code.

Code:
typedef double (*FUNC)(double);

double integrate(FUNC f, double a, double b)
{
     double sum = 0;
     double delta = 0.0001;

     for(double i = a; i<b; i += delta)
     {
          sum = sum + delta*f(i);
     }
     return sum;
}

double line(double x)
{
     return x;
}
double square(double x)
{
     return x*x;
}
double cube(double x)
{
     return x*x*x;
}

int main()
{
     cout<<"The integral of f(x)=x between 1 and 5 is: "<<integrate(line, 1, 5)<<endl;
     cout<<"The integral of f(x)=x^2 between 1 and 5 is: "<<integrate(square, 1, 5)<<endl;
     cout<<"The integral of f(x)=x^3 between 1 and 5 is: "<<integrate(cube, 1, 5)<<endl;
}
 
Just curious, but how would you merge sort a linked list? Not merging two sorted linked list, that's easy as well. I know it's possible after doing a google search but can't find the psuedo code for it, only for for arrays. Doing it on an array is easy, but I've never done it on a linked list before. I remember someone telling me you do it with pointers...
 

Slavik81

Member
My friend asked me this but I honestly have no idea so I'm forwarding it to you guys.

What's the difference between:
Code:
typedef double (*FUNC)(double);
and
Code:
typedef double (FUNC)(double);

From what I know of function pointers, the former is valid syntax while the latter isn't. But both statements work fine in her code.

It's an oddity in the language. To make the syntax simpler, C allows an implicit conversion from functions into function pointers. C also allows function pointers to be invoked without dereferencing.

In short, C mucked around with the normal addressing/dereferencing semantics for functions by adding a lot of implicit behaviours. See also: how does dereferencing of a function pointer happen?
 
Hi guys. I want a user to be able to enter a number with decimals, but I can't seem to get it to work; you can only enter whole numbers. The language I'm using is C#. I've been Googling but I can't find any solutions. This is my code (the text inside the quotes is Swedish but that's not important; it's just asking the user to enter a decimal number):

Code:
            Console.WriteLine("Skriv in ett decimaltal:");
            double x = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Skriv in ett till:");
            double y = Convert.ToDouble(Console.ReadLine());
            double produkt = x * y;

            Console.WriteLine("Produkten av talen är = " + produkt + ".");
            Console.ReadLine();

Thankful for any help!
 

maeh2k

Member
Hi guys. I want a user to be able to enter a number with decimals, but I can't seem to get it to work; you can only enter whole numbers. The language I'm using is C#. I've been Googling but I can't find any solutions. This is my code (the text inside the quotes is Swedish but that's not important; it's just asking the user to enter a decimal number):

Code:
            Console.WriteLine("Skriv in ett decimaltal:");
            double x = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Skriv in ett till:");
            double y = Convert.ToDouble(Console.ReadLine());
            double produkt = x * y;

            Console.WriteLine("Produkten av talen är = " + produkt + ".");
            Console.ReadLine();

Thankful for any help!

I'm not that familiar with the issue and I'm not sure how exactly Convert works, but try using Double.Parse instead.

Edit: looks like Convert should do the same thing. Make sure you enter a valid format. Maybe there are issues with . and ,
Try some of the strings in those examples: http://msdn.microsoft.com/en-us/library/zh1hkw6k(v=vs.110).aspx
 

Ya no

Member
I have a quick question regarding HTML...

I have a VERY basic page built that displays a java applet I made for an intro to java class. I want to embed an mp3 to play in the background while the applet runs. Is it possible to do this if I'm just opening the page on a local machine? I tried putting the mp3 in the same folder as the html file, but I haven't been able to get it to work.

Thanks

EDIT: Figured it out
 
Hi guys. I want a user to be able to enter a number with decimals, but I can't seem to get it to work; you can only enter whole numbers. The language I'm using is C#. I've been Googling but I can't find any solutions. This is my code (the text inside the quotes is Swedish but that's not important; it's just asking the user to enter a decimal number):

Code:
            Console.WriteLine("Skriv in ett decimaltal:");
            double x = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Skriv in ett till:");
            double y = Convert.ToDouble(Console.ReadLine());
            double produkt = x * y;

            Console.WriteLine("Produkten av talen är = " + produkt + ".");
            Console.ReadLine();

Thankful for any help!

Pro-tip when seeking help with code: When you say "it's not working," tell us how. What are your inputs, are you getting errors, what errors, unexpected outputs, etc. Your code will fall over on its face if you provide invalid inputs, certainly, but the happy path "works on my machine" (I have run the code locally as a sanity check). I'd be interested to know if you're getting errors that might reveal cultural, or localization, issues.
 

Atruvius

Member
Hi guys. I want a user to be able to enter a number with decimals, but I can't seem to get it to work; you can only enter whole numbers. The language I'm using is C#. I've been Googling but I can't find any solutions. This is my code (the text inside the quotes is Swedish but that's not important; it's just asking the user to enter a decimal number):

Code:
            Console.WriteLine("Skriv in ett decimaltal:");
            double x = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Skriv in ett till:");
            double y = Convert.ToDouble(Console.ReadLine());
            double produkt = x * y;

            Console.WriteLine("Produkten av talen är = " + produkt + ".");
            Console.ReadLine();

Thankful for any help!

Are you using commas(,) or dots(.) as your decimal mark? Tested your code in VS2010 and it works when using commas.

If you want to use dots instead of commas, check out CultureInfo.
 
Oh wow, you're right. I was using dots instead of commas. Can't believe that was it. Thanks a lot for the help!

Pro-tip when seeking help with code: When you say "it's not working," tell us how. What are your inputs, are you getting errors, what errors, unexpected outputs, etc. Your code will fall over on its face if you provide invalid inputs, certainly, but the happy path "works on my machine" (I have run the code locally as a sanity check). I'd be interested to know if you're getting errors that might reveal cultural, or localization, issues.

You're definitely right, I should've.

It's solved now though; a cultural issue I suppose.
 
i fixed my error from earlier. all i had to do was implement the objects in GameWorld(). smh.
Code:
public class GameWorld implements IObservable, IGameWorld 
{
// create collections class
private Vector<GameObject> GameObjectList; // declare vectors
private Vector<IObserver> ObserverList;

// declare objects and variables
private Tank pTank;
private EnemyTank eTank;
private Missile missile;
private Tree tree;
private Rock rock;
private int locX; 
private int locY;
private Point newCoord;
private int randFixed;
private int time; // time decrement
private int numberOfLives; // number of lives
private int score; // player score
private Scanner userInput; // UserInput to create items
private int n; // get user input for number of objects to create

public GameWorld() // create objects and add them to vector
{
	GameObjectList = new Vector<GameObject>(); // PROBLEM stack overflow error
	ObserverList = new Vector<IObserver>(); // vector to hold observers
	locX = 0 + (int) (Math.random() * ((1+ 1023.0) - 0)); // random from 0 - 1023.0
	locY = 0 + (int) (Math.random() * ((1 + 1023.0) - 0)); // random spawn location
	Point newCoord = new Point(0,0); // to fill in for new location
	randFixed = 0 + (int) (Math.random() * ((1+ 20) - 0));
	newCoord.x = locX; // All GameObjects have a random location of 0-1023.0 for x,y
	newCoord.y = locY;
	time = 200; // 200 seconds per game
	numberOfLives = 3; // number of player lives
	score = 0; // score

}

// .. other code

now i just have to fix my MVC architecture with a mapview/scoreview gui.
 

injurai

Banned
I'm about to start refactoring an entire BASH Shell script project for my job. Haven't yet been trained on the specifics, but I was wondering if there was any good resources to bring me a bit up to speed with bash scripting. Not just some of the syntax but sort of it's functions and capabilities. I don't plan to get super deep, but just enough to feel comfortable.
 

Jokab

Member
Just finished implementing a doubly-linked queue in C, as my second program using C (the first one was translating back and forth between morse code and text). It passes all the professor's tests in the accompanied test file, so it should be all good. Feeling pretty proud of myself. Up until 1½ weeks ago, I had worked in Java exclusively for little over a year (and before that no programming experience), so working with pointers is very foreign and took some time getting used to. The code is probably horrible as far as conventions go, but whatever.

Link if anyone is interested. I suppose there are some specifics of that implementation that aren't very common (perhaps specified that way to simpify things). Don't blame me for the comments in Swedish, they are just copied from the professor's definition file. :)
 

Boken

Banned
So I want to get into the big world of coding.

I did introductory C a while ago. It was easy enough.

My current goal is to make a website that can accept user input through drop boxes etc, make calculations on the inputs based on a database of information and output some answers.

then I want to store the user input in the database so i can improve the calculation algorithms.

What languages should I look at for doing this? - Javascript, HTML and PHP?
 

Boken

Banned
yeah, that too.

so this sounds like a really big project...

is there any way i can break it down to make it easier to learn?
e.g. start with this language, or do x component of the project
 
yeah, that too.

so this sounds like a really big project...

is there any way i can break it down to make it easier to learn?
e.g. start with this language, or do x component of the project

I recommend learning it in layers...

Get the basic HTML/CSS down. Then, work on the client side Javascript code. Then, when you want to implement the server side you can install Apache, or nginx, or Node, or whatever you want as well as a database engine.

That way, you can have prototypes up and working at each point. You can have most of that functionality with just Javascript and HTML -- everything except retrieving/storing the info in a database.

As for a language for the server side, it's up to you. There's a lot of choices. Personally, I would recommend Python using Flask as your framework. If you would prefer PHP, you can certainly go that route. Or if you want to minimize how many new languages you need to learn, you could use Node so that you can write Javascript on your client and server sides.
 
algorithms exam in an hour, blargh. i'm fine with the logic and implementation, but being a high-school drop out the maths (here summation, recurrence relations and back-substitution) is not my strong point. i can generally figure out logically what the big o of something is going to be but expressing it with the correct formula is rough. probably should've studied more these last few days and spent less time trying to make a game.
 

hateradio

The Most Dangerous Yes Man
There is now a follow-up course to the Principles of Functional Programming course on Coursera (the Scala course by Martin Odersky) which just started today: https://www.coursera.org/course/reactive
I watched the first few lecture videos today and I quite enjoyed them. Monads make a lot more sense to me now.
I'll be taking this, too.

I really liked the previous course, and I didn't even know much about functional programing.
 

dalVlatko

Member
I'm in a basic C++ class and I've been doing my work on a Mac with Sublime Text and the Terminal as my compiler but ever since I updated to Mavericks, the way the errors are displayed has changed.

Apparently the compiler has changed with the update to Mavericks but it seems like downgrade to me. It just shows too much info and it was easier for me to find errors with the old compiler.

Is there an alternative or a way to get the old compiler back?
 

injurai

Banned
I'm in a basic C++ class and I've been doing my work on a Mac with Sublime Text and the Terminal as my compiler but ever since I updated to Mavericks, the way the errors are displayed has changed.

Apparently the compiler has changed with the update to Mavericks but it seems like downgrade to me. It just shows too much info and it was easier for me to find errors with the old compiler.

Is there an alternative or a way to get the old compiler back?

can't you link the terminal command with a different target? I would think the binaries would still be there for the old version. Is this gcc?
 
I'm in a basic C++ class and I've been doing my work on a Mac with Sublime Text and the Terminal as my compiler but ever since I updated to Mavericks, the way the errors are displayed has changed.

Apparently the compiler has changed with the update to Mavericks but it seems like downgrade to me. It just shows too much info and it was easier for me to find errors with the old compiler.

Is there an alternative or a way to get the old compiler back?

Why not use xcode?
 

Onemic

Member
Anyone know how to approach coding an exponential equation, while only using the library functions within stdio.h?

I need to calculate the future value of a specified amount of money:

future value = principal * ( 1 + rate ) ^ (number of periods)
 

Magni

Member
There is now a follow-up course to the Principles of Functional Programming course on Coursera (the Scala course by Martin Odersky) which just started today: https://www.coursera.org/course/reactive
I watched the first few lecture videos today and I quite enjoyed them. Monads make a lot more sense to me now.

I got an email for this from Coursera a few days ago, but unfortunately I'm super busy right now and will be until at the very earliest February.. shame as I really enjoyed taking the first one, hopefully there'll be a new session in the Spring.
 
Anyone know how to approach coding an exponential equation, while only using the library functions within stdio.h?

I need to calculate the future value of a specified amount of money:

future value = principal * ( 1 + rate ) ^ (number of periods)

So you can't use math.h? Then I would just write a simple exponential function. Something like this should do the trick. In Python, I'll let you translate it to C so I don't do all the work for you. =)

Code:
def exp(x, n):
    num = 1
    for i in range(n):
        num *= x
    return num

Just pass in your (1+rate) and periods into that function, then multiply the returned val by principal.
 

TUSR

Banned
Does anyone known of a decent polymorphism tutorial for C++? I'm having difficult with inheritance and virtual/pure virtual functions.
 

Onemic

Member
So you can't use math.h? Then I would just write a simple exponential function. Something like this should do the trick. In Python, I'll let you translate it to C so I don't do all the work for you. =)

Code:
def exp(x, n):
    num = 1
    for i in range(n):
        num *= x
    return num

Just pass in your (1+rate) and periods into that function, then multiply the returned val by principal.

thanks
 

GabDX

Banned
I need help with C++. The header of a class I'm defining looks like this (I only kept the relevant parts if the following code):

Code:
#ifndef TOOLS_HPP
#define TOOLS_HPP

#include <complex>

using namespace std;

class tools {
    public:
};

#endif

This is the file where I'm actually defining the functions declared in the header:

Code:
#include "tools.hpp"

const complex<double> I(0, 1);
const double PI = atan(1)*4;

When I try to compile, I get these errors:

Code:
tools.cpp|3 col 25| error: expected identifier before numeric constant
tools.cpp|3 col 25| error: expected ',' or '...' before numeric constant
tools.cpp|4 col 19| error: 'std::atan' cannot appear in a constant-expression
tools.cpp|4 col 25| error: a function call cannot appear in a constant-expression
tools.cpp|4 col 27| error: ISO C++ forbids initialization of member 'PI' [-fpermissive]
tools.cpp|4 col 27| error: making 'PI' static [-fpermissive]
etc...

The weird thing is that it use to compile properly. I then made a few changes that are, as far as I know, unrelated to those errors and now it won't compile. What's going on?
 
Top Bottom