C Program to Find the Largest Number in an Array

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hi
I was just reading about array chapter. I want to use array in program. I tried to write c program to find largest number in array. I saw some programs on Internet but I didn't understand because I don't understand logic how to find largest number manually . first I want to understand basic and than I want to go for Program.

For example

Example: Users enter: 1 0 7 9 6
The largest number is : 9

By looking we can say that the number 9 is largest number but how does program do ?

I am trying to understand basic logic so that I can try to implement program .

int Array[5] = {1,0,7,9,6};

how to find out manually which number is largest in array ?
 
Last edited:

MrChips

Joined Oct 2, 2009
30,806
By looking we can say that the number 9 is largest number but how does program do ?
You arrived at the answer 9 by visual examination of the numbers.
How did you arrive at this answer?
Your brains worked through the logic without your realizing it.
A computer program has to do exactly the same thing.

Draw a flow chart of what you think the brain has to do.

Don't reply to this thread until you have worked out a functional flow chart.
(Tip: This has nothing to do with writing computer code.)
 

jayanthd

Joined Jul 4, 2015
945
This is the quick code which I made.

C:
#define sZInt 2

int array[5] = {1, 0, 7, 9, 6};
int i = 0;
int tmp = 0;

void main() {

    while(1) {
    
        for(i = 0; i < (sizeof(array) / sZInt); i++) {
            if(array[i] > tmp) {
                tmp = array[i];
            }          
        }

        //print tmp here
    }
}
 
Last edited:

WBahn

Joined Mar 31, 2012
30,058
Your program has the right idea, but it has several problems.

First, how do you know that the size of an int is 2 bytes? It might be on the compiler you are using right now, but what if you change compilers?

You want your code to be as portable as possible and you also want it to be easy to maintain.

So you could use

#define sZint sizeof(int)

But now what if you change the type of the array to a long or a double? The fact that you have a meaningful name in sZint will help remind you to update this, but it's even better to make it happen automatically. So find the number of elements in an array by first finding out how much memory the array needs and then divide that by how much memory each element (such as the first element) needs.

sizeof(array) / sizeof(array[0])

or, even better,

sizeof(array) / sizeof(*array)

This is better because this approach makes it easy to find the number of rows and columns in a two dimensional array (and extend that up to as many dimensions as you have).

Next, what is the value of 'i' the last time through the loop? More particularly, what is the value of (i+1) when you use it to dereference the array?

After that, your determination for the value of tmp is based solely on the value stored at one point in the array and the value stored in the next element. Walk through your logic using a case in which the first value in the array is the largest and see what happens.

Finally, your code is in an infinite loop, so it is going to keep finding and printing out the value of tmp over and over. I realize that this is an artifact of working with an embedded microcontroller. Which is why I and others have recommended that you use a host platform (such as a PC) while you are learning the language and leave the additional details of dealing with an embedded environment to later.

You need to start thinking like a computer program and what will help that is if you ask yourself how YOU might solve the problem if you had to work under the same constraints as the program. The program can't glance at the entire list of numbers and pick out the largest. Humans can, if the list isn't too long. But what if the list had thousands of numbers? Now we have to work much more like a computer. So how might you approach the task now?

Another way is to imagine taking a stack of index cards and writing one number from the array on each card and then you get to flip them over one at a time and you only get to look at one card at a time. But you can write numbers on other cards if you want. But the most you can ever look at are two cards at the same time.

So what information might you keep on this other card and how would you use it?
 

jayanthd

Joined Jul 4, 2015
945
Yes, you are correct. I am forgetting things.

C:
int array[5] = {1, 0, 7, 9, 6};
unsigned long i = 0;
int tmp = 0;

void main() {

    while(1) {
   
        for(i = 0; i < (sizeof(array) / sizeof(*array)); i++) {
            if(array[i] > tmp) {
                tmp = array[i];
            }

       if(i > MAX) {
        break;
       }          
        }

        //print tmp here
    }
}
 
Last edited:

MrChips

Joined Oct 2, 2009
30,806
Another problem:

You make the assumption that all numbers are equal or greater than 0.
Your program fails to find the max if numbers are all negative values.
 

jayanthd

Joined Jul 4, 2015
945
Another problem:

