|
-
March 2nd, 2006, 05:36 AM
#1
Traversing arrays problem
hi, i'm having problems getting my code to work, i'm very new to C++, what i want this code to do is to display the location of the largest value of the array.
#include <algorithm> // for min_element/max_element
#include <iostream> // for cout
using namespace std;
int main()
{
cout <<"witness the awesome power of the LARGEST VALUE IN ARRAY FINDER !!!\n\n";
int array[] = {5,6,3,230,3,4,6,110};
unsigned const num_elements=sizeof(array)/sizeof(int);
int largest = *max_element(array,array+num_elements);
int bigpos;
bigpos = array[&largest];
cout <<"It's position in the array is "<< bigpos<<"\n";
cout <<"Largest value in array: "<<largest;
cin.get();
cin.get();
return 0;
}
it should display 3 as the answer but it wont even compile.
can someone help me fix this problem ?
Last edited by samaank; March 2nd, 2006 at 05:39 AM.
-
March 2nd, 2006, 05:36 AM
#2
Re: Traversing arrays problem
-
March 2nd, 2006, 05:49 AM
#3
Re: Traversing arrays problem
OK, I don't know about max_element() function, probably there is also an error, but I found two other:
Code:
const unsigned int num_elements = sizeof(array);
// ...
largest = array[bigpos];
Please don't forget to rate users who helped you!
-
March 2nd, 2006, 05:49 AM
#4
Re: Traversing arrays problem
Code:
#include <algorithm> // for min_element/max_element
#include <iostream> // for cout
using namespace std;
int main()
{
cout <<"witness the awesome power of the LARGEST VALUE IN ARRAY FINDER !!!\n\n";
int array[] = {5,6,3,230,3,4,6,110};
unsigned const num_elements=sizeof(array)/sizeof(int);
int * plargest = max_element(array,array+num_elements);
cout <<"It's position in the array is "<< (plargest-array) <<"\n";
cout <<"Largest value in array: "<< *plargest;
cin.get();
cin.get();
return 0;
}
max_element yields a pointer/iterator to the largest element, not a pointer to a position of the largest element.
To get position, you simply need to compute the difference pointer_to_largest-array
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
March 2nd, 2006, 05:52 AM
#5
Re: Traversing arrays problem
it works !
thanks heaps KoKo
-
March 2nd, 2006, 06:32 AM
#6
Re: Traversing arrays problem
ok, one more question, how can i get a user to input a number, then the program to search for it's index in the array and print the index, and if the number does not exist it will say "number does not exist" ?
#include <algorithm> // for min_element/max_element
#include <iostream> // for cout
using namespace std;
int main()
{
int array[] = {1,12,32,45,56,89,122};
int ivalue;
cout << "please enter the value you are looking for\n";
cin >> ivalue;
cin.get();
cin.get();
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|