Displaying lowest/highest in an Array?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 4
Reputation: hiddendragon is an unknown quantity at this point 
Solved Threads: 0
hiddendragon hiddendragon is offline Offline
Newbie Poster

Displaying lowest/highest in an Array?

 
0
  #1
Jul 19th, 2008
how would u diplsay the lowest or highest value of an array
that the user entered

so u ask the user to enter several number and it goes into an array
then how would u display the highest/lowest value in that array to the sreen
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Displaying lowest/highest in an Array?

 
0
  #2
Jul 19th, 2008
Create a variable called say 'highestSoFar'
Compare with each element of the array, and update it if you find a bigger value.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 21
Reputation: ishaanarora is an unknown quantity at this point 
Solved Threads: 0
ishaanarora's Avatar
ishaanarora ishaanarora is offline Offline
Newbie Poster

Re: Displaying lowest/highest in an Array?

 
0
  #3
Jul 21st, 2008
here is code for ur problem:
  1. #include<iostream.h>
  2. int main()
  3. {
  4. int array[1000000],n;
  5. int high,low;
  6. cout<<"Enter the no of values u want to enter");
  7. cin>>n;
  8. /* Now entering the values in array*/
  9. for(i=0;i<n;i++)
  10. cin>>array[i];
  11.  
  12. /* The highest no is*/
  13. high=array[0];
  14. for(i=0;i<n;i++)
  15. {
  16. if(a[i+1]>a[i])
  17. {
  18. high=a[i+1];
  19. }
  20. else
  21. {
  22. high=a[i];
  23. }
  24. }
  25. puts<<"the highest number in array is"<<high;
  26.  
  27. //sSimilarly u can do for lowest number
  28. return 0;
  29. }
Thanks and Warm Regards:
Ishaan
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Displaying lowest/highest in an Array?

 
0
  #4
Jul 21st, 2008
>here is code for ur problem
Giving away homework answers is frowned upon here.

>#include<iostream.h>
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"<<high;
I'd say you should stick to C, but your program is broken in either language.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 89
Reputation: camilojvarona is an unknown quantity at this point 
Solved Threads: 10
camilojvarona camilojvarona is offline Offline
Junior Poster in Training

Re: Displaying lowest/highest in an Array?

 
0
  #5
Jul 21st, 2008
Hi,

Hire is the code.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14.  
  15. int arrayLength = 0;
  16. cout << "Enter the numbers of elements of the array: ";
  17. cin >> arrayLength;
  18. int array[arrayLength];
  19. int index = 0;
  20. 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++)
  21. int highest ;//Here you could assing the 'int' lowest value.
  22.  
  23. //Capturing the entries and getting the lowest and highest values.
  24. do
  25. {
  26. cout << "Enter value number " << index << " :" ;
  27. cin >> array[index];//Here you should use get_line()
  28. cout << endl;
  29. //Remove this if statement if you initialize the value as mentioned above.
  30. if(index == 0)
  31. {
  32. cout << "index == 0";
  33. lowest = array[0];
  34. highest = array[0];
  35. }
  36. if(lowest > array[index])
  37. lowest = array[index];
  38. if(highest < array[index])
  39. highest = array[index];
  40. index++;
  41. }
  42. while(index < arrayLength);
  43.  
  44. cout << "The highest value is: " << highest << " And the lowest is: " << lowest << endl;
  45.  
  46. system("PAUSE");
  47.  
  48. return 0;
  49. }

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
Last edited by camilojvarona; Jul 21st, 2008 at 11:45 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Displaying lowest/highest in an Array?

 
0
  #6
Jul 21st, 2008
>Hire is the code.
Giving away homework answers is still frowned upon here.

>#include <sstream>
>#include <string>
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:
  1. #include <iostream>
  2.  
  3. void foo();
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. cout<<"Hello, world!\n";
  10. foo();
  11. }
  12.  
  13. void foo()
  14. {
  15. // std:: is required because "using namespace std"
  16. // only applies to the main function
  17. std::cout<<"foo\n";
  18. }
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 89
Reputation: camilojvarona is an unknown quantity at this point 
Solved Threads: 10
camilojvarona camilojvarona is offline Offline
Junior Poster in Training

Re: Displaying lowest/highest in an Array?

 
0
  #7
Jul 21st, 2008
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
Last edited by camilojvarona; Jul 21st, 2008 at 12:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 4
Reputation: originaldaemon is an unknown quantity at this point 
Solved Threads: 0
originaldaemon originaldaemon is offline Offline
Newbie Poster

Re: Displaying lowest/highest in an Array?

 
0
  #8
Jul 21st, 2008
I think it would be good for you to check out sorting methods. Using a "bubble sort" is probably the easiest, and makes a good starting point. If you sort the entire array so the entries are in order from heighest to lowest you can just return array[0] and array[n] to show the heighest and lowest values.

What would also be good is random number generators. This could make your testing a little easier by allowing you to automatically generate the information to fill the array (save you having to enter it every time you want to try something out).

There are lots of places you can find this information out, so just search until you find somewhere that explains it best for you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Displaying lowest/highest in an Array?

 
0
  #9
Jul 21st, 2008
The problem doesn't even need an array, nevermind a sorted one.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Displaying lowest/highest in an Array?

 
0
  #10
Jul 21st, 2008
Originally Posted by Salem View Post
The problem doesn't even need an array, nevermind a sorted one.
Yes, doesn't need one, but insist on having one.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC