• 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

Ziltoid

Unconfirmed Member
Now that you mention it, most things you can write in VHDL would turn into a very small amount of logic gates and wires after optimisation, or at least a constant and predictable amount of them.
Yep, you kinda havo to have a different mindset when writing VHDL/Verilog code :p.

It seems like the output buffer gets overridden or something when I try to get each thread to print out its "primes" vector. I've also tried making the "primes" vector static so that it would be shared between all the threads and then just show that one vector, but it looks even weirder. Any ideas?
I don't know any Java, but do you use any guards or mutexes when accessing the buffer? Threads accessing the same memory location simultaneously is a no-no.
 
They should be identical. Post your code.

Not near my computer now, but I know it has to do with the initialization of the vector vs the initialization of the array.

A very unscientific test to see the difference between the two and the cost, if noticeable, to dynamically resize the vector, which there should be vs a normal fixed sized array. I'd done it mathematically in my intro to algorithms class, and I'd never actually done it practically.
 
Just for fun I'm writing a quick multi-threaded java program to benchmark your cpu by computing all the prime numbers up to 200,000. The only problem occurs at the very end of the program where I try to show the prime numbers that were computed.

If you take out that very last for-loop inside the main method then the output shows normal. (shows the computation time but just doesn't show any of the prime numbers)

It seems like the output buffer gets overridden or something when I try to get each thread to print out its "primes" vector. I've also tried making the "primes" vector static so that it would be shared between all the threads and then just show that one vector, but it looks even weirder. Any ideas?

The System.out.println calls are not thread safe/atomic. The easiest way to solve this would be to create a static utility method that is synchronized and have calls to that be made.

Edit: You could also just put them into a string buffer (or is StringBuilder the thread safe one) shared between threads and print it when all threads join.

Edit2: I think I read your question wrong, ha. That's what I get for skimming.
 

Celcius

°Temp. member
The System.out.println calls are not thread safe/atomic. The easiest way to solve this would be to create a static utility method that is synchronized and have calls to that be made.

Edit: You could also just put them into a string buffer (or is StringBuilder the thread safe one) shared between threads and print it when all threads join.

Edit2: I think I read your question wrong, ha. That's what I get for skimming.

lol, no problem
 

KageZero

Member
Hello, i need a bit of help.
I started working in javascript and html (finished all courses on codeacademy and through 1 book) but now i would like to continue with it but i'm not sure where to start from since there are too many resources available and i got lost in that. Can someone point me what programs to use for coding in javascript/html5(ide), and if possible to give ne some advices what should i do next.
 
Hello, i need a bit of help.
I started working in javascript and html (finished all courses on codeacademy and through 1 book) but now i would like to continue with it but i'm not sure where to start from since there are too many resources available and i got lost in that. Can someone point me what programs to use for coding in javascript/html5(ide), and if possible to give ne some advices what should i do next.

I wouldn't suggest starting off with an IDE. Use Notepad++ to code in, and use w3schools as your main resource. Start by putting a simple webpage together, and extend it with Javascript.

You'll need to setup Apache on your machine as well in order to run your website.
 

Celcius

°Temp. member
What does the error look like?

At first the output console looks like:

Your computer has 8 processors. How many would you like to use? 8
splitThread_0 created.
splitThread_1 created.
splitThread_2 created.
splitThread_3 created.
splitThread_4 created.
splitThread_5 created.
splitThread_6 created.
splitThread_7 created.
splitThread_0 is calculating primes from 1 to 25000
splitThread_1 is calculating primes from 25001 to 50000
splitThread_2 is calculating primes from 50001 to 75000
splitThread_4 is calculating primes from 100001 to 125000
splitThread_6 is calculating primes from 150001 to 175000
splitThread_7 is calculating primes from 175001 to 200000
splitThread_5 is calculating primes from 125001 to 150000
splitThread_3 is calculating primes from 75001 to 100000
That took 0 minute(s) and 25 second(s).
The prime numbers computed were:

Then it gets to that last for-loop in the main method, wipes out the console, and puts:

Thread-2 computed: *long list of numbers* (some digits are green for some reason)
Thread-2 computed: *long list of numbers*
Thread-3 computed: *long list of numbers*
Thread-4 computed: *long list of numbers*
Thread-5 computed: *long list of numbers*

Not only are the thread names different than what gets shown from the run() method (splitThread_2 vs Thread-2) but also the strange thing is that I'm just printing values from a Vector (basically a thread-safe arrayList) in a for-loop at the end after the threads have finished.
 

usea

Member
celsius, it looks like you're just printing a vector of integers. I don't think those print nicely by default, do they? You'll have to join all the elements into a string or something.

Code:
System.out.println(splitThreads[i].getName()+" computed: "+splitThreads[i].primes);
primes is a Vector<int>. So you're calling Vector's toString(), which I think just prints some info about the object in memory, not the actual values contained in it right?
 

Celcius

°Temp. member
hmm, I tried doing:

Code:
System.out.println("The prime numbers computed were:"); //display the prime numbers computed
			String primes0="";
			for(int i=0; i<splitThreads[0].primes.size(); i++)
			{
				primes0+=splitThreads[0].primes.get(i)+" ";
			}
			String primes1="";
			for(int i=0; i<splitThreads[1].primes.size(); i++)
			{
				primes1+=splitThreads[1].primes.get(i)+" ";
			}
			String primes2="";
			for(int i=0; i<splitThreads[2].primes.size(); i++)
			{
				primes2+=splitThreads[2].primes.get(i)+" ";
			}
			String primes3="";
			for(int i=0; i<splitThreads[3].primes.size(); i++)
			{
				primes3+=splitThreads[3].primes.get(i)+" ";
			}
			String primes4="";
			for(int i=0; i<splitThreads[4].primes.size(); i++)
			{
				primes4+=splitThreads[4].primes.get(i)+" ";
			}
			String primes5="";
			for(int i=0; i<splitThreads[5].primes.size(); i++)
			{
				primes5+=splitThreads[5].primes.get(i)+" ";
			}
			String primes6="";
			for(int i=0; i<splitThreads[6].primes.size(); i++)
			{
				primes6+=splitThreads[6].primes.get(i)+" ";
			}
			String primes7="";
			for(int i=0; i<splitThreads[7].primes.size(); i++)
			{
				primes7+=splitThreads[7].primes.get(i)+" ";
			}
			
			System.out.println("The prime numbers discovered were:"+primes0+"\n"+primes1+"\n"+primes2+"\n"+primes3+"\n"+primes4+"\n"+primes5+"\n"+primes6+"\n"+primes7);

but it still wipes out the console and then puts numbers with some digits in green.
 
At first the output console looks like:

Not only are the thread names different than what gets shown from the run() method (splitThread_2 vs Thread-2) but also the strange thing is that I'm just printing values from a Vector (basically a thread-safe arrayList) in a for-loop at the end after the threads have finished.

Looking more closely.

One thing I noticed is that inside your split thread class you are creating another thread, t2. Not sure what for?

splitThread is your thread, so you want to use this.setName rather than t2.

Also, in your last for loop in main you have numThreads-1 instead of numThreads. You won't get all threads like that I don't think.
 

KageZero

Member
I wouldn't suggest starting off with an IDE. Use Notepad++ to code in, and use w3schools as your main resource. Start by putting a simple webpage together, and extend it with Javascript.

You'll need to setup Apache on your machine as well in order to run your website.

I did that already... Made a web site, added some javascrpit to it(calendar,timetables,forms etc) animated it with jquery and even did some php but now i would like to move on and start doing some applications using html5/javascript
 

Celcius

°Temp. member
Looking more closely.

One thing I noticed is that inside your split thread class you are creating another thread, t2. Not sure what for?

splitThread is your thread, so you want to use this.setName rather than t2.

Also, in your last for loop in main you have numThreads-1 instead of numThreads. You won't get all threads like that I don't think.

Thanks, I fixed both issues and now the output looks like:

splitThread_3 computed: *long list of numbers* some digits in green
splitThread_4 computed: *long list of numbers*
splitThread_5 computed: *long list of numbers*
splitThread_6 computed: *long list of numbers*
splitThread_7 computed: *long list of numbers*
 
Thanks, I fixed both issues and now the output looks like:

splitThread_3 computed: *long list of numbers* some digits in green
splitThread_4 computed: *long list of numbers*
splitThread_5 computed: *long list of numbers*
splitThread_6 computed: *long list of numbers*
splitThread_7 computed: *long list of numbers*

I tried doing this...

Code:
System.out.println("The prime numbers computed were:"); //display the prime numbers computed
			StringBuffer stringBuffer = new StringBuffer();
			for(int i=0; i<numThreads; i++)
			{
				stringBuffer.append(splitThreads[i].getName()+" computed: "+splitThreads[i].primes + "\n");
			}
			System.out.println(stringBuffer.toString());

... and I get the same strange result. At first I thought it might be because your BufferedReader was still open, but I closed it before the outs and still no dice. Could be a quirk in vector.toString().
 

Celcius

°Temp. member
hmm, check this out:

Code:
//Multi-threaded edition of benchmark program that calculates all the prime numbers less than 200,000
//a prime number is only divisible by 1 and itself
import java.util.Vector;
import java.io.*;
public class PrimesMT
{
	public static void main(String[] args)
	{
		int cpuCores = Runtime.getRuntime().availableProcessors(); //determine how many processors are installed
		try
		{
			int numThreads=0; //the number of threads to use
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			do
			{
				System.out.print("Your computer has "+cpuCores+" processors. How many would you like to use? ");
				numThreads = Integer.parseInt(br.readLine()); //get the user's input
			}while((numThreads<1)||(numThreads>cpuCores));
			
			long startTime = System.currentTimeMillis(); //the timestamp when the calculation started
			
			splitThread[] splitThreads = new splitThread[numThreads];
			for (int h = 0; h < numThreads; h++) // create the threads
			{ 
				splitThreads[h] = new splitThread(h, numThreads);
			}
			for(int i = 0; i < numThreads; i++) // start the threads
			{
				splitThreads[i].start();
			}
			for(int i = 0; i < numThreads; i++) //halt main program until all threads finish calculations
			{
				try
				{
					splitThreads[i].join();
				}
				catch(Exception e)
				{
					System.out.println(e);
				}
			}
			long stopTime = System.currentTimeMillis(); //the timestamp when the calculation stopped
			long elapsed = ((stopTime - startTime) / 1000); //how long in seconds the calculation took
			if (elapsed == 0) //if it took less than one second
			{
				elapsed = (stopTime - startTime); //how long in milliseconds the calculation took
				System.out.println("That took ."+elapsed+" seconds.");
			}
			else //if it took longer than one second
			{
				int minutes = (int)elapsed / 60; //the minutes part of time elapsed
				int seconds = (int)elapsed % 60; //the seconds part of time elapsed
				System.out.println("That took "+minutes+" minute(s) and "+seconds+" second(s).");
			}
			
			System.out.println("The prime numbers computed were:"); //display the prime numbers computed
			for(int i=2; i<numThreads; i++)
			{
				System.out.println(splitThreads[i].getName()+" computed: "+splitThreads[i].primes.toString());
			}
			
			br.close();
		}
		catch(IOException ioe)
		{
			System.exit(0);
		}
	}
}

class splitThread extends Thread //this is the code for the threads to execute
{ 
	int iterations = 200000; //calculate all the primes up to this number
	double iden; //thread id
	double numbThreads; //total number of threads to use
	Vector<Integer> primes = new Vector<Integer>(); //arraylist to hold the discovered primes
	splitThread(int id, int nThreads) // id number, number of threads that will be created
	{
		iden = id;
		numbThreads = nThreads;
		this.setName("splitThread_"+Integer.toString(id));
		System.out.println(this.getName() + " created.");
	} 
	public void run() // This is the entry point for the splitThreads. 
	{ 
		try
		{
			int minRange = (int)((iden/numbThreads)*200000)+1; //this thread should check for primes starting with this number
			int maxRange = (int)(((iden+1)/numbThreads)*200000); //this thread should check for primes stopping with this number
			boolean divByOthers = false; //if the number is divisible by something other than 1 or itself
			System.out.println(this.getName()+" is calculating primes from "+minRange+" to "+maxRange);//debug statement
			for(int i=minRange; i<maxRange; i++) //check for primes
			{
				for(int y=(i-1); y>1; y--)
				{
					if(i%y==0)
						divByOthers=true;
				}
				if(divByOthers==false)
				{
					if(i!=1)
						primes.add(i);
				}
				divByOthers=false;
			}
		}
		catch (Exception e)//InterruptedException e)
		{ 
			System.out.println("splitThread "+this.getName()+" interrupted."); 
		}  
	}
}
All I did was change that last for-loop in the main method to start from index 2 instead of 0 and now instead of wiping out the console it shows:

Your computer has 8 processors. How many would you like to use? 8
splitThread_0 created.
splitThread_1 created.
splitThread_2 created.
splitThread_3 created.
splitThread_4 created.
splitThread_5 created.
splitThread_6 created.
splitThread_7 created.
splitThread_0 is calculating primes from 1 to 25000
splitThread_1 is calculating primes from 25001 to 50000
splitThread_2 is calculating primes from 50001 to 75000
splitThread_4 is calculating primes from 100001 to 125000
splitThread_3 is calculating primes from 75001 to 100000
splitThread_6 is calculating primes from 150001 to 175000
splitThread_5 is calculating primes from 125001 to 150000
splitThread_7 is calculating primes from 175001 to 200000
That took 0 minute(s) and 25 second(s).
The prime numbers computed were:
splitThread_2 computed: *numbers*
splitThread_3 computed: *numbers*
splitThread_4 computed: *numbers*
splitThread_5 computed: *numbers*
splitThread_6 computed: *numbers*
splitThread_7 computed: *numbers*

This means that for some reason there's something wrong with the thread 0's and thread 1's "primes" vector. I can run the program fine if the last for-loop starts at index 2 or greater (not 0 or 1).

edit: I can call .size() on any of the thread's primes vector, but if I try to print thread 0 or 1's vector then it wipes the console and skips their values...
 
Can anyone here recommend a 3rd party library for playing movies in their C++ apps? Something like .mpeg, wmvs, etc? I guess something like Bink but free. I'm currently looking into ffmpeg or vlc.

Ultimately what I'd like to do is to place a video on a texture and render it in a 3d environment.

edit:
Doing Win64 development fwiw
 

Lathentar

Looking for Pants
Thanks, Lathentar. I'm taking all your suggestions and cleaning up my code.



1) Because I'm passing built in types (strings, ints), I'm passing the values. But if I were to pass an entire Node, I'd use a reference. Right?
2&3) I've since changed my code to look like this:

