Create a variable called say 'highestSoFar'
Compare with each element of the array, and update it if you find a bigger value.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>here is code for ur problem
Giving away homework answers is frowned upon here.
>#include
We're well beyond the point where this will fail to compile on many modern compilers. I recommend you get with the times.
>int array[1000000]
This is a terrible waste of memory and it's also a broken design. You don't take into account that n could contain a value greater than 999999, which is a blatant security risk and certainly doesn't make for a robust program.
>if(a[i+1]>a[i])
Let's say that n is 1000000 and i is at the end of the loop with 999999. What happens when you index a[i + 1]?
>puts<<"the highest number in array is"<
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Hi,
Hire is the code.
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int arrayLength = 0;
cout << "Enter the numbers of elements of the array: ";
cin >> arrayLength;
int array[arrayLength];
int index = 0;
int lowest ;//Here you could assing the 'int' highest value even though I've read that depends on sytem the prorgram is compiled 32-bits... But maybe there is a funtion that give you the value at run time. Or simple depending on the system.(I'm new to C++)
int highest ;//Here you could assing the 'int' lowest value.
//Capturing the entries and getting the lowest and highest values.
do
{
cout << "Enter value number " << index << " :" ;
cin >> array[index];//Here you should use get_line()
cout << endl;
//Remove this if statement if you initialize the value as mentioned above.
if(index == 0)
{
cout << "index == 0";
lowest = array[0];
highest = array[0];
}
if(lowest > array[index])
lowest = array[index];
if(highest < array[index])
highest = array[index];
index++;
}
while(index < arrayLength);
cout << "The highest value is: " << highest << " And the lowest is: " << lowest << endl;
system("PAUSE");
return 0;
}
you should do defensive programming if you want to improve it.
Any sugestiong on how to improve this code will be highly appritiated. I am new to C++;
Hope this help.
Camilo
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 10
Solved Threads: 10
>Hire is the code.
Giving away homework answers is still frowned upon here. :icon_rolleyes:
>#include
>#include
You don't use either of these, so remove them.
>using namespace std;
If you must use this, keep its scope limited as much as possible. In case you didn't know, you can use this in a block and its effect will only apply to that function:
#include <iostream>
void foo();
int main()
{
using namespace std;
cout<<"Hello, world!\n";
foo();
}
void foo()
{
// std:: is required because "using namespace std"
// only applies to the main function
std::cout<<"foo\n";
}
>int array[arrayLength];
While this may work for you, it's not going to work everywhere. Standard C++ requires that array sizes must be a compile-time constant. Arrays with a runtime size are only allowed as a compiler extension, so your code is not portable. If you really want this feature, use a vector instead.
>//Here you could assing the 'int' highest value even though I've read that depends on OS
>32b 64 and I am not sure if I've read that also depends on the compiler. But maybe there
>is a funtion that give you the value at run time.(I'm new to C++)
I assume you mean initializing the variable to the largest possible value. You can do that several ways, but all of them are pointless in this case as this doesn't make the code any clearer. It's better simply to set both largest and smallest to array[0] and update as necessary as you iterate through the values.
>cin >> array[index];//Here you should use get_line()
So should you. If you want to talk about how things should be done, just go ahead and do them instead of confusing people with poorly described potential options.
>system("PAUSE");
Get used to not using this. It's terribly unsafe.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Hi Narue,
Thanks for your sugestions. Actually this is my first day at C++.
"So should you. If you want to talk about how things should be done, just go ahead and do them instead "
In case you haven't noticed almost every tutorial for beginers as I am and as I asume the one who posted the answer they do it the way I did it for the sake of clarity.
"Giving away homework answers is still frowned upon here. "
Sorry about that. You should think that I did it on porposed bu I didn't.I haven't see your post. Still is a functional code as you see has a lot of flags so everybody learn. Today I've wrote my first line with C++ :-).
" but all of them are pointless in this case as this doesn't make the code any clearer"
Here I disagreed with you this since you would remove a couple of lines of code and every line of code that you add increases the risks of erros , and makes the code less readeble and increases the maintenance effort, and de costs of the project.
Again thanks for your sugestions.
Camilo
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 10
Solved Threads: 10
The problem doesn't even need an array, nevermind a sorted one.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Hi,
I was trying a thing but it didn't work. When will I learn tha array's size most be constant an compile-time :-).
Regards,
Camilo
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 10
Solved Threads: 10
I've found a workaround you just need to initialize the array first like this. int arrays[1] and then you can do like this arrays[arrayLength];
If you mean that you're doing something like this:
int a[1];
a[2] = 9;
cout << a[2];
then I can only say :If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Vectors would definitely be the answer you're looking for.
But if you insist on using arrays, you could use:
int* a = NULL; // Pointer to int, initialize to nothing.
int size = 100;
a = new int[size]; // Allocate "size" ints and save ptr in a.
for (int i=0; i<size; i++)
a[i] = 0; // Initialize all elements to zero.
//do stuff
delete [] a; // When done, free memory pointed to by a.
[edit]
Ah.. you've changed your orginal post, which makes this post sounds like something that dropped out of the sky ...
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Hi niek_e,
Yeap, I've changed it. Thanks for your coments. Although I used your idea but without the 'initialize all elemets to zero' part and works fine. Now I am reading a manual and saw this way arrays = new (nothrow)int['variable']
Regards,
Camilo
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 10
Solved Threads: 10
.Just because it works with your compiler does not mean it will work with others. The initial values of an array are undefined in C++, which means they may be 0 or anything else.
To initialize an array whose size is not know at compile time, you have to dynamically allocate space, which in C++ usually means using the new operator. You shouldn't use the std::nothrow parameter unless you understand what it does: if the requested memory cannot be allocated, no exception is thrown and NULL is returned.
See niek_e's post for a proper example of dynamically allocating an array.
Hi,
Just because a named it 'arrays' doest mean that it is not a pointer ;-). Also a read the material on this site http://www.cplusplus.com and I now why to use 'nothrow'.
In this articles said that wen you create an array (e.i int array[value]) It actually reserved that space in the memory. So there is no needs for inticialitation. I assume the same behavior with dynamic memory for an example that is in that site to.
Regards,
Camilo
camilojvarona
Junior Poster in Training
89 posts since Jul 2008
Reputation Points: 10
Solved Threads: 10