• 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

Yeah, why not?

My interview went well... I think?

image.php


If you think it went well, you probably have the job. I come out of most interviews feeling like I'm humanity's biggest failure.
 
Are there any good programming podcasts out there? Thinking about loading up my phone with some when I go to take a walk.

I listen to Hanselminutes and Herding Code. Stack Exchange podcast sometimes has some interesting stuff depending on their guest. I'd be interested to hear about more.
 

Onemic

Member
Can someone explain how pointers work? Reading it in my C++ book, it seems like I'm being thrown a bunch of concepts and explanations with no real write up of how or why pointers are important
 

Bollocks

Member
Can someone explain how pointers work? Reading it in my C++ book, it seems like I'm being thrown a bunch of concepts and explanations with no real write up of how or why pointers are important

With what exactly do you have trouble with?
Pointers are not that hard to grasp as they seem at first.

Pointers point to a location in memory.
if you do char *p=0x01234. It points to the memory location 0x01234. You can then read the value that is stored at 0x0123 like so: printf("Value at 0x1234 is %c", *p).
Notice the (*), this is used to access the value of said memory location.

Since pointers store the memory location you can also retrieve that by simply doing
printf("Value at %p is %c", p, *p);
Again notice the (*), with the asterisk you access the value, without the memory location.
If you do:
*p = 10//set value that the pointer points to, to 10
p = 10//set the pointer to point to memory adress 10

Also note that char *p = 10 and *p = 10 is NOT the same.
The first statement declares a char pointer that points to memory location 10.
Second statement is a simple assignment.
That can be confusing at first, since the use of the asterisk is different.

You also don't need to explicitly point to memory location by hand.
Say you want to point to a known variable:
int i=23;
int *p = null;

p = &i; //get the address of i via the & operator
*p = 58; //same as i=58 since p "points" to it

When you allocate memory dynamically with new/malloc you also get a pointer back in return.

I mean there's more to pointers like function pointers but the above is the basic concept behind it.
 

Jackben

bitch I'm taking calls.
Can someone explain how pointers work? Reading it in my C++ book, it seems like I'm being thrown a bunch of concepts and explanations with no real write up of how or why pointers are important
Check out this guy named Bucky's C++ Youtube videos. They're pretty awesome in terms of breaking down the concepts into easy to relate to examples and actually showing you some code on screen of what he is talking about. I was struggling with a lot of stuff but seeing the code being written, built and executed in real-time on a screen helped immensely versus trying to decipher how it all fits together from static print on a textbook page.

http://www.youtube.com/watch?v=Fa6S8Pz924k
 

Water

Member
Can someone explain how pointers work? Reading it in my C++ book, it seems like I'm being thrown a bunch of concepts and explanations with no real write up of how or why pointers are important
You need pointers to mess with raw memory. That is mainly necessary in situations where you want to write your own data structures from the ground up. Otherwise, good modern C++ code should use pointers only rarely.
 
You need pointers to mess with raw memory. That is mainly necessary in situations where you want to write your own data structures from the ground up. Otherwise, good modern C++ code should use pointers only rarely.
....I just realized how little I use pointers in C++. Every project has at least one or two but I use a lot less than I do in C.
 

Onemic

Member
With what exactly do you have trouble with?
Pointers are not that hard to grasp as they seem at first.

Pointers point to a location in memory.
if you do char *p=0x01234. It points to the memory location 0x01234. You can then read the value that is stored at 0x0123 like so: printf("Value at 0x1234 is %c", *p).
Notice the (*), this is used to access the value of said memory location.

Since pointers store the memory location you can also retrieve that by simply doing
printf("Value at %p is %c", p, *p);
Again notice the (*), with the asterisk you access the value, without the memory location.
If you do:
*p = 10//set value that the pointer points to, to 10
p = 10//set the pointer to point to memory adress 10

Also note that char *p = 10 and *p = 10 is NOT the same.
The first statement declares a char pointer that points to memory location 10.
Second statement is a simple assignment.
That can be confusing at first, since the use of the asterisk is different.

You also don't need to explicitly point to memory location by hand.
Say you want to point to a known variable:
int i=23;
int *p = null;

p = &i; //get the address of i via the & operator
*p = 58; //same as i=58 since p "points" to it

