Solved

Arrays

Posted on 2006-06-17
11
440 Views
Last Modified: 2010-04-15
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.

thanks
0
Comment
Question by:thunderrolls
11 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 125 total points
ID: 16927242
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.

Paul
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16927367
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

0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16929134
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);

Goodluck.
Bonmat86.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:thunderrolls
ID: 16930644
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);
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16930667
Good :)
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16930741
>>          if(x[i] > x[large])

This might work better:

          if(x[i] > large)

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16930742
Oops!

and

>>               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.

Paul
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16930749
Paul, the code posted by the author works perfectly without any flaws. Why are you introducing the bugs ?
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16930795
Harish,

Ahhhh.. I seee. :-)

thunderrolls,

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.

Paul
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question