Code:
//in Node:
public:
	Node(string const name, int const id, int const x_cord, int const y_cord) {
		_name = name;
		_id = id;
		_x_cord = x_cord;
		_y_cord = y_cord;
	}

//in main:
Node* ptr = new Node(name, id, x_cord, y_cord);

Thanks again.
A string is not what I meant by built in type. I meant primitive built in types: http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm

String is actually an array of characters, when it is passed by value the entire character array can be copied into another string. This is wasteful. So pass it by const reference.

Also, don't be afraid to use the initializer list in constructors.
 

cyborg009

Banned
Oh geez I'm having a bit of issues with this code in Java. The program I'm trying to make is to buy movie tickets online. My teacher is making us do this using GUI so I'm having a little trouble with it.

This part of the code (SelectingWindow)is just the main page showing only the administer button, user button and the returning user. I wanted to use inheritance and use it in the action listener but I'm not too sure how.
Code:
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class SelectUserWindow extends JFrame{
	
	
	private JButton adminButton, userButton;
	private JPanel panel;
	private JLabel messageLabel;
	private JTextField referenceNum;
	private final int WINDOW_WIDTH = 400, WINDOW_HEIGHT = 200;
	

	
public SelectUserWindow(){
	super("Welcome to SJI movies");
	
	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	
	setLayout(new GridLayout(2,3));
	
	
	
	buildPanel();
	
	
	
	setVisible(true);
	
}

private void buildPanel(){
	messageLabel = new JLabel("Returning User?");
	referenceNum = new JTextField(8);
	
	
	adminButton = new JButton("administer");
	userButton = new JButton("User");
	
	//action listener 
	
	userButton.addActionListener(new UserButton());
	
	
	
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel3 = new JPanel();
	JPanel panel4 = new JPanel();
	JPanel panel5 = new JPanel();
	JPanel panel6 = new JPanel();
	
	add(panel1);
	add(panel2);
	add(panel3);
	add(panel4);
	add(panel5);
	add(panel6);
	
	panel1.add(userButton);
	panel3.add(adminButton);
	panel5.add(messageLabel);
	panel5.add(referenceNum);
	
	
	
	
	
	
	
}// end of build panel

private class UserButton implements ActionListener{
	public void actionPerformed (ActioEvent e)
	{
		
	}
}



}

second is just the movie selection with the names,rating,prices, and times initialize in the array.
Code:
import java.util.Arrays;


public class MovieSelection {
private String[] movieName = {"RED","Taken","Star Trek","Star Wars","Avatar"};
private String[] movieRating = {"G","PG","PG-13","R","NC-17"};
private String[][] movieTime = {{"8:00 AM","11:00 AM","3:15 PM"},{"7:35 AM","12:45 PM","4:30 PM"},{"8:45 AM","12:00 PM","5:00 PM"}
,{"10:00 AM","6:15 PM","7:45 PM"},{"11:30 AM","7:00 PM","8:00 PM"}};

private double[] moviePrice = {7.35,5.95,7.00,10.00,8.45};

public MovieSelection() {
	
	// TODO Auto-generated constructor stub
}

public MovieSelection(String[] movieName, String[] movieRating,
		String[][] movieTime, double[] moviePrice) {
	super();
	this.movieName = movieName;
	this.movieRating = movieRating;
	this.movieTime = movieTime;
	this.moviePrice = moviePrice;
}

public String[] getMovieName() {
	return movieName;
}

public void setMovieName(String[] movieName) {
	this.movieName = movieName;
}

public String[] getMovieRating() {
	return movieRating;
}

public void setMovieRating(String[] movieRating) {
	this.movieRating = movieRating;
}

public String[][] getMovieTime() {
	return movieTime;
}

public void setMovieTime(String[][] movieTime) {
	this.movieTime = movieTime;
}

public double[] getMoviePrice() {
	return moviePrice;
}

public void setMoviePrice(double[] moviePrice) {
	this.moviePrice = moviePrice;
}

@Override
public String toString() {
	return "MovieSelection [movieName=" + Arrays.toString(movieName)
			+ ", movieRating=" + Arrays.toString(movieRating) + ", movieTime="
			+ Arrays.toString(movieTime) + ", moviePrice="
			+ Arrays.toString(moviePrice) + "]";
}



}