You make the assumption that all numbers are equal or greater than 0.
Your program fails to find the max if numbers are all negative values.
9 > 0 and also 9 > -10

Where am I wrong ? I have used int variable for tmp and array[].

You mean -9 > -2 ?
 

WBahn

Joined Mar 31, 2012
30,058
9 > 0 and also 9 > -10

Where am I wrong ? I have used int variable for tmp and array[].

You mean -9 > -2 ?
Two things: You never define MAX. What is it?

Consider the case of:

int array[5] = {-20, -7, -10, -5, -13};

What will you determine the largest value is? What should it be?

Think of the piece of information that the variable tmp is capturing. It tells you the largest value you have seen in the array so far.

When the only value in the array that you have looked at so far is the first value, what is the largest value you have seen in the array so far?
 

jayanthd

Joined Jul 4, 2015
945
Two things: You never define MAX. What is it?

Consider the case of:

int array[5] = {-20, -7, -10, -5, -13};

What will you determine the largest value is? What should it be?

Think of the piece of information that the variable tmp is capturing. It tells you the largest value you have seen in the array so far.

When the only value in the array that you have looked at so far is the first value, what is the largest value you have seen in the array so far?

MAX is the max value a unsigned long variable can hold.

-5 is the largest value because -5 is > other -ve numbers.

9 is thr largest value I have seen in the array.
 

WBahn

Joined Mar 31, 2012
30,058
MAX is the max value a unsigned long variable can hold.
Are you sure about that? It's not in the language standard and you aren't including any header files, so where are you getting this from? If it's something native for your platform, then it's very non-portable to use it.

C has limits of the various integer data types defined in the header limits.h. You should use those.

https://www.tutorialspoint.com/c_standard_library/limits_h.htm

So you should use ULONG_MAX

But let's assume that you are correct and that MAX is the largest value that an unsigned long variable can hold. How can (i > MAX) ever be true? In order to be true, 'i' would have to hold a value LARGER than MAX, but MAX is the largest value that it can hold!

Furthermore, what is the purpose of this check? It isn't preventing you from accessing an illegal element of the array, since (1) it isn't keyed to the size of the array, and (2) it is outside of the for() loop that accesses the array. So how could 'i' ever get this big.

-5 is the largest value because -5 is > other -ve numbers.
But what will YOUR program determine the largest value is? Walk though your code and execute it line by line yourself and see what is happening.

9 is thr largest value I have seen in the array.
Really? After seeing only the first element of the array, which is 1 for your data, you conclude that 9 is the largest value you have seen so far?
 

WBahn

Joined Mar 31, 2012
30,058
Will think about it more.
While you are at it, it might be helpful to consider a different perspective to the whole thing.

Many people try to focus on how to solve a problem with a program. In doing so, they focus on the syntax and the libraries and the control structures and tend to lose sight of the real objective.

We don't use programs to solve problems. We use programs to IMPLEMENT solutions to problems.

There's a very real difference here. You need to solve the problem BEFORE you even think about writing a program -- particularly when you are at the stage of not being proficient at writing programs. Once you have the solution to the problem -- meaning the logic behind the solution has been worked out -- then start thinking about how to implement that solution using a program. Now you are in a position to struggle with the syntax and libraries and logic errors in a very concrete and meaningful way.
 

MrChips

Joined Oct 2, 2009
30,806
You are close but now you have to pay attention to details.

1. When do you need to increment i to i + 1?
2. When do you stop?
 

WBahn

Joined Mar 31, 2012
30,058
You are getting very close, but you have one logic error and one inefficiency.

Assuming the N is the number of items stored in the array, ask yourself what happens when i is equal to N in this flowchart. Is that what you want?

Then consider your first pass, when i is equal to zero. Is there any point in executing this pass through the loop? If you think it makes the code cleaner, there is no harm done beyond wasting a bit of time. If you have the time to waste, then the decision should favor the clean code. But, especially when doing embedded programming, you often don't have the time to waste.
 

MrChips

Joined Oct 2, 2009
30,806
There is one other possibility to consider.

What happens if N = 0, i.e. the array is empty (a null array)?

Many times, computer programs "crash" because the unexpected happens which was not given consideration in the design of the program.

A classic example is having a divide routine that does not take into account the case when the divisor is zero.
 
Top