Discussion Forums > Technology
C++ help
xShadow:
--- 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.
--- End quote ---
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.
--- End quote ---
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
--- End quote ---
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.
--- End quote ---
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...
GoGeTa006:
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 *[]'
xShadow:
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)^Name is actually up to you. This is to prevent any possible conflict with memory you're not allowed to access by default; this is a problem in C when you introduce something like
char *damn="What the fuck."
because you can't actually modify "damn" after your first declaration, because it's in a section of memory that you're not allowed into.
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.
Fr0steh:
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;
}
}
--- End quote ---
Pzc:
Ok, since we're obviously not talking about
the same things lets start with this:
--- Code: ---
#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
}
--- End code ---
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: ---
Address to first element: 00345AA0
First: 1 second: 2
--- End code ---
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)It was meant to show when the type with
star and brackets are used, with argv.
Here's some code for you to look at and
compile if you like:
--- Code: ---
#include <iostream>
typedef char *charpointer; // helper typedef
int main( int argc, char **argv)
{
char **mystrings = new charpointer[ 5 ]; // allocate an array with 5 pointers to char
for( int i = 0 ; i < 5 ; ++i ) // allocate the char-arrays and store their pointers
{
mystrings[ i ] = new char[ 20 ];
}
for( int i = 0 ; i < 5 ; ++i ) // read five words from user
{
std::cout << i << ": ";
std::cin >> mystrings[ i ];
}
std::cout << "mystrings[0]: " << mystrings[0] << std::endl;
std::cout << "*mystrings[1]: " << *mystrings[1] << std::endl;
for( int i = 0 ; i < 5 ; ++i )
{
std::cout << "mystrings[" << i << "]: " << mystrings[i] << std::endl;
}
for( int i = 0 ; i < 5 ; ++i ) // clean up the char-arrays
{
delete [] mystrings[ i ];
}
delete [] mystrings; // clean up the pointer-array
}
--- End code ---
Output:
--- Code: ---
0: hello
1: this
2: is
3: just
4: nonsense
mystrings[0]: hello
*mystrings[1]: t
mystrings[0]: hello
mystrings[1]: this
mystrings[2]: is
mystrings[3]: just
mystrings[4]: nonsense
--- End code ---
Not really what's going on with the OP's code
now is it ? *mystrings[1] is the first character
in the second char-array. You use that syntax for
pointer-to-pointers, not regular pointers.
And while I'm at it why not poke a hole at your
array-gets-copied-and-can't-be-modified statement:
--- Code: ---
#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;
}
--- End code ---
Output ? This, of course:
--- Code: ---
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
--- End code ---
Please, do test your stuff before you start
giving advice if you're not sure you're right.
&(*arry)[startscan] ? wut ?
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version