So basically I just wanted to press the user button and a new window showing movies info.
 

cyborg009

Banned
What do you need help with? Handling the event?

Code:
private class UserButton implements ActionListener{
	public void actionPerformed (ActionEvent e)
	{
		if(e.getSource() == userButton) {
                       // open a new window and fill it
                }
	}
}

Yep I want to use the MoviesSelection Class but I'm not even too sure how to do that properly .
 

luoapp

Member
JAVA PrimesMT ...
.

Your code runs just fine on my MAC

Code:
Your computer has 8 processors. How many would you like to use? splitThread_0 created.
splitThread_1 created.
splitThread_2 created.
splitThread_3 created.
splitThread_4 created.
splitThread_5 created.
splitThread_6 created.
splitThread_7 created.
splitThread_0 is calculating primes from 1 to 25000
splitThread_1 is calculating primes from 25001 to 50000
splitThread_2 is calculating primes from 50001 to 75000
splitThread_3 is calculating primes from 75001 to 100000
splitThread_4 is calculating primes from 100001 to 125000
splitThread_5 is calculating primes from 125001 to 150000
splitThread_6 is calculating primes from 150001 to 175000
splitThread_7 is calculating primes from 175001 to 200000
That took 0 minute(s) and 16 second(s).
The prime numbers computed were:
Thread-0 computed: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 
...

