Author Topic: C++ help  (Read 2407 times)

Offline GoGeTa006

  • Member
  • Posts: 6863
  • The fate of destruction is also the joy of Rebirth
    • Anime Planet listing
C++ help
« on: July 21, 2009, 02:09:09 AM »
so im doing this thing. . .its a program that asks the user for "how many tests do you want to input?"
then using arrays it stores all the scores.
afterwards you need to output the scores in descending order. . .and I just cant get this done!
help plz!
:)
Quote
#include <iostream>
#include <iomanip>
using namespace std;


void sort(double *arry[], int);  <== function prototype
double dropped(double[], int);

int main()
(click to show/hide)

sort(scores, numtest); <==== function call


   cout << fixed << showpoint << setprecision(1);
   cout << "****************************" << endl;
   cout << "Arranged test scores: " << endl;
   for (count = 0; count < numtest; count++)
      cout << scores[count] << endl;
   cout << "****************************" << endl;
   cout << "Average test score: " << average << endl;
   cout << "****************************" << endl;

}
Quote
void sort(double *arry[], int size) <===== function definition
{
   int startscan;
   int maxindex;
   double *maxelem;
   for (startscan = 0; startscan < size; startscan++)
   {
      maxindex = startscan;
      maxelem = arry[startscan];
      for(int index = startscan + 1; index < size; index++)
      {
         if (*(arry[index]) > *maxelem)
         {
            maxelem = arry[index];
            maxindex = index;
         }
      }
      arry[maxindex] = arry[startscan];
      arry[startscan] = maxelem;

   }

I get error C2664: 'sort' : cannot convert parameter 1 from 'double *' to 'double *[]


im supposed to use pointers.
I already tried the following:


-change in the function prototype
from
void sort(double *arry[], int)
to
void sort(double *[], int);

and its driving me nuts!



ignore anything that goes with the "lowest" . . .thats something else and it already works fine.

;D
« Last Edit: July 21, 2009, 04:11:15 AM by GoGeTa006 »

Offline wolkec

  • Member
  • Posts: 833
Re: C++ help
« Reply #1 on: July 21, 2009, 06:27:28 AM »
fuction call sort and function definition sort isnt the same
the call should have an array

sort(scores[],numtest);
« Last Edit: July 21, 2009, 06:31:07 AM by wolkec »

Offline Pzc

  • Member
  • Posts: 780
Re: C++ help
« Reply #2 on: July 21, 2009, 11:04:43 AM »
Since you allocate the array with new methinks
it should look like this:

Code: [Select]
void sort(double *arry, int);  <== function prototype

void sort(double *arry, int size) <===== function definition

You should also delete everything you new:
Code: [Select]
...
   cout << "Average test score: " << average << endl;
   cout << "****************************" << endl;
   delete [] scores;
}
  A casual stroll through a lunatic asylum shows that faith does not prove anything. -- Friedrich Nietzsche

Offline xShadow

  • Member
  • Posts: 1502
  • No
Re: C++ help
« Reply #3 on: July 21, 2009, 03:19:41 PM »
No, the function prototype is fine, it matches up.

I'm no pro at C++ (I took a C class), but it looks like you have the variables you're using initialized incorrectly. Basically, your function is made to take in a double pointer, because you're basically saying this:

