Thursday, July 9, 2009

How to find maximum number C++?

Ok we're using an array, and the program itself displays 10 random numbers. From here, these numbers are put into the array. After this, we're supposed to find the maxiumum number out of those 10 random numbers.





Is there a function that can automatically do this? If not, do you have recommendations? For-Loops? Do-While Loops? etc.

How to find maximum number C++?
Ok, u want to generate 10 random numbers, and put them into an array, then find there max.





1- Decalre an array of length 10.


2- Initialize the array with random numbers.


for(int c=0 ; c%26lt;10 ; c++)


{


arr[c] = rand();


}





3- Declare a variable called max, and initialize it by the first element of the array.


int max = arr[0];





4- Loop over the array, and see if there is a number greater than max, if there is, then update the value of max to that greater number, and keep doing that till u reach the end of the array.





for(int c=1; c%26lt;10 ; c++)


{


if( max %26lt; arr[c] ) // If there is a greater value than max


max = arr[c]; // Please update max to that value


}





This is the simplest way to do it, i beleive there are more efficient ways (takes shorter time).





Good luck


Bye
Reply:Alternatively, use a sorting algorithm such as qsort, then take the last (and therefore largest) item from the array.
Reply:It's easy:


set a temp variable to the value of the array at 0, then you do a loop from 1 to 9, comparing if the temp var is smaller than the current array value, if so, change the temp value for the array value. At the end the temp value is the greater.
Reply:How would you do this manually? Figure that out and then code it.





If you cannot do such a simple homework assignment, how are you going to pass the tests?





Herre's a hint. Most people when looking for the largest number would look at each number and remember the largest that they have found.
Reply:Its called selection sort. Read this:


http://www.learncpp.com/cpp-tutorial/64-...





Good Luck
Reply:... She never said anything about sorting, so I don't know why it's being suggested.





As Samantha and Shyboy have said, it's not very difficult of a problem. You're going to want to look through the values in the array comparing each value to a variable in which you store your "current maximum value." If you continually place the larger of the two values into that variable through each iteration, you will have the largest value in that variable by the end of the array.





Secondly, Samantha gave you a perfectly good hint that should leave you to do nothing more than translate her hint into code. If you're expecting anyone to do that for you, then you're expecting more than you should be.
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int num[10],max,i;


printf("Enter the first number ");


scanf("%d",%26amp;num[0]);


max=num[0];


for(i=1;i%26lt;=9;i++)


{


printf("Enter the next number ");


scanf("%d",%26amp;num[i]);


if(num[i]%26gt;max)


max=num[i];


}


printf("The maximum number is %d",max);


getch();


}


/*copy and paste the last code and programme will run correctly*/
Reply:When I had such a task I did it with a for-loop. Just go through all elements in the array and use an integer variable to save the current max. If the current element is greater than the current max, then set the current max to the current element.

song words

No comments:

Post a Comment