April 23rd, 2005, 01:45 AM
|
#1 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
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.
|
| |
April 23rd, 2005, 02:14 AM
|
#2 (permalink)
| | Member
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  |
| |
April 23rd, 2005, 03:58 PM
|
#3 (permalink)
| | Member
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;}
} }
} |
| |
April 23rd, 2005, 06:06 PM
|
#4 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
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. |
| |
April 23rd, 2005, 06:14 PM
|
#5 (permalink)
| | Member
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? |
| |
April 23rd, 2005, 06:16 PM
|
#6 (permalink)
| | Member
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. |
| |
April 23rd, 2005, 07:30 PM
|
#7 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
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. |
| |
April 23rd, 2005, 08:45 PM
|
#8 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
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 <-------------------------------------------------
}}}} |
| |
April 23rd, 2005, 08:53 PM
|
#9 (permalink)
| | Ultimate Member
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? |
| |
April 23rd, 2005, 09:00 PM
|
#10 (permalink)
| | Ultimate Member
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.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |