• 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

I've got a VBA question if anyone can help. I wrote a program but I get an error when running it. The error says, "Run Time Error '6': Overflow"

The code for the program is :

Code:
Sub MonteCarlo()
    n = Range("Number")
    Hits = 0
    For Index = 1 To n
        If Rnd ^ 2 + Rnd ^ 2 < 1 Then Hits = Hits + 1
    Next Index
    Range("Estimate") = 4 * Hits / n
End Sub

I'm assuming it's a really easy mistake but does anyone know what it is? Is it because I didn't use dim to declare the variable? I tried that and it seemed to still have issue with the same line. The highlighted line is the line it highlights when I click debug after getting the error. Any help is appreciated.
 
I've got a VBA question if anyone can help. I wrote a program but I get an error when running it. The error says, "Run Time Error '6': Overflow"

The code for the program is :

Code:
Sub MonteCarlo()
    n = Range("Number")
    Hits = 0
    For Index = 1 To n
        If Rnd ^ 2 + Rnd ^ 2 < 1 Then Hits = Hits + 1
    Next Index
    Range("Estimate") = 4 * Hits / n
End Sub

I'm assuming it's a really easy mistake but does anyone know what it is? Is it because I didn't use dim to declare the variable? I tried that and it seemed to still have issue with the same line. The highlighted line is the line it highlights when I click debug after getting the error. Any help is appreciated.

Been a while since I've done any VBA, but what is "n" when you're debugging? Is it 0? Check your variable values. Actually, put a breakpoint at the top of your method and step through it so you can see what it's doing at every step. If n is zero, then your loop won't execute, Hits will not be populated, and you'll ultimately just not get your desired result.
 
Been a while since I've done any VBA, but what is "n" when you're debugging? Is it 0? Check your variable values. Actually, put a breakpoint at the top of your method and step through it so you can see what it's doing at every step. If n is zero, then your loop won't execute, Hits will not be populated, and you'll ultimately just not get your desired result.

Oops! I forgot to enter a number in my model for that cell. :p I at least I didn't screw up the VBA. It feels weird to program in VBA after using Java. Thanks for the help!
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
Feud*

I just saved your A+, son.
No joke, I freaked out about this yesterday before we presented. I went back and changed two splash screens that had the misspelling and fixed it in the comments that described the project in the code. I rely on spellcheck way too much sometimes >_>
Congrats, sounds like good experience. You can always finish it up outside of class. If the project is relevant to your career goals, you'd be able to point out to potential employers how you went above and beyond to polish it up.
As disappointed as I was, it really was a great project. Probably the most fun I have ever had with programming. I was really worried that our game was going to be incredibly basic compared to all the other projects but it ended up being pretty much on par with everything else. It was a lot of fun to see what everyone else came up with during their presentations too.

My major is in Computer Science and my school does offer a game design track but I opted for a more general track called Computer Science Foundations. I'm interested in game design, but I'd have to go back and take two physics courses to get into the classes, and I have already filled my natural science requirements for my degree so going back to take those would just be extra money spent on top.
 

Kinitari

Black Canada Mafia
I might be interviewing for a job in around a week that has 'DotNetNuke' as a nice-to-have skill. I was wondering if there was anything I could read about it so that I could talk to the interviewer about it in some fashion - everything else on the desired skills list (including the other nice to haves) I have in spades, so I am feeling reasonably good about the job.
 
Haha, I'd rather have pie. :( Is VBA pretty easy to learn I guess? Doesn't seem too bad once you get used to the reserved words.

Easy to learn. It will take a while to forget, though. Some things you just can't unsee.

Unless things have recently changed, VBA is still basically based on pre-.NET VB. When I was in school purusing my IS degree, we learned VB6, so transitioning to VBA was seamless.
 

pompidu