*scores[], which indicates that you want a pointer to an array of doubles called scores. In the C language in general,
scores[] basically means you want to have [] number of memory addresses that all have a double in them. So, what your function WANTS to take in is a pointer to an array of pointers that point to numbers stored in their memory addresses (I took the class a few months ago, so someone correct me if I'm wrong, because I hated C and all of its damned manual dynamic memory allocation).

So, if you have scores[], then you just go *scores, it's basically going to access the first number in your array. That's the first pointer. Then, if you do *(scores+1), it's going to increment to the next pointer and then access the number that's inside of it.

Basically, with the way you've got that function set up, if you've been seeing where this is going, you've basically got to do a double pointer to access what's inside:

**scores will get the first one in what your function is taking in.

... Well, all that aside, I think all you have to do to fix it is take out the * in front of the double your function has initialized. By doing scores[], that is already a pointer; maybe your teacher said to use pointers, and you're trying to make it extra obvious, but it's completely unnecessary there. Maybe you already knew this all and just forgot to take it away by accident (I know C has some infernally hard to spot problems sometimes). Make sure you also take out any pointer-dependent elements within the function (*maxele or whatever it is).


Oh, and from a glance at your function, it looks like you're basically keeping track of what the largest number in the array is and then sticking it at the beginning (incrementing where the beginning is by one), and then sticking the smallest one you find at the end (and the end is going to be one less each time). That's fine, but I don't suggest doing it to the original array. Maybe it's my ol' paranoid programmer senses tingling, but I feel like numbers are going to get left out if you start killing the numbers in the old array. Also, there are a few other quirks I'd address here and there if I were you, but I'm fairly certain you'll find out about them sooner or later, unless I'm just too sleepy (and I am exhausted and about to go to sleep right now, so that's not too improbable). <.<;

Hope this helps.

