Is there a difference between these 2 function prototypes if I want to pass a static-sized array to a function?
[code]
int func(int array[]);
int func2(int *array);
[/code]
[QUOTE=rilez;45433304]Looking for C++/C# book recommendations. I want to buy a nice big selection of books of all kinds. Reference, introductory programming, etc. I've looked myself, but there's a pretty huge selection.
I'm still mostly a beginner, if that factors in. Also other languages are fine, those are just two I want to focus on right now[/QUOTE]
For C# definitely the [URL="http://www.amazon.com/Programming-Language-Covering-Microsoft-Development/dp/0321741765/"]annotated version of the official specification[/URL]. I don't think there is one available for C# 5.0 yet so it won't have anything on the Async and Caller Information features.
[QUOTE=ollie;45435512]Is there a difference between these 2 function prototypes if I want to pass a static-sized array to a function?
[code]
int func(int array[]);
int func2(int *array);
[/code][/QUOTE]
There isn't really.
This is a question for those who program for a living.
I'm going to college next year, and I have to figure out what I'm going to major in. I've pretty much been interested in computer science and programming since 5 years ago, and I'm interested in programming games as a career, and I want to get a 4 year degree.
The problem that I'm having is that I have heard that a "game programming" degree isn't worth as much as a straight up computer science degree. I don't know what to go for. I am mostly interested in the lower level of game programming. I love dealing with structure, and rendering, and the lower levels of the games systems rather than just scripting game levels or whatever. Any input on what I should be looking for would be great.
[QUOTE=Duskling;45437298]This is a question for those who program for a living.
I'm going to college next year, and I have to figure out what I'm going to major in. I've pretty much been interested in computer science and programming since 5 years ago, and I'm interested in programming games as a career, and I want to get a 4 year degree.
The problem that I'm having is that I have heard that a "game programming" degree isn't worth as much as a straight up computer science degree. I don't know what to go for. I am mostly interested in the lower level of game programming. I love dealing with structure, and rendering, and the lower levels of the games systems rather than just scripting game levels or whatever. Any input on what I should be looking for would be great.[/QUOTE]
If you are certain you want to get a job as a game developer then there's nothing wrong with doing a games programming degree. It does also open up a lot of other opportunities for you, but a CS degree will probably give you a wider range of non-game choices.
I'd say go with CS. Before college I felt the same way, thinking that I wanted a game programming degree, but I was talked out of it and went with CS instead. Like BackwardSpy said it opens up a lot more options for you if you realize that game programming isn't your thing.
CS majors learn mostly the same stuff as a game programmer, but obviously the topics are more broad. In many cases it is actually better because you get to see a bit of everything which gives you different perspectives on the same problem and helps you become a better programmer. Depending on the school you go to there will be CS classes you can take that focus on game-related things like computer graphics.
Game programming isn't really that distinct a subset of programming, anyway. If you can program, you can program games, and the trickier stuff you'll probably have to figure out yourself, anyway.
Okay, sounds good. I think I will definetly go with Computer Science then. I'll try to focus mainly on game related classes but I do want a more general "computer programming" education.
Alright, I tried to start learning the basics of Lua yesterday. I made a script with some basic things but I keep getting an error and I don't know how to interpret it.
[code]
--There is a bug in the box eating the cookies!
function boxBug(boxnumber)
local amounttoEat = 69
if box[boxnumber] = 0 then
Msg("The bug ate no cookies from box "..boxnumber.." because there were none there to begin with.\n")
return 0
elseif (box[boxnumber] - amounttoEat) <= 0 then
Msg("The bug ate all "..box[boxnumber].." of the cookies from box "..boxnumber..".\n")
return 0
else
Msg("The bug ate "..amounttoEat.." of the cookies from box "..boxnumber..".\n")
return (amountinbox - amounttoEat)
end
end
--Runs the boxBugfunction for all four boxes.
function runboxBug()
for i=1,4 do
box[i]=boxBug(i)
end
end
--Displays the amount of cookies in a specific box.
function inBox(index)
Msg("There are "..box[index].." cookies in box "..index..".\n")
end
--Runs the inBox function for all four boxes.
function runinBox()
for i=1,4 do
inBox(i)
end
end
--Sets amount of cookies in four boxes.
box={}
box[1]=69
box[2]=420
box[3]=96
box[4]=16
runinBox()
runboxBug()
runinBox()
runboxBug()
runinBox()
Msg("end \n")[/code]
input:4: 'then' expected near '='
[QUOTE=tanktan38;45438950]Alright, I tried to start learning the basics of Lua yesterday. I made a script with some basic things but I keep getting an error and I don't know how to interpret it.
-code-
input:4: 'then' expected near '='[/QUOTE]
[lua]
--There is a bug in the box eating the cookies!
function boxBug(boxnumber)
local amounttoEat = 69
if (box[boxnumber] == 0) then
Msg("The bug ate no cookies from box "..boxnumber.." because there were none there to begin with.\n");
return 0;
elseif (box[boxnumber] - amounttoEat <= 0) then
Msg("The bug ate all "..box[boxnumber].." of the cookies from box "..boxnumber..".\n");
return 0;
else
Msg("The bug ate "..amounttoEat.." of the cookies from box "..boxnumber..".\n");
return (amountinbox - amounttoEat);
end
end
--Runs the boxBugfunction for all four boxes.
function runboxBug()
for iii = 1, 4 do
box[iii] = boxBug(iii);
end
end
--Displays the amount of cookies in a specific box.
function inBox(index)
Msg("There are "..box[index].." cookies in box "..index..".\n")
end
--Runs the inBox function for all four boxes.
function runinBox()
for iii = 1, 4 do
inBox(iii);
end
end
--Sets amount of cookies in four boxes.
box = {};
box[1] = 69;
box[2] = 420;
box[3] = 96;
box[4] = 16;
runinBox();
runboxBug();
runinBox();
runboxBug();
runinBox();
Msg("end \n");
[/lua]
= is assignment, == is comparison :)
[QUOTE=G4MB!T;45439031]
= is assignment, == is comparison :)[/QUOTE]
Thanks. :smile:
I also fixed up your spacing (according to my preferences sorry) and some other styling.
More fun with binary trees in java...
[CODE]
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
class BinarySearchTree<T extends Comparable<? super T>> extends ArrayList<Node<T>> {
private Node<T> root;
private String inOrder = "";
private int count = 0;
public BinarySearchTree() {
root = null;
}
public Node<T> getRoot() {
return root;
}
public BinarySearchTree(ArrayList<T> tree) {
for (int i = 0; i < tree.size(); i++) {
this.addData(tree.get(i));
}
}
public void addData(T data) {
Node<T> nodeToAdd = new Node(data);
if (root == null) {
root = nodeToAdd;
}
addNode(root, nodeToAdd);
}
private void addNode(Node<T> node, Node<T> nodeToAdd) {
if (nodeToAdd.value.compareTo(node.value) < 0) {
if (node.leftChild == null) {
nodeToAdd.parent = nodeToAdd;
node.leftChild = nodeToAdd;
} else {
addNode(node.leftChild, nodeToAdd);
}
} else if (nodeToAdd.value.compareTo(node.value) > 0) {
if (node.rightChild == null) {
nodeToAdd.parent = nodeToAdd;
node.rightChild = nodeToAdd;
} else {
addNode(node.rightChild, nodeToAdd);
}
}
}
public void removeNode(T item) {
if (root != null) {
remove(item);
} else {
System.err.println("No nodes to remove");
}
}
private Node<T> remove(T value) {
Node<T> nodeToBeDeleted = new Node(getNode(value));
System.out.println(nodeToBeDeleted.value);
if (nodeToBeDeleted.isLeaf()) {
removeLeaf(nodeToBeDeleted);
this.remove(this.indexOf(nodeToBeDeleted));
return nodeToBeDeleted;
} else if (nodeToBeDeleted.hasChildren()) {
removeChildren(nodeToBeDeleted);
this.remove(this.indexOf(nodeToBeDeleted));
return nodeToBeDeleted;
} else if (nodeToBeDeleted.hasLeftChild()) {
removeChild(nodeToBeDeleted);
this.remove(this.indexOf(nodeToBeDeleted));
return nodeToBeDeleted;
} else if (nodeToBeDeleted.hasRightChild()) {
removeChild(nodeToBeDeleted);
this.remove(this.indexOf(nodeToBeDeleted));
return nodeToBeDeleted;
}
return null;
}
private void removeChildren(Node<T> nodeToBeDeleted) {
Node<T> min = new Node(findLeftNode(nodeToBeDeleted));
removeChild(min);
min.parent = nodeToBeDeleted.parent;
min.leftChild = nodeToBeDeleted.leftChild;
min.rightChild = nodeToBeDeleted.rightChild;
if (nodeToBeDeleted.parent == null) {
root = min;
} else {
if (nodeToBeDeleted.parent.leftChild == nodeToBeDeleted) {
nodeToBeDeleted.parent.leftChild = min;
} else if (nodeToBeDeleted.parent.rightChild == nodeToBeDeleted) {
nodeToBeDeleted.parent.rightChild = min;
}
}
}
private Node<T> findLeftNode(Node<T> tNode) {
if (tNode.leftChild == null) {
return tNode;
}
return findLeftNode(tNode.leftChild);
}
private void removeChild(Node<T> nodeToBeDeleted) {
if (nodeToBeDeleted.parent.leftChild == nodeToBeDeleted) {
if (nodeToBeDeleted.leftChild != null) {
nodeToBeDeleted.parent.leftChild = nodeToBeDeleted.leftChild;
} else if (nodeToBeDeleted.rightChild != null) {
nodeToBeDeleted.parent.leftChild = nodeToBeDeleted.rightChild;
}
} else if (nodeToBeDeleted.parent.rightChild == nodeToBeDeleted) {
if (nodeToBeDeleted.leftChild != null) {
nodeToBeDeleted.parent.rightChild = nodeToBeDeleted.leftChild;
} else if (nodeToBeDeleted.rightChild != null) {
nodeToBeDeleted.parent.rightChild = nodeToBeDeleted.rightChild;
}
}
}
private void removeLeaf(Node<T> nodeToBeDeleted) {
if (nodeToBeDeleted.parent.leftChild == nodeToBeDeleted) {
nodeToBeDeleted.parent.leftChild = null;
} else if (nodeToBeDeleted.parent.rightChild == nodeToBeDeleted) {
nodeToBeDeleted.parent.rightChild = null;
}
}
public T getNode(T item) {
Node<T> val = root;
while (val != null) {
if (val.getData().compareTo(item) < 0) {
val = val.rightChild;
} else if (val.getData().compareTo(item) > 0) {
val = val.leftChild;
} else {
return val.getData();
}
}
return null;
}
public boolean find(T item) {
if (getNode(item) == item) {
return true;
}
return false;
}
private String inOrderTraversal(Node<T> root) {
if (root != null) {
inOrderTraversal(root.getLeftChild());
inOrder += root.getData() + " ";
inOrderTraversal(root.getRightChild());
}
return inOrder;
}
public String inOrder() {
String order = inOrderTraversal(root);
inOrder = "";
return order;
}
public String levelOrderTraversal(Node<T> root) {
Queue<Node<T>> nodequeue = new LinkedList();
if (root != null) {
nodequeue.add(root);
}
while (!nodequeue.isEmpty()) {
Node<T> next = nodequeue.remove();
System.out.print(next.getData() + " ");
if (next.getLeftChild() != null) {
nodequeue.add(next.getLeftChild());
}
if (next.getRightChild() != null) {
nodequeue.add(next.getRightChild());
}
}
return "";
}
public int compareTo(T o) {
return o.compareTo(root.getData());
}
}
[/CODE]
[CODE]
public class Node<T> {
public T value;
public Node<T> leftChild, rightChild,parent;
public Node(T value) {
this.value = value;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node<T> leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setValue(T value) {
this.value = value;
}
public void setRightChild(Node<T> rightChild) {
this.rightChild = rightChild;
}
public Node(T data, Node<T> next) {
this.value = data;
}
public T getData() {
return value;
}
public boolean hasLeftChild() {
return this.leftChild != null;
}
public boolean hasRightChild() {
return this.rightChild != null;
}
@Override
public String toString() {
return "Node{" + "value: " + value + ", leftChild: " + leftChild + ", rightChild: " + rightChild + '}';
}
public boolean isLeaf() {
return (this.leftChild == null && this.rightChild == null);
}
public boolean hasChildren() {
return (hasLeftChild() || hasRightChild());
}
}
[/CODE]
[CODE]
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
bst.addData(5);
bst.addData(1);
bst.addData(2);
bst.addData(6);
bst.addData(7);
bst.addData(8);
bst.addData(0);
// 5
// 1 6
// 0 2 7 8
System.out.println(bst.inOrder());
bst.removeNode(0);
}
}[/CODE]
So I keep getting a null pointer exception. I've tried every node and can't remove any of them. I keep hitting my remove leaf method and I don't know why because 5 is clearly not a leaf nor is 6 or 1.
Can someone explain to me why sending structs through send/sendto() is a bad idea? I guess I'm not understanding it.
[code]struct Message {
unsigned int id;
unsigned int packetsize;
float coord[2];
unsigned char data[1024];
};
[/code]
I don't even know how to do that yet, but I've been told that I'm stupid for ever considering it. Without much explanation.
Nothing wrong with it. Obviously you just can't use pointers inside the struct.
Just remember to check that packetsize doesn't overflow the actual amount of data you received.
[QUOTE=blacksam;45440038]More fun with binary trees in java...
<code>
So I keep getting a null pointer exception. I've tried every node and can't remove any of them. I keep hitting my remove leaf method and I don't know why because 5 is clearly not a leaf nor is 6 or 1.[/QUOTE]
Why are you creating a new node in the remove method? That's why it thinks it's a leaf.
Change
[code]private Node<T> remove(T value) {
Node<T> nodeToBeDeleted = new Node(getNode(value));
...
}[/code]
to
[code]private Node<T> remove(T value) {
Node<T> nodeToBeDeleted = getNode(value);
...
}[/code]
How is that even compiling anyway? Node doesn't have a constructor that takes another node. It should complain about not being able to convert Node<T> to T or something.
It works because of the default copy constructor.
Actually I'm not sure, I though it was C++ not Java.
[QUOTE=WTF Nuke;45442479]It works because of the default copy constructor.
Actually I'm not sure, I though it was C++ not Java.[/QUOTE]
If I remember correctly Java's only default constructor takes no arguments, and you only get it if you don't define any others constructors.
[QUOTE=rilez;45433304]Looking for C++/C# book recommendations. I want to buy a nice big selection of books of all kinds. Reference, introductory programming, etc. I've looked myself, but there's a pretty huge selection.
I'm still mostly a beginner, if that factors in. Also other languages are fine, those are just two I want to focus on right now[/QUOTE]
C++ Early Objects 8th Edition
I liked the O'Reilly C# 4.0 Pocket Reference. There is one for C# 5 now.
[editline]20th July 2014[/editline]
I just bough the Python, bash, C, C++ and regex ones for $65.
[QUOTE=false prophet;45441539]Can someone explain to me why sending structs through send/sendto() is a bad idea? I guess I'm not understanding it.
[code]struct Message {
unsigned int id;
unsigned int packetsize;
float coord[2];
unsigned char data[1024];
};
[/code]
I don't even know how to do that yet, but I've been told that I'm stupid for ever considering it. Without much explanation.[/QUOTE]
As long as the layout isn't implementation specific it works. I think it's always the same with C(++), at least for one binary.
With .NET you have to set it explicitly (either as explicit or sequential layout), but it won't let you marshal it without doing that anyway.
[QUOTE=false prophet;45441539]Can someone explain to me why sending structs through send/sendto() is a bad idea? I guess I'm not understanding it.
[code]struct Message {
unsigned int id;
unsigned int packetsize;
float coord[2];
unsigned char data[1024];
};
[/code]
I don't even know how to do that yet, but I've been told that I'm stupid for ever considering it. Without much explanation.[/QUOTE]
Things like padding and byte-ordering are platform-specific. If you just dump the struct into a buffer and on the other side use the buffer as a direct memory print which you access via a struct pointer, you can get corrupted data, performance loss and even crashes on some systems.
[QUOTE=Dienes;45445137]Things like padding and byte-ordering are platform-specific. If you just dump the struct into a buffer and on the other side use the buffer as a direct memory print which you access via a struct pointer, you can get corrupted data, performance loss and even crashes on some systems.[/QUOTE]
I don't know what this means :v:
I haven't made it far enough into receiving the data yet, but I'm packing my stream/packet like this:
[code]// packing crap into a message
data = new Stream();
data->id = 2;
data->streamsize = sizeof(Stream);
data->type = 102;
strcpy((char*)data->message, "Some raw message");
net->Send(client, data);
delete data;
// elsewhere
int SimpleNetworkLib::Send(int client, Stream* data) {
return send(client, (char*)data, data->streamsize, 0);
}[/code]
[QUOTE=false prophet;45445713]I don't know what this means :v:[/QUOTE]
You can't send struct or class objects over the network. You have to serialise them somehow.
Low-level programs use C structs to serialize data, both with networking and with other I/O. It's prevalent in the Linux kernel and Windows libraries.
C compilers are not allowed to reorder struct members. Only bitfields and struct padding are ambiguous and may differ between compilers. Padding can be controlled with compiler options like pragma pack and __attribute__. Bitfields can be avoided entirely in favour of doing the equivalent manually (and I believe GCC has options for imitating MSVC bitfields anyway).
Different platforms can have differerent byte orders, so if you're targetting multiple platforms (like Linux does), you usually use the htonl and htons functions while packing your struct with data.
[QUOTE=esalaka;45446129]You can't send struct or class objects over the network. You have to serialise them somehow.[/QUOTE]
I'm not sure how serializing works in C++, but here's my server setting some things in an object and sending junk to a client
[img]http://i.imgur.com/7ju8yeL.png[/img]
Is this essentially what you're describing I need to do?
-snip-
What graphics library Flappy Birds and Timberman were built?
so i have been finding this a little weird. It never did this before and is only now acting up. anyone know what the problem is?
i wrote up something small just to show you what happens and well if you need the code here:
as far as i know it shouldn't be doing this. (yes there is vulgar words but that's just because this is getting under my skin)
[code]#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Enter sentence" << endl;
cin >> str;
getline(cin, str);
cout << str;
return 0;
}
[/code]
[IMG]http://puu.sh/akwHx/b1a5cad0ba.png[/IMG]
Remove the "cin >> str;" line, it is what consumes the first word.
Sorry, you need to Log In to post a reply to this thread.