When you allocate memory dynamically with new/malloc you also get a pointer back in return.

I mean there's more to pointers like function pointers but the above is the basic concept behind it.

It starts getting complicated when combined with references, classes, constructors, destructors, and the like. So it's not that important in the grand scheme of things? just based on how much stuff im being thrown here I thought it was a very important part of writing code in C++

I'm gonna be doing a class in C starting Tuesday, how important are pointers in C?
 

Water

Member
It starts getting complicated when combined with references, classes, constructors, destructors, and the like. So it's not that important in the grand scheme of things? just based on how much stuff im being thrown here I thought it was a very important part of writing code in C++

I'm gonna be doing a class in C starting Tuesday, how important are pointers in C?
You can't get anything done in C without pointers. For example, C doesn't have a separate datatype for strings; C strings are pointers to an array of characters. (If you think that sounds like a mess, you are absolutely correct.)
 

tokkun

Member
Otherwise, good modern C++ code should use pointers only rarely.

I encounter pointers pretty frequently. Some common design patterns like bridge, PIMPL, and factories rely on them. In multi-threaded programming, it is often necessary to store pointers for functors/closures. And of course there are times when you absolutely need to allocate something from the heap - again, with asynchronous programming this happens very often.
 

John_B

Member
SQL + PHP question.

I have filled an array with a result set of a large amount of games. What is a simple way to output the array by grouping some of the values?

The way I get the wanted ouput can not be the most simple way (building multidimensional raids from two queries).

SQL result set
Code:
round  date         team_1    team_2     score_1  score_2
1      2013-08-17   John      Brian       5        5
1      2013-08-17   Jack      Bird        5        5
1      2013-08-17   Josh      Bean        5        5
2      2013-08-24   Brian     John        5        5
2      2013-08-24   Bird      Jack        5        5
2      2013-08-24   Bean      Josh        5        5

PHP array
Code:
$games = array(
    array(
        'round'   => 1,
        'date'    => '2013-08-17',
        'team_1'  => 'John',
        'team_2'  => 'Brian',
        'score_1' => 5,
        'score_2' => 5,
    ),
    array(
        'round'   => 1,
        'date'    => '2013-08-17',
        'team_1'  => 'Jack',
        'team_2'  => 'Bird',
        'score_1' => 5,
        'score_2' => 5,
    ),
);

Wanted output (in html tables)
Code:
2013-08-17
Round 1
John  5 - 5 Brian
Jack  5 - 5 Bird
Josh  5 - 5 Bean

2013-08-24
Round 2
Brian 5 - 5 John
Bird  5 - 5 Jack
Bean  5 - 5 Josh

Thanks for any input.
 

Jackben

bitch I'm taking calls.
Fuck. I was not prepared for this C++ Object-Oriented Programming class. I took a fundamentals course over the summer and felt pretty established but the shit we are doing now is ridiculously complicated that it makes me realize I didn't really master anything except the most basic of procedural programming in my last class.

Despite reading the introduction chapter on Classes twice, watching a shit load of videos, reading a chapter from a separate book (C++ Primer Plus) and looking up solutions online I'm still struggling to get through the first assignment (converting roman numerals to decimal). I could just steal code and submit it but I'm not an idiot; maybe I'd do that if I actually understood what the stolen code was doing but I don't. I actually need to understand this shit to continue on or it's a pointless exercise. And even if I cheated my way through the course, then what? I'm just going to cheat through 3 more programming courses or until I switch languages? I don't think so. I want to learn this shit. But having to pause over every bit of code and brush up on fundamentals that I'm already supposed to know, all while the clock is ticking down on an already late assignment, with another due in a few days that requires yet another chapter I haven't even tackled yet? Seems like a fools game.

I'm just sinking into a deeper and deeper hole here.

Considering just dropping it and picking it up later when I can do an in-person class (current one is online which sucks as there is basically NO instructor involvement aside from "tips via email" or "feel free to call me and I will walk you through over the phone"). But that just means delaying getting my associates by another semester. I feel like I make better connections when watching free tutorial videos than I do by reading my text book, but not enough to understand the complicated shit going on in my assignments. Majorly depressed as it seems lose-lose no matter what. Maybe programming is just not for me. I hate the "quitter" mentality but I honestly am at my wit's end here.
 

