• C


write a c program segment to display the index of the smallest and the largest numbers in an array x of 20 integers. Assume array x already has values assigned to each element.

so arrays means using int x[20]? I don't know how you to make the program pick the smallest and the largest from a set of 20 integers. This teach yourself class is very difficult for me.

Who is Participating?
PaulCaswellConnect With a Mentor Commented:
Hi thunderrolls,

To find the maximum value of an array, start by setting a value to something lower than you can reasonably expect to see. Then loop through the array. For each element, if it is greater than your current value, set your value to it.

Minimum is just the opposite way around.

Harisha M GCommented:
OK.. I'll try algorithm this time ;)

set min to arr[0]
set max to arr[0]
loop from 1st to 19th element (i<20) and in each iteration, perform the below operations:
  if  max < current element, max = current element
  if  min > current element, min = current element

Khanh DoanSenior DeveloperCommented:
oops, sorry

thunderrolls, first of all, you should assgin min, max to the first element of array:
min = max = x[0];

Then you use the loop FOR
for (i = 0; i < 20; i++)

then you use IF statement.
if (max < x[i]) max = x[i];
if (min > x[i]) min = x[i];

Finally, you output min, max value by prinft("Min : %d, Max : %d", min, max);

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

thunderrollsAuthor Commented:
is this even close?

int small = 0;
      int large = 0;
      int i;

      for(i=0; i<20; ++i) {
            if(x[i] > x[large])
                  large = i;
            if(x[i] < x[small])
                  small = i;

      printf("Smallest is at: %d \n", small);
      printf("Largest is at: %d \n", large);
Harisha M GCommented:
Good :)
>>          if(x[i] > x[large])

This might work better:

          if(x[i] > large)



>>               large = i;

you'd be better off with:

               large = x[i];

see 'large' is just a number and 'x' is an array. That means that most of the time when you see an x it should have a '[' next to it while large will almost certainly not have one associated with it.

Harisha M GCommented:
Paul, the code posted by the author works perfectly without any flaws. Why are you introducing the bugs ?

Ahhhh.. I seee. :-)


I completeley misread your code. My apologies! Please ignore my above posts.

I was trying to find you the highest value, without interest in where it is. Mine is a very slightly faster algorithm that only delivers the highest value, not 'where it is'. Harish's solution is a much more flexible and equally tidy technique that I would like to see more often.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.