When you're going to be using a set value for something all throughout a piece of code it's better to define it and use the definition. That way you'd have #define ARRAYSIZE 5 at the top and wherever you were gonna specify the array size like: int[5] you'd instead only need to put int[ARRAYSIZE]. Makes the code a <b>lot</b> easier to tweak in the long run =3
I got teh professor to give me lenience on having it in late, seeing as what she told me to do when I asked for help was completely wrong, heres my finished code, all in POINTER NOTATION.... not array notation...
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1--> // Program by Aaron Friedley
<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I got teh professor to give me lenience on having it in late, seeing as what she told me to do when I asked for help was completely wrong,<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> Good to hear <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
Let's have a look...
Ay! You aren't <span style='font-family:Courier'>delete</span>ing your <span style='font-family:Courier'>new</span>'d memory!
<span style='font-family:Courier'>return 1</span>, as far as I know, is a code for an error having occured. Unless you really need it, don't bother with returning anything in <span style='font-family:Courier'>main</span>.
Haven't checked the whole algorithm through, but it looks good <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
<!--QuoteBegin-SoulSkorpion+Mar 10 2004, 09:37 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Mar 10 2004, 09:37 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <span style='font-family:Courier'>return 1</span>, as far as I know, is a code for an error having occured. Unless you really need it, don't bother with returning anything in <span style='font-family:Courier'>main</span>. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> On the contrary, you should return 0 on success, or any other number on failure. Thats how scripting works on UNIX and in Windows .bat files, they catch the errorcode.
In batch files it goes something like this:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->echo 1. This echo 2. That echo 3. The other thing
REM This is th program we run to get the users input and return it as an error choice
IF ERRORLEVEL 1 GOTO X IF ERRORLEVEL 2 GOTO Y IF ERRORLEVEL 3 GOTO Z
This is relatively unimportant for a Windows environment, but scripts are used much more heavily in any UNIX environment. Heres an example of a bash script that detects weather a program failed or succeded:
<!--QuoteBegin-SkulkBait+Mar 11 2004, 11:33 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SkulkBait @ Mar 11 2004, 11:33 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-SoulSkorpion+Mar 10 2004, 09:37 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Mar 10 2004, 09:37 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <span style='font-family:Courier'>return 1</span>, as far as I know, is a code for an error having occured. Unless you really need it, don't bother with returning anything in <span style='font-family:Courier'>main</span>. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> On the contrary, you should return 0 on success, or any other number on failure. Thats how scripting works on UNIX and in Windows .bat files, they catch the errorcode. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd--> When I said "don't bother returning anything", I meant "because C does it for you" <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo-->
- never, never, never use prototypes in functions in the real world. Yuk. - comment your code liberally. For example: <span style='font-family:courier'><!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1--> // swap pAscending j and j - 1 temp = *(pAscending+j); *(pAscending+j) = *(pAscending+(j-1)); *(pAscending+(j-1)) = temp; <!--c2--></td></tr></table><div class='postcolor'><!--ec2--></span> - check out the web for bubblesort algorithms. Bubblesort is very innefficient, but see how they're done anyway. You can achieve the same result in a lot less code. - control those array sizes. In the real world, hard coding values can and do cause big problems if not controlled carefully. - array[i] is the same as array + i. I see pointers and arrays as interchangeable. - check your memory allocations. new can return 0 if it wants to. Dereferencing 0 == bang. - as SoulSkorpion says, clean up your memory allocations. Remember to use delete[] and not delete because you're using new[].
<!--QuoteBegin-SoulSkorpion+Mar 10 2004, 10:49 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Mar 10 2004, 10:49 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-SkulkBait+Mar 11 2004, 11:33 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SkulkBait @ Mar 11 2004, 11:33 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-SoulSkorpion+Mar 10 2004, 09:37 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Mar 10 2004, 09:37 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <span style='font-family:Courier'>return 1</span>, as far as I know, is a code for an error having occured. Unless you really need it, don't bother with returning anything in <span style='font-family:Courier'>main</span>. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> On the contrary, you should return 0 on success, or any other number on failure. Thats how scripting works on UNIX and in Windows .bat files, they catch the errorcode. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> When I said "don't bother returning anything", I meant "because C does it for you" <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd--> Yes, and what I meant was "You should do it, even if you don't really need it"
Comments
When you're going to be using a set value for something all throughout a piece of code it's better to define it and use the definition.
That way you'd have #define ARRAYSIZE 5 at the top and wherever you were gonna specify the array size like: int[5] you'd instead only need to put int[ARRAYSIZE].
Makes the code a <b>lot</b> easier to tweak in the long run =3
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// Program by Aaron Friedley
#include <time.h>
#include <iostream.h>
#include <math.h>
int main (void)
{
// Local Declaration
int array[5];
int **pAscending = new int*[5];
int **pDescending = new int*[5];
int i;
// Prototype Declaration
void generatearray(int[]);
void sortascend (int[], int**);
void sortdescend (int[], int**);
void displayarray (int[], int**, int**);
generatearray(array);
for(i = 0; i<5;i++)
{
*(pAscending +i) = &array[i];
*(pDescending+i) = &array[i];
}
sortascend (array, pAscending);
sortdescend (array, pDescending);
displayarray (array, pAscending, pDescending);
return 1;
}
// *** generatearray ***
void generatearray (int array[])
{
int i = 0;
srand(clock());
for (i = 0; i < 5; i++)
array[i] = (rand()%98+1); // for
} // generatearray
// *** sortascend ***
void sortascend (int array[], int **pAscending)
{
int i = 0;
void bubbleup (int**, int);
for (i = 0; i < 5; i++)
bubbleup (pAscending, i); // for
} // sortascend
void bubbleup (int **pAscending, int i)
{
int j;
int *temp;
for (j = 4; j > i; j--)
{
if ((**(pAscending + j)) < (**(pAscending+(j-1))))
{
temp = *(pAscending+j);
*(pAscending+j) = *(pAscending+(j-1));
*(pAscending+(j-1)) = temp;
} // if
} // for
}
// *** sortdescend ***
void sortdescend (int array[], int **pDescending)
{
int i = 0;
void bubbledown (int**, int);
for (i = 0; i < 5; i++)
bubbledown (pDescending, i); // for
} // sortdescend
// *** bubbledown ***
void bubbledown (int **pDescending, int i)
{
int j;
int *temp;
for (j = 4; j > i; j--)
{
if ((**(pDescending + j)) > (**(pDescending+(j-1))))
{
temp = *(pDescending+j);
*(pDescending+j) = *(pDescending+(j-1));
*(pDescending+(j-1)) = temp;
} // if
} // for
} // bubbledown
// *** displayarray ***
void displayarray (int array[], int **pAscending, int **pDescending)
{
int i;
cout << "Program by Aaron Friedley" << endl << "Ascending" <<"\tOriginal"
<< "\tDescending" << endl;
for (i = 0; i < 5; i++)
{
cout << **(pAscending+i) << "\t\t" << array[i] << "\t\t" << **(pDescending+i)
<< endl;
} // for
} // displayarray
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Good to hear <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
Let's have a look...
Ay! You aren't <span style='font-family:Courier'>delete</span>ing your <span style='font-family:Courier'>new</span>'d memory!
<span style='font-family:Courier'>return 1</span>, as far as I know, is a code for an error having occured. Unless you really need it, don't bother with returning anything in <span style='font-family:Courier'>main</span>.
Haven't checked the whole algorithm through, but it looks good <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
On the contrary, you should return 0 on success, or any other number on failure. Thats how scripting works on UNIX and in Windows .bat files, they catch the errorcode.
In batch files it goes something like this:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->echo 1. This
echo 2. That
echo 3. The other thing
REM This is th program we run to get the users input and return it as an error
choice
IF ERRORLEVEL 1 GOTO X
IF ERRORLEVEL 2 GOTO Y
IF ERRORLEVEL 3 GOTO Z
X:
Y:
Z:
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
This is relatively unimportant for a Windows environment, but scripts are used much more heavily in any UNIX environment. Heres an example of a bash script that detects weather a program failed or succeded:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
#!/bin/bash
/bin/dostuff
if [ $? eq 0 ]; then
echo "Program succeded!"
else
echo "Program failed!!"
fi
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
This is much more typical then getting user input and is often used in buildscripts and such.
<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
On the contrary, you should return 0 on success, or any other number on failure. Thats how scripting works on UNIX and in Windows .bat files, they catch the errorcode. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
When I said "don't bother returning anything", I meant "because C does it for you" <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo-->
Few comments:
- never, never, never use prototypes in functions in the real world. Yuk.
- comment your code liberally. For example:
<span style='font-family:courier'><!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// swap pAscending j and j - 1
temp = *(pAscending+j);
*(pAscending+j) = *(pAscending+(j-1));
*(pAscending+(j-1)) = temp;
<!--c2--></td></tr></table><div class='postcolor'><!--ec2--></span>
- check out the web for bubblesort algorithms. Bubblesort is very innefficient, but see how they're done anyway. You can achieve the same result in a lot less code.
- control those array sizes. In the real world, hard coding values can and do cause big problems if not controlled carefully.
- array[i] is the same as array + i. I see pointers and arrays as interchangeable.
- check your memory allocations. new can return 0 if it wants to. Dereferencing 0 == bang.
- as SoulSkorpion says, clean up your memory allocations. Remember to use delete[] and not delete because you're using new[].
<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
On the contrary, you should return 0 on success, or any other number on failure. Thats how scripting works on UNIX and in Windows .bat files, they catch the errorcode. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
When I said "don't bother returning anything", I meant "because C does it for you" <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Yes, and what I meant was "You should do it, even if you don't really need it"