?
Solved

Program that Displays the Max of an Array

Posted on 2008-10-14
2
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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