Edit: Oh, forgot to mention, if you do go and remove the  *'s, you won't be able to modify your target array within the original function, which may have been your goal (though again I don't suggest it), so you'll have to make an output and a variable for it.. Just a heads up. Sleep time.
« Last Edit: July 21, 2009, 03:53:49 PM by xShadow »

Cute, huh?

Offline Pzc

  • Member
  • Posts: 780
Re: C++ help
« Reply #4 on: July 22, 2009, 12:59:08 AM »
No, the function prototype is fine, it matches up.
Well, no, they actually don't match up.

When using **scores or *scores[] you're dealing
with an array of pointers to array's of doubles.

Does the definition of main look familiar ?
Code: [Select]
int main( int argc, char *argv[] )
  or
int main( int argc, char **argv )
**argv is an array with pointers to null-terminated
c-type strings. They're allocated in two steps, first
the pointer array and then the arrays they're pointing
to.

*scores[], which indicates that you want a pointer to an array of doubles called scores. In the C language in general,
scores[] basically means you want to have [] number of memory addresses that all have a double in them. So, what your function WANTS to take in is a pointer to an array of pointers that point to numbers stored in their memory addresses (I took the class a few months ago, so someone correct me if I'm wrong, because I hated C and all of its damned manual dynamic memory allocation).
Don't really understand what you mean here but it
seems wrong ;)  Doing *scores[] isn't getting you
anything useful since what's stored there is a value
and not a pointer. Only 'scores'  is the address to
the array, '*scores' is the value of the first element.
'*scores[]' is wrong at any rate.

So, if you have scores[], then you just go *scores, it's basically going to access the first number in your array. That's the first pointer. Then, if you do *(scores+1), it's going to increment to the next pointer and then access the number that's inside of it.
Correct. scores[0] and *scores  is equivalent.
So are scores[1] and *(scores+1).

Edit: Oh, forgot to mention, if you do go and remove the  *'s, you won't be able to modify your target array within the original function, which may have been your goal (though again I don't suggest it), so you'll have to make an output and a variable for it.. Just a heads up. Sleep time.
Incorrect. It's pointers that allow values to be sent
to be modified since they're not just passed by value.

Modified code that compiles but obviously don't
work since the function dropped() was missing
and there might be logical errors for you to hunt
down:
(click to show/hide)

Pointers are a little bothersome and easy to get
wrong if you're not careful and maybe I've made
a mistake myself up there somewhere but I do
know for a fact that double *arr[] is not a
pointer to an array of doubles.
  A casual stroll through a lunatic asylum shows that faith does not prove anything. -- Friedrich Nietzsche

Offline xShadow

  • Member
  • Posts: 1502
  • No
Re: C++ help
« Reply #5 on: July 22, 2009, 04:59:34 AM »
Quote
Well, no, they actually don't match up.

When using **scores or *scores[] you're dealing
with an array of pointers to array's of doubles.

Does the definition of main look familiar ?
Code:

int main( int argc, char *argv[] )
  or
int main( int argc, char **argv )

**argv is an array with pointers to null-terminated
c-type strings. They're allocated in two steps, first
the pointer array and then the arrays they're pointing
to.

I don't know what the hell you're getting at here, but those are generic main arguments when you're dealing with programs that are meant to run in the console. Those tell the console that it needs to take in some things. I'm not sure on the specifics, but put simply his program wants to take in things and the sort them. Those arguments are to tell the console it wants inputs from the users. Maybe not 100% right here, but I don't know what you're getting at.

As for his prototypes, they match up. Tell me where they don't. His "main" function isn't the one that's doing the sorting, and that's not where the problem is at. In case you didn't notice (obviously you didn't). Look harder before you speak.

Quote
Don't really understand what you mean here but it
seems wrong Wink  Doing *scores[] isn't getting you
anything useful since what's stored there is a value
and not a pointer. Only 'scores'  is the address to
the array, '*scores' is the value of the first element.
'*scores[]' is wrong at any rate.

I'm pretty damn sure I made it simple enough for people with any experience to understand, but I guess that's not true. *scores[] would access scores at whatever value you put in the brackets. What I was saying is that scores itself is a double pointer. The first pointer points to the first pointer to the first value in the array "scores". Seriously, what the hell? >_>

Quote
Incorrect. It's pointers that allow values to be sent
to be modified since they're not just passed by value.

Modified code that compiles but obviously don't
work since the function dropped() was missing
and there might be logical errors for you to hunt

Umm... No. It's not incorrect. When you input just the array -> arr[]
like that, the entire array will get copied for the function to use.. in another place in memory. In other words, when the function actually MODIFIES it, it'll have to output something to modify the ORIGINAL. This is how it works. Now, if you take in *arr[], you're just taking in the memory address to an array. When that happens, while the memory address does get copied to another location, it's still pointing to the same place, allowing you to modify the original array. I believe that this is right, and you can test it yourself by making a function like that. Up to you.

Quote
Pointers are a little bothersome and easy to get
wrong if you're not careful and maybe I've made
a mistake myself up there somewhere but I do
know for a fact that double *arr[] is not a
pointer to an array of doubles.

No, you're the one that's wrong. Try actually doing *arr[] and then tell me what arr is, and then tell me what *arr is. I can tell you right now, though.

*arr is a going to be the address of the first value in your array. arr itself is going to be the address that POINTS to the first pointer in your array. When you take *arr[], it'll be whatever the value is at the location in brackets, thus the same as going **arr.


Edit: Maybe it's confusing because I use "memory address" and "pointer" interchangeably... though I don't see what the problem with that is. A pointer is the memory address of a place in memory. That's all it is. C probably has to deal with this much more extensively than C++, because if I remember correctly C++ does not require manual dynamic memory allocation, whereas C does, so we had to know everything about how pointers worked.

Edit2: Actually, I was playing another game, and I thought about it again... I'm not 100% sure if putting a bracket array into a function will or will not let you modify the original array. It all depends on whether or not the memory itself is copied or just the pointers to them are copied. I think that's all I had to correct...
« Last Edit: July 22, 2009, 06:45:19 AM by xShadow »

Cute, huh?

Offline GoGeTa006

  • Member
  • Posts: 6863
  • The fate of destruction is also the joy of Rebirth
    • Anime Planet listing
Re: C++ help
« Reply #6 on: July 22, 2009, 07:10:29 AM »
well. . .I added the brackets into the statement:

sort(scores[], numtest);

and it did compile. . .but it didnt do shit. . .it didnt sort them at all. . .and im pretty sure the "sort" function is well written to actually sort them (ive tried it alone)

the "error" the compiler keeps pointing out is that on that specific line

sort(scores, numtest);

error C2664: 'sort' : cannot convert parameter 1 from 'double *' to 'double *[]'

Offline xShadow

  • Member
  • Posts: 1502
  • No
Re: C++ help
« Reply #7 on: July 22, 2009, 10:06:17 AM »
I'm not going to go too far into how well the sort function works, but your basic problem is that the thing you're putting in is a regular pointer, while your function takes in double pointers.

In other words, here:

void sort(double *arry[], int size)
should become
double *sort(double arry[], int size)

(remember to change your prototype, too)
then,

double *maxelem;
v
double maxelem;

and

if (*(arry[index]) > *maxelem)
to
if (arry[index] > maxelem)

also add:

return arry;

Somewhere at the top, introduce:

double sorted[];
(click to show/hide)

Then, change the call to:

sorted=sort(scores, numtest);

Finally, make sure to change what you're printing in the console out to sorted, not scores.

The reason it's pointing it out on that line is because your function treats what it's supposed to take in just fine, but you're putting the wrong stuff into it, thus the problem is at the call, not the function. Save another copy of your current function and then try my fix. Tell me if you get any errors.
« Last Edit: July 22, 2009, 10:08:23 AM by xShadow »

Cute, huh?

Offline Fr0steh

  • Member
  • Posts: 44
Re: C++ help
« Reply #8 on: July 22, 2009, 10:49:16 AM »
call scores like this
sort(&scores, numtest);

want me to explain why? no, right?  :P

also in your sort function there are a lot of memory leaks because basically u have a pointer to an array, so everytime you access the array you have to derefence it.

ex:
maxelem = &(*arry)[startscan]; //&because maxelem is a pointer

by doing this:
maxelem = arry[startscan]; //(wrong!)
you are accessing an unallocated position in memory.

i don't know what the dropped function is so I commented it (best solution for everything  : D)
Quote
//this works
#include <iostream>
#include <iomanip>
using namespace std;


void sort(double *arry[], int);
//double dropped(double[], int);

int main()
{
   double *scores;

   double total = 0.0, average;
   int numtest;
   int count;
   double lowest;

   cout << "How many test scores will be entered? " << endl;
   cin >> numtest;

   scores = new double[numtest];



   cout << "Enter the grade for the tests: " << endl;
   for (count = 0; count < numtest; count++)
   {
      cout << "Score no. " << (count + 1) << " :" << endl;
      cin >> scores[count];
      while (scores[count] <= 0)
      {
         cout << "no negatives!!!" << endl;
         cin >> scores[count];
      }


     
   }



   for (count = 0; count < numtest; count++)
   {
      total += scores[count];
   }
lowest = 0;// dropped(scores, numtest);


   average = (total - lowest) / numtest;

sort(&scores, numtest);


   cout << fixed << showpoint << setprecision(1);
   cout << "****************************" << endl;
   cout << "Arranged test scores: " << endl;
   for (count = 0; count < numtest; count++)
      cout << scores[count] << endl;
   cout << "****************************" << endl;
   cout << "Average test score: " << average << endl;
   cout << "****************************" << endl;

}
void sort(double *arry[], int size)
{
   int startscan;
   int maxindex;
   double *maxelem;
   for (startscan = 0; startscan < size; startscan++)
   {
      maxindex = startscan;
      maxelem = &(*arry)[startscan];
      for(int index = startscan + 1; index < size; index++)
      {
         if ((*arry)[index] > *maxelem)
         {
            maxelem = &(*arry)[index];
            maxindex = index;
         }
      }
      (*arry)[maxindex] = (*arry)[startscan];
      (*arry)[startscan] = *maxelem;

   }
}
« Last Edit: July 22, 2009, 11:11:48 AM by Fr0steh »

Offline Pzc

  • Member
  • Posts: 780
Re: C++ help
« Reply #9 on: July 22, 2009, 12:14:14 PM »
Ok, since we're obviously not talking about
the same things lets start with this:

Code: [Select]
#include <iostream>
int main( int argc, char **argv)
{
  int *array = new int[10];    // <-- that gets you an array with 10 ints
  array[0] = 1;  // first value
  array[1] = 2;  // second value
  std::cout << "Address to first element: " << array << std::endl;
  std::cout << "First: " << array[0] << "  second: " << *(array + 1) << std::endl;
  std::cout << " *array[0] " << *array[0] << std::endl;
  delete [] array;  // clean up
}
This won't even compile due to the error:
1>c:\documents and settings\my documents\visual studio 2008\projects\bkttst\bkttst\bkttst.cpp(9) : error C2100: illegal indirection
Line 9 is the one with *array[0].
Comment out that line and the output:
Code: [Select]
Address to first element: 00345AA0
First: 1  second: 2

So can you now at last give up on *double[] ?

Here's the somewhat unrelated reason to why
I brought up the declaration of main and when
such syntax is used:
(click to show/hide)

And while I'm at it why not poke a hole at your
array-gets-copied-and-can't-be-modified statement:
Code: [Select]
#include <iostream>

void modifier1(double a);   // pass by value
void modifier2(double *a); // pass by pointer

int main( int argc, char **argv)
{
double *scores = new double[10];
scores[0] = 1.0f;
scores[1] = 2.0f;

std::cout << "scores[0]: " << scores[0] << "  scores[1]: " << scores[1] << std::endl;
std::cout << "modifier1( scores[0] )";
modifier1( scores[0] );
std::cout << "..done   scores[0]: " << scores[0] << "  scores[1]: " << scores[1] << std::endl;
std::cout << "modifier2( scores )";
modifier2( scores );
std::cout << "..done   scores[0]: " << scores[0] << "   scores[1]: " << scores[1] << std::endl;

delete [] scores;
}

void modifier1(double a)
{
a = 7;
}

void modifier2(double *a)
{
a[0] = 5.0f;
a[1] = 75.0f;
}

Output ? This, of course:
Code: [Select]
scores[0]: 1  scores[1]: 2
modifier1( scores[0] )..done   scores[0]: 1  scores[1]: 2
modifier2( scores )..done   scores[0]: 5   scores[1]: 75

Please, do test your stuff before you start
giving advice if you're not sure you're right.
&(*arry)[startscan] ? wut ?
  A casual stroll through a lunatic asylum shows that faith does not prove anything. -- Friedrich Nietzsche

Offline xShadow

  • Member
  • Posts: 1502
  • No
Re: C++ help
« Reply #10 on: July 22, 2009, 01:32:30 PM »
Quote
This won't even compile due to the error:
1>c:\documents and settings\my documents\visual studio 2008\projects\bkttst\bkttst\bkttst.cpp(9) : error C2100: illegal indirection
Line 9 is the one with *array[0].
Comment out that line and the output:

...Well, yeah, no shit sherlock, you put a double pointer in for a variable that only had one pointer in the first place. Of course it won't compile. What's your point? I'm guessing you're totally missing what I'm saying? Like for the past 2-3 times now?

I was just establishing that arr[] is the same as an *arr. It's simply that *arr does not have a defined number of memory locations set aside to it, which can be fixed (at least in c) by something like:

double *arr= double *malloc(5);
That sets aside room for 5 doubles in a modifiable area of the memory.
You can then proceed to go
arr[0]= 41.41;
Or whatever the hell you want.

Also, indexing with double pointers should be something like this:

arr[0][0]
is equivalent to
**arr
and,
arr[1][0]
is equivalent to
*(arr[1])
or
**(arr+1)
and
arr[1][1]
is the same as
*(*(arr+1)+1)
or
*(arr[1]+1)

I'm honestly quite rusty with my programming, so you can go test it out for me, if you wish.

Quote
So can you now at last give up on *double[] ?

Here's the somewhat unrelated reason to why
I brought up the declaration of main and when
such syntax is used:

... Wtf? Give up on it? What the hell are you talking about? Unless C++ is just VASTLY different from C, I don't see where you're taking the *double[] argument to?

Moreover... his main function isn't even where the problem is. What the hell are you getting at?

I looked over your console function that takes inputs in from the user, and I don't see what point you're trying to make. You do realize that any NORMAL char declaration in C can only have one letter by itself? A mere word is already an array. For the first level of your character array's pointers, you put in space for 5 character pointers. Then, you proceeded to give each of those character pointers their own array. In essence, this is a double pointer. I believe you're just proving exactly what I said, so I don't see what your point is.

You do **mystrings after the user input and you'll just get out the letter h, which makes sense, because mystrings is an array of pointers to arrays of pointers which point to letters, and you're pretty much saying mystrings[0][0].

Quote
And while I'm at it why not poke a hole at your
array-gets-copied-and-can't-be-modified statement:

Poke a hole in? If you look at edit 2 at the bottom of my post, you'll see that I realized that it may just copy addresses and thus make it modifiable, and I said that. Thanks for making sure, though. I've personally never had a situation where I've had to modify an array like that, but that's because most of the stuff I was dealing with were dynamically allocated data structures like binary trees, nary trees, and linked lists. Something as simple as modifying a numerical array isn't a part of my (recent) experience.

Quote
Please, do test your stuff before you start
giving advice if you're not sure you're right.

... No, I don't really feel like opening Visual Studio to test something like this.

I mean, I don't know where the hell you're headed with YOUR argument (nowhere?), but the topic creator's original problem (which I am addressing) was that he was sticking in a pointer into a function that took a double pointer. For that branch, everything I said makes sense. For the corrections I suggested in my post, the a few can be cut off now that I know that arrays can be modified inside of the function. That's about it. On a side note, though, C++ has some nice convenient little things. Lol @ the "delete" and "new" commands. In C, this would have been more of a pain.

However, I have to applaud this:

Quote
call scores like this
sort(&scores, numtest);

That's brilliant. I totally forgot about the & (address) operator. That should mostly fix your stuff, Gogeta. There might be a few errors inside the actual function, but this should fix the error you're getting right now.
« Last Edit: July 22, 2009, 01:58:07 PM by xShadow »

Cute, huh?

Offline Fr0steh

  • Member
  • Posts: 44
Re: C++ help
« Reply #11 on: July 22, 2009, 02:37:43 PM »
&(*arry)[startscan] ? wut ?

that's simply because you need to deference arry ("(*arry)"), get the position of the array given by startscan ([starscan]) and return a pointer to that position (&).

You can also do  ::)
(*arry) + startscan*sizeof(double);   



