Question:
Find the largest integer in an array of ten integers...?
pinkchii_blu
2007-03-02 05:24:11 UTC
...the user will input 10 integers and the program will determine the largest integer...

can anybody help??It would be a great help for me
Five answers:
Jennifer H
2007-03-02 05:58:03 UTC
I don't know what language you're using, but here's an answer in C#. I have assumed that you will have the ten integers stored in an array.



int temp = 0; //store the largest number

//Below, the array index will be the integer i.



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

{

if (numbers[i] > temp)

{

temp = numbers[i];

}

}



Hope this helps!
void7x
2007-03-02 05:34:28 UTC
create a temporary variable and set it to the value of the first element of the array.



loop through each element of the array comparing it with the temporary valuable. if it's greater, set the temporary variable to it. if' it's not, don't do anything.



at the end the temporary variable contains the largest number.



C++ implementation will look like this:





int a[10];



// read the values;

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

cin >> a[i];



// the loop

int max = a[0];

for (int i=1; i<10; i++)

if (a[i] > max) max = a[i];



// print the result

cout << "The largest number is " << max;
boshell
2016-12-05 08:34:43 UTC
SPB's answer gained't artwork except contained in the particular case the position you've a non-empty array and non of the elements are -ve. close inspite of the truth that enhancing SPB's set of guidelines ... precondition: array no longer empty generate exception initialize max to the first aspect contained in the array
nick
2007-03-02 05:40:06 UTC
main{

int arr[10],i,large;

//get 10 numbers in an array

//then

//initialize large to first number of an array and not zero becuase

//integer can be negative also

large=arr[0];

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

{

if(arr[i]>large)

{

large=arr[i];

}

}

//display result stored in variable large
2007-03-02 05:35:34 UTC
Which programming language would you prefer your solution to be coded in? C, C#, C++, Perl, Java, etc


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...