Here is what I got run java -version:
Code:
java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-9M3425)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-384, mixed mode)
 

cyborg009

Banned
I actually got the new window to appear but I' m trying to use arrays to get rid of excess but it proving to be a bit of trouble. I'll post a little later since going be doing other stuff.
 

Mangotron

Member
So theoretically this should be pretty easy, but I can't seem to get it to work. (C++ btw).

I've got 5 arrays that look like
Code:
double invoiceAmount[MAX] = {40.75, 2433.00, 1367.50, 53.25, 13.75,
      2312.20, 25.67, 26.75, 2115.81, 23.50};

And I'm trying to print them vertically in 5 columns side-by-side like:
Code:
1   1   1   1   1
2   2   2   2   2 
3   3   3   3   3

I can get them to print out vertically using:
Code:
 for (int index = 0; index < MAX; index++)
	       cout << right << invoiceNumber[index] << endl;

but they're all on top of each other instead of side by side (I'm assuming because of the endl;) I have to use another "for" loop to control all the "fors" that are inside of it, but I'm a little lost as how to print them out so they're still vertical as well as side by side.
 

leroidys

Member
So theoretically this should be pretty easy, but I can't seem to get it to work. (C++ btw).

I've got 5 arrays that look like
Code:
double invoiceAmount[MAX] = {40.75, 2433.00, 1367.50, 53.25, 13.75,
      2312.20, 25.67, 26.75, 2115.81, 23.50};