btw, my code works on visual studio...         
« Last Edit: July 22, 2009, 02:44:22 PM by Fr0steh »

Offline Pzc

  • Member
  • Posts: 780
Re: C++ help
« Reply #12 on: July 22, 2009, 04:46:21 PM »
Quote
This won't even compile due to the error:
1>c:\documents and settings\my documents\visual studio 2008\projects\bkttst\bkttst\bkttst.cpp(9) : error C2100: illegal indirection
Line 9 is the one with *array[0].
Comment out that line and the output:

...Well, yeah, no shit sherlock, you put a double pointer in for a variable that only had one pointer in the first place. Of course it won't compile. What's your point? I'm guessing you're totally missing what I'm saying? Like for the past 2-3 times now?
Good, we agree on that one then. So, next. Where
in the OP's code do you see any pointer to pointers ?
I aint missing what you write but you're all over the
place. And I do hope you're not confusing the type
double (double precision floating point) with what many
call a double pointer, pointer-to-pointer (double **g) ?

I was just establishing that arr[] is the same as an *arr. It's simply that *arr does not have a defined number of memory locations set aside to it, which can be fixed (at least in c) by something like:

double *arr= double *malloc(5);
That sets aside room for 5 doubles in a modifiable area of the memory.
You can then proceed to go
arr[0]= 41.41;
Or whatever the hell you want.
No, C-arrays (double[]) is not the same as
the one allocated with new/malloc (double *).
C-arrays can decay to pointers but not the
other way. First link on google just for you.
Maybe not always very important for 1D arrays
but extremely important for 2+D arrays.