Kalnos

Banned
@Jackben

Honestly, I can't offer you any real help but I can tell you that I know how you feel. There was a point early in my academic career where nothing 'clicked'. Programming is a field where there is a lot of trying and failing as part of the learning process, and that's OK. The documentation exists so that you can quickly relearn fundamental things. Everyone struggles like this so don't sweat it.

Don't be afraid of doing some soul-searching though, I wish I would have earlier. I have a Computer Engineering degree and I'm looking to go into technical positions that don't require me to program. I love technology but not programming. Not because I'm a horrible programmer but rather because I just don't enjoy doing it all day. I want to keep it as a hobby at this point.

Don't worry about 'quitting'. If you drop a class with the intent to retake it later you aren't 'quitting' and if you find out that something isn't for you then you aren't 'quitting' but simply making a really good choice for your own happiness.
 

Jackben

bitch I'm taking calls.
Thanks for sharing your words and experience Kalnos. I honestly appreciate it a lot.

C++ is my first language. I'm going to press pause, try and review some things in my spare time by personal reading of my books, stackoverflow and code academy and then try again with an in-person class in the spring.
 

Tamanon

Banned
In your own words, how would you convert roman numbers to decimal? Which steps would you need?

Would have to determine the rules, which I always forget, but would just be a series of nested if-thens to add to a final total, as you iterate over the spliced string. Would have to start at the left and then compare the digit to the left and right to determine if you add or subtract it.

Oh you're talking to the banned guy:)
 

Kalnos

Banned
In your own words, how would you convert roman numbers to decimal? Which steps would you need?

Parse the roman numeral input into tokens which are mapped into corresponding decimal values (e.g. I = 1, V = 5) and then loop through the tokens and sum them all together.

EDIT: yeah he is, just answering it anyway just in case. :p
 

arit

Member
It' not really clear to me, are you having problems solely because you have problems with the programming itself, or do your problems begin with the solving of the assignment?

Do you try to solve the problem of the assignment while trying to figure out how to do the programming, or are you already doing it in two steps, as in solving the problem on paper first and then implementing it?
 

hateradio

The Most Dangerous Yes Man
SQL + PHP question.

I have filled an array with a result set of a large amount of games. What is a simple way to output the array by grouping some of the values?

The way I get the wanted ouput can not be the most simple way (building multidimensional raids from two queries).

SQL result set
Code:
round  date         team_1    team_2     score_1  score_2
1      2013-08-17   John      Brian       5        5
1      2013-08-17   Jack      Bird        5        5
1      2013-08-17   Josh      Bean        5        5
2      2013-08-24   Brian     John        5        5
2      2013-08-24   Bird      Jack        5        5
2      2013-08-24   Bean      Josh        5        5
Just store the date, if everything is stored by date and round.

Code:
$date = '';
foreach($games as $g) {
	// Output the date/round once
	if ($date != $g['date']) {
		echo $g['date'] . "\nRound " . $g['round'] . "\n";
	}
	// store the date
	$date = $g['date'];
	
	
	printf("%s %d - %d %s\n", $g['team_1'], $g['score_1'], $g['score_2'], $g['team_2']);
}
 

leroidys

Member
Just to add to other people's suggestions for roman numerals - be sure to check all possible scenarios. For example, if you get IIX, you would be able to tell that that is an illegal combination, and error out, rather than trying to parse impossible sequences.

EDIT: Ah, banned. Just stick with it if you have an interest. Many people will tell you to quit if you don't LOOOOOOOOOOOVE programming right away, but I don't think this is good advice for everyone. My first year of school I pretty much hated programming and hated my life, but once i started to understand how things were working together and being able to creatively accomplish non-trivial engineering problems, I really started to love it. C++ is hard to start out with. Just keep plugging away and don't stress out about things like "another semester". Take the time to learn it thoroughly and you'll be much better off in the end.
 
Just stick with it if you have an interest. Many people will tell you to quit if you don't LOOOOOOOOOOOVE programming right away, but I don't think this is good advice for everyone. My first year of school I pretty much hated programming and hated my life, but once i started to understand how things were working together and being able to creatively accomplish non-trivial engineering problems, I really started to love it. C++ is hard to start out with. Just keep plugging away and don't stress out about things like "another semester". Take the time to learn it thoroughly and you'll be much better off in the end.