Member
Need ya guys again!
Basically im reading a txt file and making each contact and adding them into the contact list but I have no idea how to properly sort the by name in a doublylinked list.
This is where I am trying to add each contact sorted by name. The teacher gives really bad examples on how to sort and them add them to the doubly linked list. There is some uncompleted code but I cant figure out how to sort then add them.
Code:
public class ContactList 
{
	private DoublyLinkedList<Contact> cList = new DoublyLinkedList<Contact>();
	int count = 0;
	//Temporary variables
	String nameFirst, nameLast, nameMiddle, month, cellNum, homeNum, email, addr, city, state,
    bday, zip;
    
	public ContactList(String file)
	{
		//Open the data file
        Scanner myFileIn = null;
        try
        {
            myFileIn= new Scanner(new File(file));
        } catch (FileNotFoundException e)
            {
                System.out.println("File: "+ file +" is not found");
            }
        
        //Read how many lines
        int numRecords = myFileIn.nextInt();
        //Read the file
        for (int i = 0; i < numRecords; i++)
        {
	        nameLast = myFileIn.next();
	        nameFirst = myFileIn.next();
	        nameMiddle = myFileIn.next();
	        month = myFileIn.next();
	        bday = myFileIn.next();
	        cellNum = myFileIn.next();
	        homeNum = myFileIn.next();
	        email = myFileIn.next();
	        addr = myFileIn.next();
	        city = myFileIn.next();
	        state = myFileIn.next();
	        zip = myFileIn.next();
	        Contact contactName = new Contact(nameLast, nameFirst, nameMiddle, month, bday, 
	        		cellNum, homeNum, email, addr, city, state,zip);
	        insert(contactName);
	        
        }
        
	}
	