Also, indexing with double pointers should be something like this:

arr[0][0]
is equivalent to
**arr
and,
arr[1][0]
is equivalent to
*(arr[1])
or
**(arr+1)
and
arr[1][1]
is the same as
*(*(arr+1)+1)
or
*(arr[1]+1)

I'm honestly quite rusty with my programming, so you can go test it out for me, if you wish.
No need for testing, really. You can only use the
syntax arr[x1][y2] if you have declared arr like arr[10][10].
Not when you allocate it with new/malloc.

I mean, I don't know where the hell you're headed with YOUR argument (nowhere?), but the topic creator's original problem (which I am addressing) was that he was sticking in a pointer into a function that took a double pointer. For that branch, everything I said makes sense. For the corrections I suggested in my post, the a few can be cut off now that I know that arrays can be modified inside of the function. That's about it. On a side note, though, C++ has some nice convenient little things. Lol @ the "delete" and "new" commands. In C, this would have been more of a pain.
new and delete are essential to c++ since they
do things malloc and free don't.

However, I have to applaud this:

Quote
call scores like this
sort(&scores, numtest);

That's brilliant. I totally forgot about the & (address) operator. That should mostly fix your stuff, Gogeta. There might be a few errors inside the actual function, but this should fix the error you're getting right now.
That's not needed here.. sure, you can do that but
it only adds indirection as you create a pointer to
a pointer, useless in the OP's post since (s)he's only
allocating a 1D array.