And I'm trying to print them vertically in 5 columns side-by-side like:
Code:
1   1   1   1   1
2   2   2   2   2 
3   3   3   3   3

I can get them to print out vertically using:
Code:
 for (int index = 0; index < MAX; index++)
	       cout << right << invoiceNumber[index] << endl;

but they're all on top of each other instead of side by side (I'm assuming because of the endl;) I have to use another "for" loop to control all the "fors" that are inside of it, but I'm a little lost as how to print them out so they're still vertical as well as side by side.

try printing out the first element in every array, make a new line, print out second element in each array, etc... you may want to pad out the arrays with nulls if theyre not all the same length
 

Splatt

Member
I'm interested in opionions... which is a better language for OOP introduction? C++ or Java?

I've studied both at my college, but I am of an opinion that we should have first started with C++ instead of Java (especially since we had regular C in the first year).
 
This has been bugging me for the last two days, and it'd be great if someone here could help. In a course I'm taking at school we're supposed to program a Nexys 3 FPGA, and among other things we're going to have it output VGA resolution at 60Hz. We need to program every individual signal ourselves and get the timing right, and I keep getting what I think is synchronization problems. I have tested it on three LCD monitors; on the first and oldest I only get one row at the top and the rest is pitch black, and on the other two I've gotten the whole image, but it's shifted to the right and starts coming around on the left side. The numbers we're using are in the image below, taken from the card's manual (page 17).

XZLZDfC.png

This is the relevant code:

Code:
process(clk) begin --VGA
	if rising_edge(clk) then
		pixelctr <= pixelctr + 1;
		if pixelctr = 0 then --FPGA is running at 4x pixel clock speed, this "slows it down"
			if rst = '1' then --rst is a button on the card being used for reset
				xctr <= "0000000000";
			elsif xctr = 799 then
				xctr <= "0000000000";
			else
				xctr <= xctr + 1;
			end if;
			
			if rst = '1' then
				yctr <= "0000000000";
			elsif xctr = 799 then
				if yctr = 520 then
					yctr <= "0000000000";
				else
					yctr <= yctr + 1;
				end if;
			end if;
			
			if xctr < 640 and yctr < 480 then
				red <= pixel(7 downto 5);
				green <= pixel(4 downto 2);
				blue <= pixel (1 downto 0);
			else
				red <= "000";
				green <= "000";
				blue <= "00";
			end if;
			
			if xctr = 687 then
				hs <= '1';
			elsif xctr = 783 then
				hs <= '0';
			end if;
			
			if yctr = 508 then
				vs <= '1';
			elsif yctr = 510 then
				vs <= '0';
			end if;				
		end if; --pixelctr
	end if; --clk
end process;

Any ideas of what I might be doing wrong?

Also, just a quick note. VHDL is different from other languages in that everything is done simultaneously, as it's describing how the hardware should be wired. So all the different if-statements are executed simultaneously.
 

