Array maximum and minimum

maximum value is wrong!

Page 1 of 1

6 Replies - 11764 Views - Last Post: 28 September 2007 - 07:34 PM Rate Topic: -----

#1 Doka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 28-June 07

Array maximum and minimum

Posted 28 September 2007 - 10:37 AM

hey I wrote this code to display the maximum and minimum of an array, it displays the minimum value right, but the maximum value is wrong, it shows some random numbers, I dont know why...help?
#include <iostream>
using namespace std;

 void main(){
	 int number[]= {1,3,4,5,5,-1,90,9,48};
	 int max = number[0];
	 int min = number[0];

for (int i=0; i<10; i++){

   if (number[i] > max) {
	  max=number[i]; 
	  }
   if (number[i] < min) {
	  min=number[i];
	  }
   
 }
	 cout << "max = " << max;
	 cout << "\n";
	 cout << "min= " << min;
	 cout << "\n";

  }


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Array maximum and minimum

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Array maximum and minimum

Posted 28 September 2007 - 11:02 AM

It works perfect for me. Just one thing though:
Do not use
void main()
{
}


Use:
int main()
{
return 0;
}



I don't think there's anything wrong. Can you post your output?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5062
  • View blog
  • Posts: 26,436
  • Joined: 10-May 07

Re: Array maximum and minimum

Posted 28 September 2007 - 11:15 AM

Try declaring a maximum for your array.

int number[]= {1,3,4,5,5,-1,90,9,48};



If you are getting random values, maybe you have garbage left over in your memory.
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Array maximum and minimum

Posted 28 September 2007 - 12:55 PM

you intiailize your array to contain 9 elements, then run your for loop from 0 to 9...that's 10 tests. so as no2pencil suggestes, you're accessing a bunch of garbage data beyond your array. whether or not your program will output the correct data, then, depends on what that garbage is. if min < garbage < max, you're still okay, but if not, you'll be displaying junk.

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 Doka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 28-June 07

Re: Array maximum and minimum

Posted 28 September 2007 - 02:06 PM

Hmm, it worked when I declared a maximum for my array....but how can I get rid of the garbage left over in the memory? I wanna know....since I am new to the C++ language..
Thanks guys, now I got it done, extra infromation would be nice ;)
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Array maximum and minimum

Posted 28 September 2007 - 02:28 PM

View PostDoka, on 28 Sep, 2007 - 02:06 PM, said:

....but how can I get rid of the garbage left over in the memory?....


DON'T!!!

the junk that you see is not really garbage, it's just stuff you shouldn't be looking at anyway, at least within the context of the array. with arrays, you need to be really careful to not overrun the end of the allocated data.

it's like picking up a book off of a library shelf. if you read it all the way through, it makes sense. if you don't stop, pick up the next book on the shelf, and read the first ten pages of that book thinking that it's the same one, it will make absolutely no sense.

just remember to keep track of how many elements are in the array. and remember that array element indices run from 0 to (max_elements - 1).

i think the problem here was that you actually meant to have 10 numbers in your array.

-jjh

This post has been edited by jjhaag: 28 September 2007 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Array maximum and minimum

Posted 28 September 2007 - 07:34 PM

You could also just sort the array since there appears to be no actions taken on it after the output. (i.e. the sorting would mess up future work with the array values.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1