&(*arry)[startscan] ? wut ?

that's simply because you need to deference arry ("(*arry)"), get the position of the array given by startscan ([starscan]) and return a pointer to that position (&).

You can also do  ::)
(*arry) + startscan*sizeof(double);   



btw, my code works on visual studio...         
Ah, yes, now I get what you mean. On the other hand
if you would've treated the 1D array as a 1D array and
not pass the pointer-to-the-pointer-to-the-array and
just went with the pointer-to-the-array there's less
indirection. But yeah, it works I suppose. But do fear
my double ************arrpointer   ;)

As for the rest, I give up. You can add as many
&'s, *'s and []'s as you want, I don't care any more.
Thread is far enough off-topic by now anyways.
  A casual stroll through a lunatic asylum shows that faith does not prove anything. -- Friedrich Nietzsche

Offline Fr0steh

  • Member
  • Posts: 44
Re: C++ help
« Reply #13 on: July 22, 2009, 05:08:38 PM »
I didn't want to change the function parameters because assuming that this is a educational exercise probably using a pointer to an array is needed. And yes passing simply the array which is a pointer by itself would suffice : )
And also if you want to make even more changes :D you didnt need to do a sort function, simply get everything in a vector and use the sort function from the algorithm library :p

And to the OP, if u're TL;DR XD simply use the code on my post, it works and only changes 2 things.