Aleph

Member
I'm interested in opionions... which is a better language for OOP introduction? C++ or Java?

I've studied both at my college, but I am of an opinion that we should have first started with C++ instead of Java (especially since we had regular C in the first year).

At my university we used C when we learnt imperative programming, and then Java for OOP (like you). I've been learning C++ on my own, and so far Java seems like a more "pure" OO language than C++ (although nothing compared to Smalltalk, which we also used). One example of this would be the root Object class in Java (which does not exist in C++).
 

Godslay

Banned
I'm interested in opionions... which is a better language for OOP introduction? C++ or Java?

I've studied both at my college, but I am of an opinion that we should have first started with C++ instead of Java (especially since we had regular C in the first year).

C#, just my personal opinion.
 
I just hate the stupid coding conventions of this language such as capitalizing the first letter of method names and curly braces on newlines

You be starting something there mate :p If you are coding in Java you put your curly braces on a new line like God/Gosling intended.

But I'll fight with you on the capitalization thing.

Either way, when learning OOP there isn't much difference.
 
You be starting something there mate :p

If you are coding in Java you put your curly braces on a new line like God/Gosling intended.

99i1t4G.jpg


I use both styles. If I'm using vim it's
Code:
function(){
}

In like xcode or visual studio

Code:
function()
{
}

And I have no idea why I end up doing it that way. *shrug*

Also capitalization of first letter for objects only, goddamnit.
 
It would be nice if every language (or rather, the communities of every language) had the same naming conventions and language patterns where applicable (obviously most applicable if talking about C-style languages with braces, for example), so that when you switched between languages, your perhaps most comfortable language's idioms don't slip through and make everybody else agitated.

Unfortunately, we do not seem to live in that world.
 

Godslay

Banned
image.php


C#? Really?
Sorry, I just hate the stupid coding conventions of this language such as capitalizing the first letter of method names and curly braces on newlines, just to differentiate from Java.

He asked for an OOP language. C# is relevant, has a big community (meaning plenty of help and examples, same for Java though), plus you get to use God's IDE with the beauty that is Entity Framework/Linq. I love Java as well, but C# is my darling.

Curly braces should belong on a newline, as it makes the code cleaner imo. As for annoyances, Java makes you spell it out way too often. class foo extends foobar implements whatever, too verbose. C# is guilty of this at times, but Java seems worse.
 
He asked for an OOP language. C# is relevant, has a big community (meaning plenty of help and examples, same for Java though), plus you get to use God's IDE with the beauty that is Entity Framework/Linq. I love Java as well, but C# is my darling.

Curly braces should belong on a newline, as it makes the code cleaner imo. As for annoyances, Java makes you spell it out way too often. class foo extends foobar implements whatever, too verbose. C# is guilty of this at times, but Java seems worse.

Which is fine, but for learning I don't see that as a negative.
 

Chris R

Member
Why all the talk of braces when we can be arguing about the bigger problem in the community? I'm of course talking about the heathens who still use spaces to indent their code.
 

Godslay

Banned
Which is fine, but for learning I don't see that as a negative.

Yeah, it's not. C# or Java is a good place to start. Both have things that are pains, but they are both good languages. I just like C# more, likely because I work with it on a daily basis, and know it more intimately. Can't go wrong with either one.
 
Ok, we're working on linked lists in class right now and I am running into a problem I can't fix.
In function "takeInfo," nTime and take don't get the values copied to them. What am I missing?

Code:
// Minor 10
// Spring 2013

#include "minor10.h"

struct info startList( char *readt, char *readn );
void addInfo( struct info **head, char *readt, char *readn );
void newList( struct info **head, char *readt, char *readn );
char takeInfo( struct info **head, char *nTime );


