Solved

Program that Displays the Max of an Array

Posted on 2008-10-14
2
192 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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
shortest distance between a Line (p1(lat,long), p2(lat,long)) and a point p3(lat,long) 10 152
Socket Programming (Unix) 8 119
What is sub-make ? 2 39
Resolve Dependency Issues 4 47
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

895 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

15 Experts available now in Live!

Get 1:1 Help Now