I wholeheartedly agree with this. I have seen a lot of students fail an introductory programming course, only to come back and try again. Sometimes these students fail again, but there are also times where the student ends up learning the content much better than those who passed the course first time, and go on to do very well. Keep trying and using all the resources available for you, because it 'clicks' at different points for everyone.
 

Water

Member
I encounter pointers pretty frequently. Some common design patterns like bridge, PIMPL, and factories rely on them. In multi-threaded programming, it is often necessary to store pointers for functors/closures. And of course there are times when you absolutely need to allocate something from the heap - again, with asynchronous programming this happens very often.
Yeah, my previous comment shouldn't be taken as an absolute. There are times and places where you need a pointer in C++, or another way would be too contrived. What I was getting at is that beginners should be suspicious of pointers and shouldn't touch them until they have a real reason. Especially people who have previously done some C programming have a tendency to write atrocious C++. Having them stop using pointers entirely for a while is an effective way to get them on the right track.

Even when you allocate something from the heap, the raw pointer often does not need to be visible. It's inside some data structure, or you make an individual allocation directly into a smart pointer like unique_ptr.
 

John_B

Member
Just store the date, if everything is stored by date and round.

Code:
$date = '';
foreach($games as $g) {
	// Output the date/round once
	if ($date != $g['date']) {
		echo $g['date'] . "\nRound " . $g['round'] . "\n";
	}
	// store the date
	$date = $g['date'];
	
	
	printf("%s %d - %d %s\n", $g['team_1'], $g['score_1'], $g['score_2'], $g['team_2']);
}
Pretty simple solution. But when I tried to use it for templates it was a bit messy (having to do multiple if statements for start and end html tags).

So I figured for templates it would probably more easy with foreach in foreach and worked my way backwards from that.

Thanks for your input.

Code:
$dates = array();
foreach ($games as $game) {
    $date = $game['date'];
    $dates[$date][] = $game;
}

foreach ($dates as $date => $games) {
    echo $date;
    // <table>
    foreach ($games as $game) {
        // <tr>
        //   <td>
        echo $game['user_1'];
        //   </td>
        //   <td>
        echo $game['user_2'];
        //   </td>
        // </tr>
    }
    // </table>
}
 

Air

