Solved

Program that Displays the Max of an Array

Posted on 2008-10-14
2
199 Views
Last Modified: 2012-05-05
I have a homework assignment which is to write a function that takes an array of floats and the length of
the array, and returns the maximum value in the array.  So far I have tried a few different things and what I have finally settled on is something as seen below.  The problem is that I realized I will need thousands of if statements to say if n10> n9>n8...and use all combinations possible to get the max displayed.  Is there an easier way to do this or did I start off completely wrong?

{
  printf("Enter up to 10 decimal values: "); /*Asks the user for values to find\
 the maximum of*/

  fgets(line, sizeof(line), stdin); /*Reads the line of floats given*/

  sscanf(line, "%f %f %f %f %f %f %f %f %f %f", &n1, &n2, &n3, &n4, &n5, &n6, &\
n7, &n8, &n9, &n10); /*Converts the string into numbers*/


  /*Here is where I need to define the arraymax function, but I don't know how*\
/


  for (i=1; i<11; i++)
    {
 A[i] = (float);
    }

         answer = arrayMax(A,10);

        printf("The arrayMax function returned %f\n", answer);

         return 0;
         }


0
Comment
Question by:spngegirl01
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 22710757
You dont have to compare each value with every other value.
If A>B and A>C then you dont need to compare B and C.

Keep a variable max. Initialize it to first value in the array.
Loop through all the items in the array. If current value > max then update max to current value. At the end of the iteration you will have the max value - the utility of loops :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

832 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