C Program to Find the Largest Number in an Array

WBahn

Joined Mar 31, 2012
32,875
C:
if((i > 0) && (i < N)) {

}
I'm not sure where in relation to your flow chart you want to use this selection statement, but it captures the gist of the logic. You don't need to test whether the current element of the array is larger than "largest" when i=0 because your initialization took care of that. You also don't want to test any case in which i is not strictly less than N, otherwise you will exceed your array bounds.

But you appear to be slapping on patches to avoid the issues caused by errors and inefficiencies instead of making the effort to remove the errors and inefficiencies.

In the case of inefficiencies, this is particularly insidious because, far from removing them, you may have actually added to them. Instead of making a check you didn't need to make on a single pass, you now add a check to check if you need to do a check, but you now do the new check on every pass.

You also need to consider your initialization of largest. You assume that array[0] holds a meaningful value. But what if nothing was ever stored in the array?

This actually brings up some subtle issues (that I don't think you are ready for). So I'll just mention them but don't worry about really understanding them. At some point in the future you will be ready to tackle them and something will tickle the back of your brain that you've seen this before and that it is something to pay attention to.

A variable of array type can't be declared with less than one element. Similarly, a pointer can't point to a zero amount of memory. We do have what are called NULL pointers, which don't point to anything at all. But we have to tread very, very carefully when working with NULL pointers (or pointers that could potentially be NULL).

Also, the amount of data stored in an array (or a dynamically allocated block of memory) is not the same as the size of the array.

For instance,

int array[10] = {1,2,3,4,5};

This only has five pieces of data in it, but the array contains ten elements. You only want your program to check the data that is not there and you do NOT want it to check the other five elements, which are uninitialized and can have ANY junk value that happens to be there.

The way your particular program is written, this is not an issue because the array is declared so that it just holds the amount of data stored in it. Sometimes this is how it is. But more often your array is larger than the data that it actually holds.

So when you write code, keep in mind the distinction between how much data an array CAN hold, and how much it DOES hold. You often need two different variables for these two fundamentally distinct pieces of information.

always has enough memory
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
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.
upload_2017-8-30_17-15-4.png
if i = 0 than check the first number of array and assume it is largest number than compare to next element. if first number is largest than increment i and if not than store number as largest and than increment.
 

WBahn

Joined Mar 31, 2012
32,875
use qsort function from lib this will solve your problems.
picbuster
So the solution to the TS's goal of better understanding how to work with an array by writing a program to find the largest number in an array is to call a standard library function which may not even be available on a freestanding microcontroller implementation of the language?
 

WBahn

Joined Mar 31, 2012
32,875
View attachment 133941
if i = 0 than check the first number of array and assume it is largest number than compare to next element. if first number is largest than increment i and if not than store number as largest and than increment.
Is the text here supposed to match the flowchart? It doesn't.

Try writing a flow chart from the following text description of the algorithm.

Set N equal to the number of data elements stored in the array. If N is zero, print "No Data" and end the program, otherwise initialize the variable 'largest' to be equal to the first element in the array and a counter equal to the index of the second element. While the counter is less than the number of elements in the array, check if the element at the index indicated by the counter is larger than 'largest' and, if it is, update 'largest' with that value. Then increment the counter and continue the loop. After the loop completes, print out "Largest value is" and the value stored in 'largest' before ending the program.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Try writing a flow chart from the following text description of the algorithm.
.
whats wrong with that flow chart
Set N equal to the number of data elements stored in the array. If N is zero, print "No Data" and end the program, otherwise initialize the variable 'largest' to be equal to the first element in the array and a counter equal to the index of the second element. While the counter is less than the number of elements in the array, check if the element at the index indicated by the counter is larger than 'largest' and, if it is, update 'largest' with that value. Then increment the counter and continue the loop. After the loop completes, print out "Largest value is" and the value stored in 'largest' before ending the program
Look at attached pdf its not exactly what you said but i think it will work.
 

Attachments

Last edited:

WBahn

Joined Mar 31, 2012
32,875
whats wrong with that flow chart
Note that I didn't say anything was wrong with it, merely that it didn't match the written description you had below it. I didn't say one way or the other whether the flow chart was wrong. Assuming that N somehow gets the number of data items stored in it, the only logical problem it has is if N happens to be zero, in which case you will access array[0], which could crash the program, but even if it doesn't it will store a bogus value in 'largest' and report that is the largest value.

From an implementation (in C) standpoint the flowchart has the problem that it doesn't show a "while" loop, but instead an "until" loop (which some languages support). With a while() loop, you stay in the loop while the loop test condition is True. Your flowchart is the other way around in which you stay in the loop until the loop test condition is True. If the flowchart is generic (also known as language-agnostic) then it doesn't matter. But when you know the intent is to target a particular language, you should try to make the flowchart map directly to the language constructs as possible. Otherwise, you are very likely to use the test condition in the flow chart without realizing that it needs to be inverted.

Look at attached pdf its not exactly what you said but i think it will work.
This isn't complete. You have a decision box (the test is Counter < Number) that doesn't go anywhere when the test fails.

You have another implementation mismatch with your bottom selection statement. The logic is valid, but it maps to an if()/else structure that doesn't have anything in the 'if' block but something in the 'else' block. When you only have something in one path of a selection statement, put it in the True path so that you can just use an if() structure (not 'else'); you only have to invert the test in order to swap the paths.

Keep at it -- you seem to be making progress and I think you have learned quite a bit from where you started.
 

Picbuster

Joined Dec 2, 2013
1,058

simozz

Joined Jul 23, 2017
170
Hello,
Picbuster said:
I did want to give a quick solution.
Not so quick.
You don't need to sort data to find the maximum value of a sequence. It's an unuseful task.
Think about it a few seconds and how the data is being sorted: it's easy to realize that looking for the max value requires less calculations (and so time) then sorting it first.
simozz
 
Last edited:

MrChips

Joined Oct 2, 2009
34,826
To members providing thoughtful assistance to this thread, the TS is now in the process of learning how to problem solve, develop computer algorithms, draw flow charts, and converting flow charts to computer code.

Suggestions of library functions and more advanced solutions are not being helpful to the learning process of the TS.

An algorithm to determine the maximum value in an array is about as basic as one can get.
A sorting algorithm would very likely be the next exercise.

Please assist in this effort in helping the TS to advance in his computer programming skills.
 

philba

Joined Aug 17, 2017
959
Why do you set smallest = array[0] twice?
Your decision box with i<N doesn't have yes and no paths.
(as a consequence) Your loop never terminates.
Your flow chart (when the above errors are corrected) only works for a 5 element array.
 
Top