int main( void )
{
	char time[5];
	char nTime[5];
	char name[15];
	char command[7];
	struct info *head;

	scanf( "%s", time );
	scanf( "%s", command );		// Understood enqueue for
	scanf( "%s", name );		// first command
	head = (struct info*)malloc(sizeof(struct info));
	*head = startList( time, name );

	printf( "%s entered the Queue at %s\n", name, time );

	while( scanf( "%s", time ) != EOF )
	{
		scanf( "%s", command );
		if( strcmp( command, "enqueue" ) == 0 )
		{
			scanf( "%s", name );
			newList( &head, time, name );
			printf( "%s entered the Queue at %s\n", name, time );
		}
		else if( strcmp( command, "dequeue" ) == 0 )
		{
			*nTime = takeInfo( &head, nTime );
			printf( "%s, who entered the Queue at %s", name, nTime );
			printf( ", dequeued, receiving service at %s\n", time );
		}
	}

	return 0;
}

struct info startList( char *readt, char *readn )
{
	struct info *head;
	head = malloc(sizeof(struct info));
	head->time = readt;
	head->name = readn;
	head->next = NULL;

	return *head;
}

void newList(struct info **head, char *readt, char *readn )
{
	struct info* current;
	current = *head;
	while( current != NULL )
		current= current->next;

	addInfo( &current, readt, readn );
}

void addInfo( struct info **head, char *readt, char *readn )
{
	struct info* newInfo = malloc(sizeof(struct info));

	newInfo->time = readt;
	newInfo->name = readn;
	newInfo->next = head;
	*head = newInfo;
}

char takeInfo( struct info **head, char *nTime )
{
	struct info* take;
	
	*take = **head;
	nTime = take->time;
	take = take->next;
	
	return *nTime;
}

Also, any general tips for anything else would be helpful :)
 

cyborg009

Banned
Ugh I hate dealing with GUI. Should I just switch out the arrays for an arrayList? I'm ruined my code every passing moment. I wanted to Display the stuff in the initialized array as a message label.

Code:
import java.awt.GridLayout;
import java.util.Arrays;
import java.awt.event.*;
import javax.swing.*;



public class MovieSelection  extends JFrame{

private String[] movieName = {"RED","Taken","Star Trek","Star Wars","Avatar"};
private String[] movieRating = {"G","PG","PG-13","R","NC-17"};
private String[][] movieTime = {{"8:00 AM","11:00 AM","3:15 PM"},{"7:35 AM","12:45 PM","4:30 PM"},{"8:45 AM","12:00 PM","5:00 PM"}
,{"10:00 AM","6:15 PM","7:45 PM"},{"11:30 AM","7:00 PM","8:00 PM"}};

private double[] moviePrice = {7.35,5.95,7.00,10.00,8.45};

public MovieSelection() {
	super("Please select your movie");
	setSize(800,400);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setLayout(new GridLayout(15,4));
	
	
	
	
	setVisible(true);
	
	// TODO Auto-generated constructor stub
}
private void buildPanel(){
	
	
}



}
 

hateradio

The Most Dangerous Yes Man
Well, since it went there.

Personal way to write stuff:
Code:
// For most C derivatives, I like a space between the name and the parentheses.
function a () {
}

someMethodB ()
{
}

// Scala, braces on the same line if braces are needed
def func = {
}

// otherwise one line
def func = ???

// Ruby, braces whut?
def your_mom!
end
 
image.php


C#? Really?
Sorry, I just hate the stupid coding conventions of this language such as capitalizing the first letter of method names and curly braces on newlines, just to differentiate from Java.
C# is better than Java.

The comparison is easy to make, since they're almost the same language. But C# has (off the top of my head):

1) a nod at RAII, with the "using" keyword
2) that cool accessors construct
3) no reliance on Oracle to produce a runtime environment. Yes, Microsoft isn't great, but they aren't Oracle

For Windows-based development it's a no brainer to end all no brainers. OTOH, for Linux I'd probably still use java because "lol mono".
 

mike23

Member
Ok, we're working on linked lists in class right now and I am running into a problem I can't fix.
In function "takeInfo," nTime and take don't get the values copied to them. What am I missing?


Also, any general tips for anything else would be helpful :)

Just taking a cursory glance, try looking up the difference between value types and reference types.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
New line. My problem with same line braces is the lack of symmetry.

Code:
function ThisShitUgly(){
     // Ugly scrunched shit
} // What is this thing even doing here?

function GloriousSymmetry()
{
     {
          // Love it
     }
}
 
Top Bottom