Banned
Hey guys a quick question. So I've been doing the code academy HTML/CSS course and I'm almost done (unfortunately I don't have a bunch of time to dedicate to it, usually an hour a day). I don't really like the course instructor (eric weinstein?) as he doesn't really go into fleshing out the lessons. I just passed the position lesson and it would be great if someone could help explain and give some context to when absolute, fixed and relative positions would be used. Maybe an example or 2 would help me understand better because even though I finished that course, I feel I still don't truly understand its functions (I guess fixed because that one is easy). A decent explanation would go a long way to helping me and I'd appreciate any help at all from you guys (even a link better explaining them).
 

Giggzy

Member
What's the best place to start learning about programming? I'm working towards a computer science degree and will be taking an intro to programming in the spring semester, but I'd like to start getting my feet wet now so I can have a head-start when starting the course. I have absolutely no prior knowledge to programming.
 

phoenixyz

Member
What's the best place to start learning about programming? I'm working towards a computer science degree and will be taking an intro to programming in the spring semester, but I'd like to start getting my feet wet now so I can have a head-start when starting the course. I have absolutely no prior knowledge to programming.
Which language will be used in the class? I would recommend learning a little bit of that.
 

pompidu

Member
This might not be the best place to ask this since it's a Networking class question but I just don't know how to do this.

One problem with a broadcast topology is that if two hosts transmit at the same time, then their signals collide, and no communication takes place. Suppose you have a bus topology with TWO hosts. During each time-slot, each host will attempt to broadcast a packet with uniform probability p = 0.25. What percentage of time slots will be wasted due to a collision? Explain your answer.

Never worked with weird math stuff like this.

Edit: My only guess is (0.25)*(2 hosts). But I really have no idea.
 

fallagin

Member
Hey you guys, I'm having trouble running this mips assembly program on qtspim

For some reason it keeps coming up with the error:
"Instruction references undefined symbol at 0x00400048
[0x400048] 0x0c000000 jal 0x00000000 [printf] ; 15: jal printf"

This is when printf is called I guess.

I copied this code out of the book I'm reading here on pages A-27 to A-29

If anyone could help me out then that would be a big help!!

Edit: Also, for clarification, this program calculates 10! and then prints the result out.

Code:
.text
.globl main
main:
move $t0, $sp  # keep record of sp
subu $sp, $sp, 32 #Stack frame is 32 bytes long
move $t1, $sp # what
sw $ra, 20($sp) # Save return address
sw $fp, 16($sp) #save old frame pointer
addiu $fp, $sp, 28 #set up frame pointer

li $a0, 10 # put argument (10) in $a0
jal fact # call factorial function

la $a0, $LC #put format string in $a0
move $a1, $v0 #move fact result to $a1
[U][B]jal printf # call the printf function -- This is where the problem is!![/B][/U]

lw $ra, 20($sp) #restore return address
lw $fp, 16($sp) #restore frame pointer
addiu $sp, $sp, 32 #pop stack frame
jr $ra # return to caller

.rdata
$LC:
.ascii "The factorial of 10 is %d\n\000"

.text
fact:
subu $sp, $sp, 32 #Stack frame is 32 bytes long
sw $ra, 20($sp) #Save return address
sw $fp, 16($sp) # save frame pointer
addiu $fp, $sp, 28 #set up frame pointer
sw $a0, 0($fp) #Save argument(n)

lw $v0, 0($fp) #load n
bgtz $v0, $L2 #branch if n>0
li $v0, 1 #return 1
jr $L1 # Jump to code to return

$L2:
lw $v1, 0($fp) #load n
subu $v0, $v1, 1 #compute n-1
move $a0, $v0 #Move value to $a0

jal fact #call factorial function
lw $v1, 0($fp) #load n
mul $v0, $v0, $v1 #compute fact(n-1) * n

$L1: #result is in $v0
lw $ra, 20($sp) #restore $ra
lw $fp, 16($sp) #restore $fp
addiu $sp, $sp, 32 #pop stack
jr $ra #return to caller
 

phoenixyz

Member
This might not be the best place to ask this since it's a Networking class question but I just don't know how to do this.

Never worked with weird math stuff like this.

Edit: My only guess is (0.25)*(2 hosts). But I really have no idea.
"Weird math stuff"? Really?
The chance that host A transmits is 25%. If host A transmits, the chance is 25% that host B also transmits, hence the chance that both transmit is 0.25*0.25 = 0.25^2 = 0.0625 = 6.25%
 

Tamanon

Banned
This might not be the best place to ask this since it's a Networking class question but I just don't know how to do this.



Never worked with weird math stuff like this.

Edit: My only guess is (0.25)*(2 hosts). But I really have no idea.

Basically it's asking what are the chances that both are transmitting at the same time. So 0.25 * 0.25 would tell you the probability of it happening. So 0.0625 or 6.25% of the time they would be having a collision.
 

pompidu

Member
"Weird math stuff"? Really?
The chance that host A transmits is 25%. If host A transmits, the chance is 25% that host B also transmits, hence the chance that both transmit is 0.25*0.25 = 0.25^2 = 0.0625 = 6.25%

Basically it's asking what are the chances that both are transmitting at the same time. So 0.25 * 0.25 would tell you the probability of it happening. So 0.0625 or 6.25% of the time they would be having a collision.

That's what I figured but was having a dispute with a classmate about the correct way to do it. Thanks Guys!
 

usea

Member
Hey you guys, I'm having trouble running this mips assembly program on qtspim

For some reason it keeps coming up with the error:
"Instruction references undefined symbol at 0x00400048
[0x400048] 0x0c000000 jal 0x00000000 [printf] ; 15: jal printf"

This is when printf is called I guess.

I copied this code out of the book I'm reading here on pages A-27 to A-29

If anyone could help me out then that would be a big help!!

Edit: Also, for clarification, this program calculates 10! and then prints the result out.
Hi. I don't know assembly, but it looks like printf isn't defined anywhere else in the program. If you look at another similar example, where it says "jal fact", it's calling (jumping to?) the factorial function. There's a part near the bottom where fact is defined, starting with "fact:" There is no such place for printf. It's missing.
 

fallagin

Member
Hi. I don't know assembly, but it looks like printf isn't defined anywhere else in the program. If you look at another similar example, where it says "jal fact", it's calling (jumping to?) the factorial function. There's a part near the bottom where fact is defined, starting with "fact:" There is no such place for printf. It's missing.

I know, I'm not sure whether I am supposed to define it myself or if there is supposed to be some kind of preset printf macro. The book isn't clear on this and it doesnt show you how to define it(if you do need to).
 

tokkun

Member
I know, I'm not sure whether I am supposed to define it myself or if there is supposed to be some kind of preset printf macro. The book isn't clear on this and it doesnt show you how to define it(if you do need to).

The example you are seeing in the book is an excerpt, not all of the C code you need for it to work, nor all of the assembled code. They are attempting to show you what a subset of the MIPS code would look like for illustrative purposes. The MIPS code is not intended to be runnable on its own.

printf is defined in the C standard library. The C code shown in the book would need to include stdio.h. The linker then combines object code generated from the compiler with the pre-compiled object code in the standard library (which contains the code for printf).

If you want that example to work, you need to do the following:
1. Compile the MIPS assembly code into object code.
2. Link the object code with a MIPS version of glibc
 

pompidu

Member
I'm sure some of you are college students. Does anyone know the deal with the free windows 8 that comes from dreamspark? I downloaded and plan on upgrading to it but idk the restrictions that come with it since its from dreamspark.
 

Tamanon

Banned
I'm sure some of you are college students. Does anyone know the deal with the free windows 8 that comes from dreamspark? I downloaded and plan on upgrading to it but idk the restrictions that come with it since its from dreamspark.

I don't think there are any restrictions. My copy came from dreamspark. I assume there's something about not using it for commercial blah blah, but it seems to be a fully functioning Win 8.
 

pompidu

Member
I don't think there are any restrictions. My copy came from dreamspark. I assume there's something about not using it for commercial blah blah, but it seems to be a fully functioning Win 8.

OK cool. I did a compatibility test and the only thing that came was the dvd issue and that my firmware doesn't support secure boot. Any issues with secure boot not being present on my machine??
 

Tamanon

Banned
OK cool. I did a compatibility test and the only thing that came was the dvd issue and that my firmware doesn't support secure boot. Any issues with secure boot not being present on my machine??

Shouldn't be an issue that wouldn't exist in any other OS, just means you can't use that feature. Not a big deal.
 

fallagin

Member
The example you are seeing in the book is an excerpt, not all of the C code you need for it to work, nor all of the assembled code. They are attempting to show you what a subset of the MIPS code would look like for illustrative purposes. The MIPS code is not intended to be runnable on its own.

printf is defined in the C standard library. The C code shown in the book would need to include stdio.h. The linker then combines object code generated from the compiler with the pre-compiled object code in the standard library (which contains the code for printf).

If you want that example to work, you need to do the following:
1. Compile the MIPS assembly code into object code.
2. Link the object code with a MIPS version of glibc

Yeah, that is probably what it is, I'll need to figure out how to do that. Thank you.
 

hateradio

The Most Dangerous Yes Man
Pretty simple solution. But when I tried to use it for templates it was a bit messy (having to do multiple if statements for start and end html tags).

So I figured for templates it would probably more easy with foreach in foreach and worked my way backwards from that.

Thanks for your input.

Code:
$dates = array();
foreach ($games as $game) {
    $date = $game['date'];
    $dates[$date][] = $game;
}
}
This isn't a good template at all, but just cycle through each game individually instead of having PHP do the key => val technique.