Offline xShadow

  • Member
  • Posts: 1502
  • No
Re: C++ help
« Reply #14 on: July 22, 2009, 07:25:35 PM »
Quote
No, C-arrays (double[]) is not the same as
the one allocated with new/malloc (double *).
C-arrays can decay to pointers but not the
other way. First link on google just for you.
Maybe not always very important for 1D arrays
but extremely important for 2+D arrays.

He's not doing a 2+D array, and what do you mean "the other way"? The article you're linking to admits that as far as the compiler is concerned they are treated the same way in functions. I know full well that *char and char[] are different. One is a modifiable entity while the other is placed in a subsection of memory that you can't directly access. However, you can still use *char for either case to access either one, just as you can use char[0] to access either one.

Quote
Good, we agree on that one then. So, next. Where
in the OP's code do you see any pointer to pointers ?
I aint missing what you write but you're all over the
place. And I do hope you're not confusing the type
double (double precision floating point) with what many
call a double pointer, pointer-to-pointer (double **g) ?

Look at the change Frosteh made and then tell me you don't understand what my point was.

Here, look:

double *scores;
This is what he's putting into his sorting function. It's just a regular pointer, meaning he intends to have the user fill in what else is needed.

Then, this is his sorting function prototype:
void sort(double *arry[], int size)

