answersLogoWhite

0

Fist of all, let's assume that by "list" you mean "array." Otherwise we would need your list implementation in order to be able to iterate through the elements.

int[] nums; // assume these are the numbers you want to search through

int nums_length; // also assume that we know how many numbers are in nums

int min; // smallest number in nums

int max; // largest number in nums

// special case for an empty list of numbers: set min = max = 0

if(nums_length == 0) {

min = 0;

max = 0;

}else {

// start min and max off as equal to the first number

min = nums[0];

max = nums[0];

// iterate through nums

int i;

for(i = 1; i < nums_length; ++i) {

// update max, if necessary

if( nums[i] > max ) {

max = nums[i];

}

// update min, if necessary

if(nums[i] < min) {

min = nums[i];

}

}

}

// min and max are now properly set (or both equal to 0 if nums is empty)

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

How do you beat find HQ beach house?

CODE HINTS ( no spoilers)box under umbrella code is on telescopeliving room code is in bathroomold scroll number to letter code use it on the box in the bedroomclick the smallest to largest decimal


Write a program find maximum number from given three number in 8085 micro processor?

This code find s the largest and the smallest number in an array terminated with a zero. It's not exactely a perfect match for the question but I dion't believe in answering question papers. It's always good to let the student do some work. There's enough example here to cut it down to provide an answer to the question. CODE SEGMENT MOV AX,DATA MOV DS,AX ; initialize DS MOV DI, OFFSET ARRAY ; DI points to the array MOV AX, [DI] ; AX contains the first element MOV LARGEST, AX ; initialize largest MOV SMALLEST, AX ; initialize smallest MOV CX,6 ; loop counter Al: MOV AX, [DI] ; get array value CMP AX, SMALLEST ; [DI] = smallest? JGE A2 ; yes : skip MOV SMALLEST, AX ; no : move [DI] to smallest JMP A3 ; as it is smallest, thus no need ; to compare it with the largest . i A2: CMP AX,LARGEST ; [DI] = largest JLE A3 ; yes : skip MOV LARGEST, AX ; no : mov [Dl] to largest A3: ADD DI,2 ; point to next number LOOP A1 ; repeat the loop until CX = 0 MOV AX, 4C00h INT 21h ; halt, return to DOS CODE ENDS DATA SEGMENT ARRAY DW -1, 2000, -4000, 32767, 500, 0 LARGEST DW ? SMALLEST DW ? DATA ENDS END.


How to find the smallest number in c code?

smallest = min (one_number, another_number); ...or... smallest = one_number &lt; another_number ? one_number : another_number;


What does photo shop do with your serial number?

The serial number is a code that unlocks the program.


How the program statement and code are related?

A statement in your program is part of the code. In a low-level programming language, a statement will map directly to a single CPU instruction. In a high-level programming language, a statement is the smallest element of the language's syntax.


What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


What is the smallest number of bases on a DNA strand needed to code for insulin?

The smallest number of bases on a DNA strand needed to code for insulin is approximately 154 bases. This sequence would code for the amino acid sequence that makes up the insulin protein.


What is Indoneisian Currency?

The rupiah is the currency of IndonesiaThe smallest coin is 50 rupiah, the largest banknote is 100,000 rupiah.The international trading code is IDR.


Use of m02 code in cnc program?

indicates the number of machined parts.the number id incremented each time m02,m30 m02= program end m30 =


What is the computer term used to describe a spelling or grammatical error in a computer program?

That's commonly called a bug. Even the smallest spelling error in a line of computer code can have a 'cascade' effect over the whole program.


A computer program has been written to break a code the code uses the letters A-G with each letter being used only once If the program can try 1 unique code every second what is the maximum number?

7!


What is the largest positive number one can represent in an eight-bit two's complement code?

127.