Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 483

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 379

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 379

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/njssjfu/includes/bbcode.php on line 112
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3828: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:483)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3830: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:483)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3831: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:483)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3832: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:483)
Action Replay Central :: View topic - PR0GRAMMING! C++

Action Replay Central

Official Forums of ARCentral.net
IRC:cookie.sorcery.net #ARCentral
* FAQ   * Rules   * Search   * Members   * The Staff
* Login
It is currently Tue Sep 19, 2017 2:05 pm

PR0GRAMMING! C++


Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Mon Dec 08, 2008 1:55 am   Post subject: Re: PR0GRAMMING! C++
Silver Member
User avatar

Joined: Thu Jan 13, 2005 3:59 am
Posts: 3822
Location: Noitacol
Haha, thanks. ^_^ Tis no problem, glad to help.

The "exit" instruction will end a program's execution, btw. :)


Sorry, I never saw your post on Oct 16 up there ^ back there <
I would have helped with that too if I had..
_________________
Image
Image
Image
Top Profile WWW
PostPosted: Tue Dec 09, 2008 3:38 pm   Post subject: Re: PR0GRAMMING! C++
Poeta's Protector
User avatar

Joined: Thu Oct 05, 2006 10:23 pm
Posts: 1246
James, on my final we have to know Bubble Sorts and Binary Searches. K, not in our lesson content and he told us to look online, which annoys me because we shouldn't be tested on crap we weren't taught.

I don't understand what the heck a bubble sort is, let alone how to code it. I'm seeing all this stuff online where people are using a function and naming it 'bubblesort' or something similar.

In my text it looks like a jarbled mess of letters:

Image

Code:
void print(float[],int);
void sort(float[],int);

int main()
{
   float a[] = (55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7);
   print (a,8);
   sort (a,8);
   print (a,8);
}

void sort (float a[], int n)
{
   for (int i = 1; i < n; i++)
   // bubble up max { a (0..n-i)}:
      for (int j = 0; j < n-i; j++)
      if (a[j] > a[j+1]) swap (a[j], a[j+1]);
   // INVARIANT: a[n-1-i...n-1] is sorted
}

This whole thing confuses me. :( My instructor is an ***hole.
Top Profile
PostPosted: Tue Dec 09, 2008 4:35 pm   Post subject: Re: PR0GRAMMING! C++
Silver Member
User avatar

Joined: Thu Jan 13, 2005 3:59 am
Posts: 3822
Location: Noitacol
No wonder you're confused, that sort algorithm is backwards... It's sorting by "sink big number to the bottom" rather than "rise small number to the top" like bubble sort normally is..

Is that the code he gave you?

Try to look at that code as "push big numbers to the bottom" rather than what you're book says, which is just confusing.. I'll try to explain what's going on.

This is the same code, written cleanly with comments and helpful variable names

void sort (float a[], int height)      //a=array of floats, height=length of that array 
{ //top element is index=0, bottom is index=height-1
for (int floor = 1; floor < height; floor++) //This outer loop acts as a floor. When the floor rises, you don't
{ //look under it because items under it will already be sorted.
for (int j = 0; j < height-floor; j++) //this loop will take all of the items above the floor and compare them top to bottom.
{ // See how it stops at array length minus the floor value? The first time through,
// the floor value is a one. Array length minus one is the last index of your array. So it's the whole list!
// Next time through, it'll be two. Array length minus two is all of the array except
// the last element which should be the biggest element at that point!
if (a[j] > a[j+1]) // if the element higher in the array (that is, closer to index 0) is bigger than the one under it,
swap (a[j], a[j+1]); //swap them so the bigger element is lower in the list (closer to the last index in the array)
// as the inner for loop continues it will keep pushing that same big element down as far
// as it will go. That might be hard to visualize, I'll try to explain more if you need me to.
} //first time through, the biggest element is now at the bottom. Raise the floor and push the next biggest item down
} //as far as it will go. Raise the floor, repeat until there is only one item above the floor, the smallest item will be at index 0. :)
}
_________________
Image
Image
Image
Top Profile WWW
PostPosted: Thu Dec 11, 2008 2:17 pm   Post subject: Re: PR0GRAMMING! C++
Poeta's Protector
User avatar

Joined: Thu Oct 05, 2006 10:23 pm
Posts: 1246
void sort (float a[], int height)      // Can this also be used if say you were dealing with user inputs and not defined array values?  If so
// would you put this before or after the 'main ()'? After, wouldn't it? Because you need the inputs
// before it can know what to sort.
{
for (int floor = 1; floor < height; floor++)
{
for (int j = 0; j < height-floor; j++) // So this loop, once the above loop executes, is what begins to sort the list?
{
if (a[j] > a[j+1])
swap (a[j], a[j+1]); // and this 'swap' is what places it where it needs to go?
}
}
}


Haha, I'm completely helpless sometimes. :P
Top Profile
PostPosted: Thu Dec 11, 2008 7:28 pm   Post subject: Re: PR0GRAMMING! C++
Silver Member
User avatar

Joined: Thu Jan 13, 2005 3:59 am
Posts: 3822
Location: Noitacol
1) I'll get back to ya on that.

2) Yes.

3) Yes, but it only moves one index at a time. The biggest one gets all the way to the bottom the first time through the inner loop. The outer loop raises the floor to block the biggest from moving, then the inner loop moves the second biggest to the "new bottom". rinse, repeat :)

1) And back to the first part:
sort() is a function just like main() is a function, Functions can be called at any time. Main is special because it's always the first one to be called and it controls when and what other functions get called. The location in the .cpp file doesn't matter. :)

In order for main to call sort, it must know what sort is. The only way it will know is if it sees that it's there before it tries to call sort. These are the only two ways that will happen:

Quote:
#include <iostream>

using namespace std;

void sort(float a[], int height); //function prototype. This says there is a function called "sort" that will be defined later

int main()
{
//do stuff and call sort
}

void sort(float a[], int height)
{
//sort definition
}

Quote:
#include <iostream>

using namespace std;

void sort(float a[], int height) //function declaration
{
//sort definition
}

int main()
{
//do stuff and call sort
}


The function sort requires you to use an array like that. So your main function will read in or set the values for the array and then pass it to sort. :)
_________________
Image
Image
Image
Top Profile WWW
PostPosted: Thu Dec 11, 2008 9:32 pm   Post subject: Re: PR0GRAMMING! C++
Poeta's Protector
User avatar

Joined: Thu Oct 05, 2006 10:23 pm
Posts: 1246
OMG! I get it now!! You are awesome! Image Thank you so much! I'm going to test it out and see if I can get it to work with a few different scenarios. I'll post up what I do. ^__^
Top Profile
PostPosted: Thu Dec 11, 2008 11:19 pm   Post subject: Re: PR0GRAMMING! C++
Silver Member
User avatar

Joined: Thu Jan 13, 2005 3:59 am
Posts: 3822
Location: Noitacol
Good deal! ^_^ You're quite welcome; I'm glad I could help. :)
_________________
Image
Image
Image
Top Profile WWW
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group