Solved

Program that Displays the Max of an Array

Posted on 2008-10-14
2
181 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now