home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1699
Discussions: 188,402, Posts: 2,243,609, Members: 232,632
Old April 23rd, 2005, 01:45 AM   Digg it!   #1 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
C++ Sort

Whats wrong with my code for this sorting of arrays?


Code:
#include <iostream.h>
#include <stdlib.h>

  int i, j;

int main()
{
      int temp;


      //INT THE ARRAY
      int x[] = { 28, 87, -3, 45, 19 };


      //fIND THE SIZE OF ARRAY
      int z;
      z = sizeof(x) / sizeof(x[0]);


      //PRINT OUT BEFORE SORT
       for (int p = 0; p < z; p++) {
                cout << x[p] << " ";

                                }

                               cout << endl;



       //Sort


       for (i = 0;i <(z - 2); i++) {
       for (j = (i + 1); j <( z - 1); j++)

           if(x[i] > x[j]){
              temp = x[i];
              x[i] = x[j];
              x[j] = temp;}


                  }


                 cout << "Swapped" << endl;
           for (int q = 0; q < z; q++) {
                cout << x[q] << " " ;
                                }

      system("PAUSE");
      return 0;
}
__________________
For the Emperor, there were the Jedi. For the Evil Empire of Microsoft, there is a penguin.

redfoxstorm is offline   Reply With Quote
Old April 23rd, 2005, 02:14 AM     #2 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
Nevermind I figured it out!

My problem was with j < (z -1), which should have been j < z.


I feel stupid now

redfoxstorm is offline   Reply With Quote
Old April 23rd, 2005, 03:58 PM     #3 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
If I wanted to use the same code down there but with a function and array pointers how would I do that?

I've wrote some code, but something is wrong:

Code:
#include <iostream.h>
#include <stdlib.h>

void sortx(int *px, int z);


int main()
{

       int pause;
      //INT THE ARRAY
      int x[] = { 28, 87, -3, 45, 19 };
      int *px;

      px = x;

      //fIND THE SIZE OF ARRAY
      int z;
      z = sizeof(x) / sizeof(x[0]);


      //PRINT OUT BEFORE SORT
       for (int f = 0; f < z; f++) {
                cout << px[f] << " ";

                                }

                               cout << endl;


      //CALL FUNCTION
      px += 0;
      sortx(px ,z);

         

//PRINT OUT AFTER SORT FUCNTION

 for (int f = 0; f < z; f++) {
                cout << px[f] << " ";}










   cin >> pause;
      return 0;
}
    


//Fucntion sortx ==============================================================

void sortx(int *px, int z){

       //Sort



       int temp;

       for (int i = 0;i <(z-1); i++) {
       for (int j = (i + 1); j < z; j++){

           if( (px += i) > (px += j)) {
              temp = (px +=i);
              (px +=i) = (px += j);
              (px += j) = temp;}






               } }

                 


                  }

redfoxstorm is offline   Reply With Quote
Old April 23rd, 2005, 06:06 PM     #4 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
Gaahhh!!! why all the spaces? there's like 5 times as many carriage returns as lines of code! the madness!!!

Anyways, what is it doing? as far as the parameter passing it looks fine. BTW, does the sizeof(x) / sizeof(x[0]) actually work? that seems fishy to me, but then again i've never tried it.
originel is offline   Reply With Quote
Old April 23rd, 2005, 06:14 PM     #5 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
yeah, that works, but I can't get it to compile.....my teacher has me doing the find the size of the array with the z = sizeof(x) / sizeof(x[0]);

I have to pass just one element of the array to the function....which I thought I was doing..and then pass the size of the array.

My function then has to check each element and put it in order from least to greatest...but I keep getting unray errors...

If the function is suppose to be void, how can it return a value?
redfoxstorm is offline   Reply With Quote
Old April 23rd, 2005, 06:16 PM     #6 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
I usually add those white spaces in there while I'm working, i usuallget rid of them when i turn it in.
redfoxstorm is offline   Reply With Quote
Old April 23rd, 2005, 07:30 PM     #7 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
hehe, yeah those spaces makes reading it really hard.

As for how to return a parameter with a void function...you're already doing it

It all has to do with passing parameters to functions. There are two ways to pass a variable to a function: by value and by reference. These really are just descriptive names though, there isn't exactly any special syntax that specifies this...persay. This is one of the more complex issues with C++, so I'm going to diverge and talk about what is called pointers.

You have already used pointers some in your code above, whether or not you were told they were called pointers. A pointer is a variable (like an int or a char) except that it only contains one type of value, a memory address. Now there are actually different types of pointers though...a pointer has to point to a certain type of data. So you have an integer pointer, a character pointer, etc. You define an int pointer by doing:

int* myPointer;

You've already done this so you already have some experience with pointers. A couple of notes about pointers: you can increment and decrement pointers. This is actually how arrays work...the number in the [] is actually an offset to add to a pointer. So myArray[6] takes the pointer to myArray (always the first element) and adds 6 to it. BTW, this is why I thought your size thing looked wierd...I would have guess it would have taken the size of the pointer and divided it by the size of 1 array element (the answer would have been 0). Another thing to keep in mind is that when you increment a pointer it actually doesn't add 1 to it...it adds the size of one element to it. So if you have an integer on a Windows NT platform and a pointer pointing to it, when you do myPointer++ it will actually add 4 to it because the size of an integer is 4 bytes in Windows NT (i.e. 32bits). Pointers work on a 1 byte system.