	public void insert(Contact c)
	{
			
		cList.resetCurrentElement();
			while (cList.isEmpty())
			{
				cList.insertAt(c, count);
				count++;
			}
			while(cList.hasMoreElements())
			{
				if (cList.nextElement().compareTo(c) > 0)
				{
					cList.insertAt(c, count);
				}
				else if (cList.nextElement().compareTo(c) < 0)
				{
					count++;
					cList.insertAt(c, count);
				}
			}
		  
		}

Here is the compareTo method in the contact class
Code:
public int compareTo(Contact other)
	{
		String fullName = getFirstName() + getLastName();
		String otherName = other.getFirstName() + other.getLastName();
		return fullName.compareTo(otherName);
	}
 

pompidu

Member
So the program works, and you're left with a DoublyLinkedList<Contact> that needs to be sorted? In that case, the code in DoublyLinkedList is the only one that matters.

The problem is, it only takes the first contact and thats ,idk how to compare the firstNode and the next and then add them in the proper order.
 

Aeris130

Member
The problem is, it only takes the first contact and thats ,idk how to compare the firstNode and the next and then add them in the proper order.

Is the DoublyList your own code? Does it bug out and only take one element? How does the list look inside, is it [element <-> element <-> element <-> ...], "<->" representing a link in both directions?
 
The problem is, it only takes the first contact and thats ,idk how to compare the firstNode and the next and then add them in the proper order.

Depends on your assignment, but typically you would not do it that way. You would just add all your elements into the list then when you are done, call a "sort" method.

With this method just have your value object implement the Comparable interface and then anything that gets added to the list can be sorted generically using the same method and compareTo.
 

pompidu

Member
Is the DoublyList your own code? Does it bug out and only take one element? How does the list look inside, is it [element <-> element <-> element <-> ...], "<->" representing a link in both directions?

Doublylist code is not mine, it is the professors. I can add the first contact (there is 12 in the txt file). Any contact after the first has to be in ascending order. im suppose to use the compareTo method in my contact list. if its returns 1, the current element will be in the list after the previous element, -1 goes before the previous element. The problem is he tells what to do for only half the project and you left on your own to figure out on your own (google, books, etc.) He doesn't really teach you how to do it.

Depends on your assignment, but typically you would not do it that way. You would just add all your elements into the list then when you are done, call a "sort" method.

With this method just have your value object implement the Comparable interface and then anything that gets added to the list can be sorted generically using the same method and compareTo.

I will try doing it that way again, it couldn't get it to work that way either lol.
 

Aeris130

Member
Just do a simple bubble sort then when the list is filled:

Code:
sorted = true
do {
check every element n and compare it to (n + 1) // Special case for the last item, nothing to compare it with.
if (n + 1) < n, switch places. sorted = false 
} while !sorted

This will repeat until the algorithm manages to go through the entire list without switching items, whereby sorted will remain true and the loop will end. Done.
 

pompidu

Member
Just do a simple bubble sort then when the list is filled:

Code:
sorted = true
do {
check every element n and compare it to (n + 1) // Special case for the last item, nothing to compare it with.
if (n + 1) < n, switch places. sorted = false 
} while !sorted

This will repeat until the algorithm manages to go through the entire list without switching items, whereby sorted will remain true and the loop will end. Done.

I think I might just try that. Il get back to you
 

pompidu

Member
Went back to his instructions and I have to sort each contact name as it gets added to the list, I can't fill the list first and then sort, has to be sorted while its getting each contact. grr
 

Aeris130

Member
Upon insertion:

Code:
c = insertion contact
Step through every element n and compare it to c until c < n
Insert c at n's place // I'm assuming the list is implemented to push n forward one position
If you reach the end of the list, insert c at the end (aka the beginning if the list is empty)
 

pompidu

Member
Upon insertion:

Code:
c = insertion contact
Step through every element n and compare it to c until c < n
Insert c at n's place // I'm assuming the list is implemented to push n forward one position
If you reach the end of the list, insert c at the end (aka the beginning if the list is empty)

Eclipse keeps stalling on me, gonna restart my computer but does something like this look right?
Edited code, it was infitely looping
Code:
public void insert(Contact c)
	{
		cList.resetCurrentElement();
		if (cList.isEmpty())
		{
			cList.insertAt(c, count);
		}
		else
			while (cList.hasMoreElements())
			{
				if (cList.nextElement().compareTo(c) == -1)
				{
					cList.insertAt(c, count + 1);
					count++;
				}
				else if (cList.nextElement().compareTo(c) == 1)
				{
					cList.insertAt(c, count);
					count++;
				}
				else
				{
					cList.insertAt(c, count);
					count++;
				}
			}
	}
 

Aeris130

Member
Eclipse keeps stalling on me, gonna restart my computer but does something like this look right?
Edited code, it was infitely looping
Code:
public void insert(Contact c)
	{
		cList.resetCurrentElement();
		if (cList.isEmpty())
		{
			cList.insertAt(c, count);
		}
		else
			while (cList.hasMoreElements())
			{
				if (cList.nextElement().compareTo(c) == -1)
				{
					cList.insertAt(c, count + 1);
					count++;
				}
				else if (cList.nextElement().compareTo(c) == 1)
				{
					cList.insertAt(c, count);
					count++;
				}
				else
				{
					cList.insertAt(c, count);
					count++;
				}
			}
	}

Code:
if (cList.nextElement().compareTo(c) == -1)
shouldn't be there. Let's say you're inserting "5" into a list [1,2,3,4,6,7,8]. Your first compairson will be to compare 1 (cList.nextElement) with 5 (c). 1 < 5, so compareTo will return -1 and 5 will be inserted between 1 and 2.

Only check for the first instance of an element that is higher than the one you're inserting (in this case 6), then insert before that element. Assuming every element has been inserted that way, the list remains sorted.
 

pompidu

Member
Code:
if (cList.nextElement().compareTo(c) == -1)
shouldn't be there. Let's say you're inserting "5" into a list [1,2,3,4,6,7,8]. Your first compairson will be to compare 1 (cList.nextElement) with 5 (c). 1 < 5, so compareTo will return -1 and 5 will be inserted between 1 and 2.

Only check for the first instance of an element that is higher than the one you're inserting (in this case 6), then insert before that element. Assuming every element has been inserted that way, the list remains sorted.

I see what your saying. You have been a great help. I'm gonaa try to do that if i can lol.
 

pompidu

Member
I officialy give up lol. I dont know how to tell which string is bigger than the other. Hopefully my teacher will actually help with this.
 

pompidu

Member
Surely there's some sort of 'length' or 'size' method?

I should clarify, not bigger in the sense of size but what comes first alphabetically. I need to compare the concatenation of a last name and first name of two objects. so if ones name starts with "a" and the other "b", the first in the list will be the one with the "a". so for however many I have, the have to be sorted as I build the objects in a doublylinkedlist. so if the first one starts with "v" and the second starts with "a". "a" has to go first, then "v" second. Then the next object has to be sorted between those 2 and so on. My professor did not show us how to do, only adding already sorted objects into a doublylinkedlist.
 
I should clarify, not bigger in the sense of size but what comes first alphabetically. I need to compare the concatenation of a last name and first name of two objects. so if ones name starts with "a" and the other "b", the first in the list will be the one with the "a". so for however many I have, the have to be sorted as I build the objects in a doublylinkedlist. so if the first one starts with "v" and the second starts with "a". "a" has to go first, then "v" second. Then the next object has to be sorted between those 2 and so on. My professor did not show us how to do, only adding already sorted objects into a doublylinkedlist.

I remember doing this in java but I forget what we did exactly. I think we turned the letters to numbers and then had it loop and move it up the list if the number was smaller than the number it was comparing to. I can't remember exactly though.

Edit: I think it was compare to that will return an integer and you can then use that to move it? What language are you using? CompareTo doesn't work?

Edit 2: Or compare with charAt?
 

pompidu

Member
I remember doing this in java but I forget what we did exactly. I think we turned the letters to numbers and then had it loop and move it up the list if the number was smaller than the number it was comparing to. I can't remember exactly though.

Edit: I think it was compare to that will return an integer and you can then use that to move it? What language are you using? CompareTo doesn't work?

Edit 2: Or compare with charAt?

Java yeah. Have to use CompareTo. I'm just having trouble doing this all dynamically. The constructor reads each contact, and as it reads each one (its in a for loop so I can all the contacts) it has to be sorted after each one. so the first contact has to be sorted, then the second has to be sorted with the first. Then the third has to be sorted between the first and second one, and so on. This all has to be sorted in a doublylinkedlist, which my professor did not describe very well. I can do it fine if the list is already full but we have to do it as it reads each contact.
 
Java yeah. Have to use CompareTo. I'm just having trouble doing this all dynamically. The constructor reads each contact, and as it reads each one (its in a for loop so I can all the contacts) it has to be sorted after each one. so the first contact has to be sorted, then the second has to be sorted with the first. Then the third has to be sorted between the first and second one, and so on. This all has to be sorted in a doublylinkedlist, which my professor did not describe very well. I can do it fine if the list is already full but we have to do it as it reads each contact.

Ok, I don't think I ever did doubly linked lists so that might be what's throwing me off.
 

pompidu

Member
Ok, I don't think I ever did doubly linked lists so that might be what's throwing me off.
Yeah its really annoying since there is new shit we have to move onto and theres no time to explain the stuff. Our school decided it would be better to cut 12 hours out of each class for the smemester so they can add more classes without changing cirriculum. so basically 1/3 of the cirriculum is dumped on everyone with 3 weeks left.
 
Hi Gaf. I have a question about programming in PHP and I was wondering if I could get some help.

I'm supposed to be writing a program that takes in pairs of words and uses them to edit. Like, if I get the string pair "bab bar" then I read some other piece of text and replace every instance of bab with bar. I might have to do this over a paragraph and I get a couple of pairs that I need to do, going through the paragraph one pair at a time.

So what I'm trying to set up is first a for-loop that goes over each pair of switches I'm supposed to make, followed by a while-loop that says "While $find is in the paragraph, replace is with $replace." It's been working pretty well so far, but not in every case and I'm wondering if there's an easy fix or perhaps and easier way to do what I'm trying to do.

The first two cases are replacing one word, no spaces with another. The problem doesn't start until I try a case with a space in it and I'm wondering if perhaps that is part of the problem. Here's the code, so maybe you guys can help.

$numRules is the number of rule pairs.
$find is the array of words to replace.
$replace is the array of words to replace with.
$string is the paragraph that is being edited.

Code:
...
for( $count = 0; $count < $numRules; $count++){
     while( strpos( $string, $find[$count]) !== false){
          $string = str_replace($find[$count], $replace[$count],$string);
     }
}
print $string

Thanks so much for the help. PHP is a little new to me, but I think my program should work completely if I can solve this one problem.
 

squidyj

Member
I should clarify, not bigger in the sense of size but what comes first alphabetically. I need to compare the concatenation of a last name and first name of two objects. so if ones name starts with "a" and the other "b", the first in the list will be the one with the "a". so for however many I have, the have to be sorted as I build the objects in a doublylinkedlist. so if the first one starts with "v" and the second starts with "a". "a" has to go first, then "v" second. Then the next object has to be sorted between those 2 and so on. My professor did not show us how to do, only adding already sorted objects into a doublylinkedlist.

wasn't generating that rank just what you did in your last assignment?

but the easiest way would be to write a function that looks at each character in order until one string runs out or you reach a difference

Fuck Prolog.

Hard.

I'm so glad my semester is over =D. I kinda liked prolog though. The only thing that bothered me was infinite loop on failure sometimes

prolog: "what if I..."?

NO PROLOG, IT'S NOT GOING TO WORK.
 

squidyj

Member
Need ya guys again!
Basically im reading a txt file and making each contact and adding them into the contact list but I have no idea how to properly sort the by name in a doublylinked list.
This is where I am trying to add each contact sorted by name. The teacher gives really bad examples on how to sort and them add them to the doubly linked list. There is some uncompleted code but I cant figure out how to sort then add them.

Is count the location of the current element or is it the number of elements in the doublylinkedlist? It seems you are treating it as both.
what does the code for doublylinkedlist look like? Are there any other methods available to you?

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

Since the compareTo method in your Contact is invoking the String compareTo method it is providing the relative alphabetical order of the two strings.
then for a simple implementation all you need to do for each insertion is walk up to where it needs to be placed.

so, consider the entry you want to add C, and the entry you are currently comparing it to X.

as long as C < X you want to keep going (i'm assuming this is ascending order)
as soon as you find an X >= C you want to insert C just before X
if you reach the condition that there are no elements after X you know that C is the largest element and must be placed after X.

cList.resetCurrentElement();
while(cList.hasNextElement() && (cList.nextElement().compareTo(c) < 0))
{}
if(cList.hasNextElement())
//insert before the current element
else
//insert after
 

Jokab

Member
@pompidu

The way you do sorting in Java is you run Collections.sort() over a Collection (which LinkedList<T> is, and therefore DoublyLinkedList<T> as well). Collections.sort takes two arguments, first the list to be sorted and second a Comparator<Object> (where Object is replaced with the type you want to compare, I guess String?). You will need to implement your own Comparator. So how is that done?

Well, you'll need to make a new class implementing the interface Comparable. Doing so requires you to implement the method compareTo(Object o1, Object o2) (again, replacing Object in both cases with String), which Collections.sort() will use to do the sorting.

compareTo() has the following specification:
  • If o1 is "larger" (this can mean anything depending in what you're trying to achieve), then return 1
  • If o2 is "larger" than o1, return -1
  • If they're "the same" (which in this case means they're the same word), return 0.

When you've completed the Comparator class, you just go:
Code:
Collections.sort(listToBeSorted, new StringAlphabeticalComparator());

Collections.sort() doesn't return anything, it modifies the list passed as an argument directly. So that's how you use compareTo, if you need help with the actual comparing then just ask (if you've asked about that above I apologize, didn't have time to read it all).

So since you'll have to do the sorting dynamically, you just run sort every time you add a new element to the list.
 

hateradio

The Most Dangerous Yes Man
Code:
...
for( $count = 0; $count < $numRules; $count++){
     while( strpos( $string, $find[$count]) !== false){
          $string = str_replace($find[$count], $replace[$count],$string);
     }
}
print $string
Not sure I entirely understood what you said, but here's what I got from it.

  1. Receive a word pair
  2. In a paragraph replace the first word with the second one

Why do you need to go through the paragraph a pair at a time? str_replace takes arrays as pairs, so you could probably use that.

Code:
//Eg word pairs: "foo bar", "abc xyz"


// Sub arrays for the words to replace and the replacements
$myAry = array(array(), array());

//Splits the string, put the first word in the first array and the second to the second array.
//(stuff)

//Two arrays that contain the words to be replaced and their replacements.
// $myAry[0] has array('foo', 'abc');
// $myAry[1] has array('bar', 'xyz');

//Then
str_replace($myAry[0], $myAry[1], $paragraph);
 
No joke, I freaked out about this yesterday before we presented. I went back and changed two splash screens that had the misspelling and fixed it in the comments that described the project in the code. I rely on spellcheck way too much sometimes >_>

As disappointed as I was, it really was a great project. Probably the most fun I have ever had with programming. I was really worried that our game was going to be incredibly basic compared to all the other projects but it ended up being pretty much on par with everything else. It was a lot of fun to see what everyone else came up with during their presentations too.

My major is in Computer Science and my school does offer a game design track but I opted for a more general track called Computer Science Foundations. I'm interested in game design, but I'd have to go back and take two physics courses to get into the classes, and I have already filled my natural science requirements for my degree so going back to take those would just be extra money spent on top.

It will never be good enough for certain people i have the same thing. Ooh shit i could have done that better, dam me why did i do that. Fuck i know how to optimize it i just need two more days. :p

Good choice. From what i heard on gaming side game design is like the thing you go for if you can't be bothered to put in the effort to learn photoshop,3D tools or programming.
Atleast from what i discovered at my uni the game design course will net you zero for landing a job.

Maybe the one at your uni is better because of the physics courses or is it more like a game engine design course?
 

squidyj

Member
@pompidu

The way you do sorting in Java is you run Collections.sort() over a Collection (which LinkedList<T> is, and therefore DoublyLinkedList<T> as well). Collections.sort takes two arguments, first the list to be sorted and second a Comparator<Object> (where Object is replaced with the type you want to compare, I guess String?). You will need to implement your own Comparator. So how is that done?

Well, you'll need to make a new class implementing the interface Comparable. Doing so requires you to implement the method compareTo(Object o1, Object o2) (again, replacing Object in both cases with String), which Collections.sort() will use to do the sorting.

compareTo() has the following specification:
  • If o1 is "larger" (this can mean anything depending in what you're trying to achieve), then return 1
  • If o2 is "larger" than o1, return -1
  • If they're "the same" (which in this case means they're the same word), return 0.

When you've completed the Comparator class, you just go:
Code:
Collections.sort(listToBeSorted, new StringAlphabeticalComparator());

Collections.sort() doesn't return anything, it modifies the list passed as an argument directly. So that's how you use compareTo, if you need help with the actual comparing then just ask (if you've asked about that above I apologize, didn't have time to read it all).

So since you'll have to do the sorting dynamically, you just run sort every time you add a new element to the list.

why would DoublyLinkedList necessarily inherit from List? is it not allowed to just have it's own implementation under java? all I can really tell is that it implements a compareTo method which suggests it's implementing the Comparable interface.
 

pompidu

Member
Thanks for your help guys! I really appreciate it!
The only way for insertion for doubliylinkedlist is list.insertAt(object, location).
So I did this, and while it does actually sort correctly I'm getting repeat contacts and missing.
Code:
public void insert(Contact c)
	{
		int count = 0;
		cList.resetCurrentElement();
		while (cList.hasMoreElements())
		{
			if (cList.nextElement().compareTo(c) > 0)
			{
				cList.insertAt(c, count);
			}
			else 
			{
				count++;
			}
		}
		
			System.out.println(cList);
	}
 
Top Bottom