Now look at the error he had. Makes some sense, maybe?

Notice the *arry[]? Now you can be all picky and say that's not actually a double pointer and blablablah, but his problem is that he was just putting a regular array (which is on the level of a single pointer) into a function that asked for a pointer to an array. Frosteh fixed this by changing it so the function call took up the address of scores[], thus pretty much putting in an extra pointer where there was none before, just for the function to use.

Quote
That's not needed here.. sure, you can do that but
it only adds indirection as you create a pointer to
a pointer, useless in the OP's post since (s)he's only
allocating a 1D array.

...???? Read above.

Quote
As for the rest, I give up. You can add as many
&'s, *'s and []'s as you want, I don't care any more.
Thread is far enough off-topic by now anyways.

Who's the one that derailed it? I'm pretty sure it wasn't me, as my first post (which you misunderstood multiple times and them proceeded to argue with me over) was made entirely with the intention of helping him.

Quote
I didn't want to change the function parameters because assuming that this is a educational exercise probably using a pointer to an array is needed. And yes passing simply the array which is a pointer by itself would suffice : )
And also if you want to make even more changes Cheesy you didnt need to do a sort function, simply get everything in a vector and use the sort function from the algorithm library :p

And to the OP, if u're TL;DR XD simply use the code on my post, it works and only changes 2 things.

Did you test it with every vector imaginable though? I mean, it should work, but uhh... I don't know, something about it just makes me a bit skeptical. >_>

Anyways, your fix is pretty simple. I tend to like to just get rid of pointers where they aren't needed (which is what my fix suggested), to make things less complex.

Cute, huh?

Offline Fr0steh

  • Member
  • Posts: 44
Re: C++ help
« Reply #15 on: July 22, 2009, 10:56:51 PM »
hey! pointers are teh r0x when you know how to use them! they just dont make coffee XD

edit: oh wait! they do make coffee if for some reason the pointer is accessed by a coffee machine!! xD
« Last Edit: July 22, 2009, 11:10:32 PM by Fr0steh »

Offline Slykester

  • Member
  • Posts: 564
  • 待ってられない未来がある。。。( ゚Д゚) ハァ?
Re: C++ help
« Reply #16 on: July 22, 2009, 11:42:17 PM »
Your best starting point, and the best advice you have been given is

(click to show/hide)

Yes, definitely that ;) I take it this is for an assignment?

Offline ant900

  • Box Fansubs
  • Member
  • Posts: 815
  • "Saito! Give me that gun Now!!"
Re: C++ help
« Reply #17 on: July 23, 2009, 01:48:00 AM »
Deleting memory does tend to be a good thing.

Also that bubble sort is pretty ugly IMO, you should try using a faster algorithm or clean that up.

Offline Proin Drakenzol

  • Member
  • Posts: 2296
  • Tiny Dragon Powers of Doom!
Re: C++ help
« Reply #18 on: July 23, 2009, 08:50:34 AM »
iirc


void sort(double array[], int num)

should work.


it's been about five years since I last used C++, though. I used Java in college and haven't programmed at all since joining the Navy.

The linear nature of your Euclidean geometry both confounds and befuddles me.

Offline xShadow

  • Member
  • Posts: 1502
  • No
Re: C++ help
« Reply #19 on: July 23, 2009, 12:41:13 PM »
Yeah, but you have to modify a few variables in the actual function if you're gonna go that route. I outlined that in the post with the correction I suggested.

To be honest, though, I just now noticed that me and Pzc essentially told him to do the same thing at the beginning, but apparently people misunderstand each other on the internetz very easily. >_>

Cute, huh?