You should read more on pointers if you get the chance, because they are one of the single most important features in C++ and one of the most vital to understanding the language.

So I said earlier that there are two ways to pass a variable to a function: by reference and by value. I actually don't like this notation because it is kinda confusing IMO, but I'll explain it anyways so you'll know what others say when they mention it. When you pass a function a value, it always sends the value of the variable. When it does this, it actually copies it to a new variable. So when you're in your function, there are actually two variables in the program: one in the function you called and one in the function you did the calling from. So if you change the value in one function it doesn't affect the variable in another.

Now enter pointers. Instead of passing a normal variable, you can pass a pointer instead. You can do all the same stuff with pointers that you can with a regular variable, although the notation can be different. At this point in time an example will help:
Code:
#include<iostream>
using namespace std;

void foo(int&);

void main()
{
	int myInt;

	myInt = 5;

	cout<<myInt<<endl;
	foo(myInt);
	cout<<myInt<<endl;
}

void foo(int &myInt2)
{
	myInt2 = 7;
}
What this bit of code does is the same thing as before, except you see the '&' in front of the variable. What this does is instead of passing the value of the variable, it passes a pointer to it. Since a pointer never changes (the variable doesn't randomly move around), it doesn't matter if the value is copied over. This is what is called passing a value by reference, because we are "referencing" the variable by sending a pointer or "reference" to it. The pointer in function foo points to the variable in the main function, so any changes made through the pointer affect the variable in the main function.

This is how you "return" a value with a void function. Although it's not exactly the same as returning a value though since the value in function main has already been changed before function foo returns.

One thing to note about this notation is that you don't always do &myVar to pass by reference. What this needs is simply a pointer and some variable types are pointers by default. An array is a pointer by default, so to pass an array by reference would look like this:
Code:
#include<iostream>
using namespace std;

void foo(int&);

void main()
{
	int myArray[10];
	foo(myArray);
}

void foo(int* myArray)
{
	for(int i = 0; i < 10; i++)
               myArray[i] = i;
}
This does the same thing as before, the array is properly returned to the main function, but notice how the notation is different. This is still called passing by reference and is why i don't like that nomenclature. Passing by pointer would be a better term.
originel is offline   Reply With Quote
Old April 23rd, 2005, 08:45 PM     #8 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
New Code

Here is my new code base on your instructions and my teachers. I'm really confused now.

I've only got it down to 2 errors. they are:

51 c:\docume~1\jacob\mydocu~1\mycoll~1\myc__~1\prog18 \prog18.cpp
parse error before `='

53 c:\docume~1\jacob\mydocu~1\mycoll~1\myc__~1\prog18 \prog18.cpp
assignment to `int *' from `int' lacks a cast


Code:
#include <iostream.h>
#include <stdlib.h>
using namespace std;

void sortx(int* &p, int z);

     int temp;
     int i;
     int j;

int main()
{

       int pause;
       //INT THE ARRAY
       int x[] = { 28, 87, -3, 45, 19 };
       int *p;

       p = &x[0];

       //fIND THE SIZE OF ARRAY
       int z;
       z = sizeof(x) / sizeof(x[0]);

       //PRINT OUT BEFORE SORT
       for (int f = 0; f < z; f++) {
       cout << p[f] << " ";
       cout; }


       //CALL FUNCTION
       sortx(p ,z);

       //PRINT OUT AFTER SORT FUCNTION
       for (int f = 0; f < z; f++) {
       cout << p[f] << " ";}

       system("PAUSE");
       return 0;
       }

void sortx(int* &p, int z){
 
for ( i = 0;i <(z-1); i++) {
for ( j = (i + 1); j < z; j++) {

if( (p += i) > (p += j)) {
   temp = (p + = i); // error line 51<--------------------------------------------------
    (p +=i) = (p += j);
    (p += j) = temp; // error line 53 <-------------------------------------------------

    }}}}
redfoxstorm is offline   Reply With Quote
Old April 23rd, 2005, 08:53 PM     #9 (permalink)
Ultimate Member
 
meese's Avatar
 
Join Date: Jun 2003
Location: NJ
Posts: 2,467
Quote:
temp = (p + = i); // error line 51<--------------------------------------------------
there is a space between the + and the = sign, is that what you want?
meese is offline   Reply With Quote
Old April 23rd, 2005, 09:00 PM     #10 (permalink)
Ultimate Member
 
meese's Avatar
 
Join Date: Jun 2003
Location: NJ
Posts: 2,467
Also are there enough right brackets on the last line?

edit: I think your are ok with the brackets

Last edited by meese : April 23rd, 2005 at 09:02 PM.
meese is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a bucket sort please. dm06040 Webmastering and Programming 3 April 19th, 2005 03:20 PM
Computer powering-up....sort of. Duxdude Technical Support 1 July 14th, 2003 03:49 AM
Stealthing a floppy...sort of BeeQuewl PC Modification 4 June 21st, 2003 04:57 AM
i know nothing of the sort.... jp22382 Mobile Computing 2 November 20th, 2002 02:36 AM
More logo help...sort of fatal xception Graphic Design and Digital Photography 1 October 9th, 2002 12:09 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Misery Loves Company... (2144)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Futronix has water features? (0)
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:30 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28