Code:
foreach ($dates as $d) {
	echo "<table>";
	foreach ($d as $g) {
		echo "\n	<tr>\n";
		printf('<td>%s</td> <td>%d</td> <td>%d</td> <td>%s</td>', $g['team_1'], $g['score_1'], $g['score_2'], $g['team_2']);
		echo "\n	</tr>";
	}
	echo "\n</table>";
}
 
I'm sure some of you are college students. Does anyone know the deal with the free windows 8 that comes from dreamspark? I downloaded and plan on upgrading to it but idk the restrictions that come with it since its from dreamspark.
After you do that get Visual Studio 2012. I got the professional version for free. Blew me away that Microsoft would let me do that.
 

Tristam

Member
Would have to determine the rules, which I always forget, but would just be a series of nested if-thens to add to a final total, as you iterate over the spliced string. Would have to start at the left and then compare the digit to the left and right to determine if you add or subtract it.

Oh you're talking to the banned guy:)


Yup, saw the guy's question and played with it in Perl as that's what I'm learning now, although for me it made sense to evaluate the array in reverse.

Code:
#!/usr/bin/perl
# roman.pl
use warnings;
use strict;

#XL = 40; L = 50; XC = 90; C = 100
my $value=0;
print "Enter a Roman numeral: ";
chomp(my $input=<STDIN>);
my @array=split("", $input);
my $temp_var="NULL";
for ( reverse(@array) ) {
	if ($_ eq "I" && $temp_var eq "V") {
		$value-=1;
	} elsif ($_ eq "I" && $temp_var eq "X") {
		$value-=1;
	} elsif ($_ eq "V") {
		$value+=5;
	} elsif ($_ eq "L") {
		$value+=50;
	} elsif ($_ eq "C") {
		$value+=100;
	} elsif ($_ eq "X" && $temp_var eq "L") {
		$value-=10;
	} elsif ($_ eq "X" && $temp_var eq "C") {
		$value-=10;
	} elsif ($_ eq "X") {
		$value+=10;
	} elsif ($_ eq "I") {
		$value+=1;
	}
	$temp_var=$_; # access temp var (previous value) in above operations
}
print "$value\n";

Not particularly elegant and no error handling to speak of, but it gets the job done up to a point. (Adding Roman numerals D, M, etc. is fairly easy to incorporate though as the general pattern is well-established.)
 
What's the best place to start learning about programming? I'm working towards a computer science degree and will be taking an intro to programming in the spring semester, but I'd like to start getting my feet wet now so I can have a head-start when starting the course. I have absolutely no prior knowledge to programming.
One of the courses over on Edx (MIT's 6.00x Introduction to Computer Science and Programming), Coursera (Rice University's an introduction to interactive programming in python, starts next month) or Udacity (University of Virginia's intro to CS).

They all use python, so if you're expected to learn another language, look around those sites. There are also a few introduction courses teaching a bit about other languages (for example, Harvard's 'Introduction to Computer Science' includes C, PHP, and JavaScript plus SQL, CSS, and HTML, while over on Udacity there is also a course teaching Java.
 

sephi22

Member
After only studying basic C in college, I'm faced with a nasty assignment in Grad School.

Could someone explain what an anchor node is in reference to a circular double linked list?
 
After only studying basic C in college, I'm faced with a nasty assignment in Grad School.

Could someone explain what an anchor node is in reference to a circular double linked list?

it's the list item that you use as the point of entry into the list. generally you store a pointer to it in your list struct, and each time you want to access the list eg. to do an insert or search for a particular element, you start from the anchor point and follow the links to subsequent nodes. the actual item can change, for instance if you are doing a sort-on-insert list of integers, you might check first that the int you are inserting is less than the anchor point and if so you insert it between the first and last items and set the anchor variable to point to the new item.
 

John_B

Member
This isn't a good template at all, but just cycle through each game individually instead of having PHP do the key => val technique.

I was putting the old array into a new array with the keys containing the dates.

I'm building an app with the Laravel framework (MVC). I need to retrieve and manipulate the data before passing it on to the view.

I'm not sure how else to do it while keeping the logic simple in the view.

Code:
@foreach ($dates as $date => $games)
<table>
    <thead>
        <tr>
            <td colspan="5">{{ $date }}</td>
        </tr>
    </thead>
    <tbody>
        @foreach ($games as $game)
        <tr>
            <td>{{ $game->league }}</td>
            <td>{{ $game->round }}</td>
            <td>{{ $game->user_1 }}</td>
            <td>{{ $game->score_1 }} - {{ $game->score_2 }}</td>
            <td>{{ $game->user_2 }}</td>
        </tr>
        @endforeach
    </tbody>
</table>
@endforeach
 
Top Bottom