C Program to Find the Largest Number in an Array

philba

Joined Aug 17, 2017
959
better.

now, why do you set N = 5 and then check N?
will the test Array[i] < Smallest ever be true the first time through the loop?
 
Last edited:

WBahn

Joined Mar 31, 2012
32,874
Take a piece of paper and put a column on it for each variable -- N, i, Smallest, and for each element of Array.

Now walk through your flowchart, doing exactly what it tells you to do at each step. Not what you want it to do, but exactly what it tells you to do.

Let's say Smallest is equal to 4, and i is equal to 1 when you get to the second decision box in your loop. Does it take the "Yes" path or the "No" path? Does it then do what you want it to do or not?
 

WBahn

Joined Mar 31, 2012
32,874
better.

now, why do you set N = 5 and then check N?
will the test Array\[i\] < Smallest ever be true the first time through the loop?
edit: brackets do funny things, ignore the "\"
You can deal with that problem by using the [plain] tags. Example: Array[i]
 

WBahn

Joined Mar 31, 2012
32,874
How does N change from 5 to 0? What box in your flowchart changes the value of N other than the very first box after Start?

You are doing what you WANT the code to do, not what the flowchart is actually doing. STOP THAT!

Look at the line when i=2. You have 2 <= 4 as True. Fine. But then you change the value of Smallest? Why? That's what you WANT to happen. But look at your flowchart! If Array < Smallest (and note you have '<' and not '<=') then the Yes arrow takes you AROUND the box that updates the value of Smallest.

You have in your table a check on N=5 each time through the loop, but that check (which is pretty meaningless to begin with) is executed ONCE before the loop ever starts.

Then your table never includes the check on i<N, which is the check that determines whether you stay in the loop.

You don't stand a chance of writing valid code if you can't walkthrough a piece of code and do what it says instead of what you want it to do.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
How does N change from 5 to 0? What box in your flowchart changes the value of N other than the very first box after Start?

You are doing what you WANT the code to do, not what the flowchart is actually doing. STOP THAT!

Look at the line when i=2. You have 2 <= 4 as True. Fine. But then you change the value of Smallest? Why? That's what you WANT to happen. But look at your flowchart! If Array < Smallest (and note you have '<' and not '<=') then the Yes arrow takes you AROUND the box that updates the value of Smallest.

You have in your table a check on N=5 each time through the loop, but that check (which is pretty meaningless to begin with) is executed ONCE before the loop ever starts.

Then your table never includes the check on i<N, which is the check that determines whether you stay in the loop.

You don't stand a chance of writing valid code if you can't walkthrough a piece of code and do what it says instead of what you want it to do.
Look at new flow chart with explanations

upload_2017-10-2_20-32-50.png


N = 5, i = 0, a[0] = 4 and Smallest = a[0]
check N = 5
Yes than
check 0 < 5
Yes Than
check 4 <= 4
Yes than smallest = 4
increment loop by 1

N = 5, i = 1, a[1] = 5 and Smallest = 4
check 1 < 5
Yes than
check 5 <= 4
No then
increment loop

N = 5, i = 2, a[2] = 2 and Smallest = 4
check 2 < 5
Yes than
check 2 <= 4
Yes than
Smallest = 2
increment loop
- - - - and so on
 

WBahn

Joined Mar 31, 2012
32,874
Much better.

No consider the following items (most of which have been mentioned previously)

1) What is the point of checking if N is equal to 5 before the loop? Since you have just set N=5 prior to this, is there any chance the test can fail? Even if there were, does it make since to print No Data if N is not equal to 5 (say you changed the first block to set N=4)?

2) Does it make sense to update Smallest when the value in the array is equal to it? What purpose does that serve?

3) Since you set the initial value of Smallest to Array[0] before the loop, does it make sense to check it again during the first pass through the loop?

4) After going through all this effort to find Smallest, you don't do anything with it.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Much better.

No consider the following items (most of which have been mentioned previously)

1) What is the point of checking if N is equal to 5 before the loop? Since you have just set N=5 prior to this, is there any chance the test can fail? Even if there were, does it make since to print No Data if N is not equal to 5 (say you changed the first block to set N=4)?
N = 5 this check length of array. this check only if there are only five element store in array. suppose the array of length is 5 but there are only four element store in array in this case. This will not check the array element.
2) Does it make sense to update Smallest when the value in the array is equal to it? What purpose does that serve?
if one element repeat more than one, that case use equal operator
Since you set the initial value of Smallest to Array[0] before the loop, does it make sense to check it again during the first pass through the loop?
In first case, I assume that first element of array is smallest. but the smallest number can be anyone in array element
 

philba

Joined Aug 17, 2017
959
WBaun's point (and a question I asked earlier to the same point) is that it doesn't make sense to bother with checking Array[0]. Start i at 1.
 

WBahn

Joined Mar 31, 2012
32,874
N = 5 this check length of array. this check only if there are only five element store in array. suppose the array of length is 5 but there are only four element store in array in this case. This will not check the array element.
Keep in mind that N is just a variable. It's value is equal to the number of data elements in the array only because YOU set it equal to 5 in the first block of your flowchart. Because it is a variable that you've set to 5, there is zero chance (barring a cosmic ray upsetting a register or some other extremely low probability event) that it will not be equal to 5 when you do the check. So why do the check? It's different if you are asking the user to enter the number of data items, because then you don't know what they are going to enter. But even then it makes no sense to perform your check against N being equal to exactly 5.

if one element repeat more than one, that case use equal operator
Why? If Smallest is equal to 42 and Array[3] is also equal to 42, why overwrite Smallest with 42 when it is already 42?

In first case, I assume that first element of array is smallest. but the smallest number can be anyone in array element
So? After setting Smallest equal to Array[0], what is the purpose of checking if Array[i] is less than or equal to Smallest when i = 0?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Keep in mind that N is just a variable. It's value is equal to the number of data elements in the array only because YOU set it equal to 5 in the first block of your flowchart. Because it is a variable that you've set to 5, there is zero chance (barring a cosmic ray upsetting a register or some other extremely low probability event) that it will not be equal to 5 when you do the check. So why do the check? It's different if you are asking the user to enter the number of data items, because then you don't know what they are going to enter. But even then it makes no sense to perform your check against N being equal to exactly 5
Why? If Smallest is equal to 42 and Array[3] is also equal to 42, why overwrite Smallest with 42 when it is already 42?
So? After setting Smallest equal to Array[0], what is the purpose of checking if Array[i] is less than or equal to Smallest when i = 0?
I was confuse by looking old post #25. I have redraw flow chart. and I think this is answer of all questions
upload_2017-10-3_17-26-55.png
 

WBahn

Joined Mar 31, 2012
32,874
Look at your decision block in your loop and THINK about what it is saying.

It says that you want to replace the value stored in Smallest with the value at the current location in the array IF the value at the current location of the array is LARGER than the value stored in Smallest.

Does that make sense to you?

You've got some variable name case issues, namely you talk about two different arrays, a and A. In almost all programming languages, these would be, in fact, two different and completely unrelated arrays.

You are still wasting the first pass through the loop doing something that has already been taken care of. You've already dealt with a[0]. So let the loop start work beginning with a[1].
 

WBahn

Joined Mar 31, 2012
32,874
Does it make any sense?
View attachment 136413
You really need to pay attention to detail (especially since programming is all about careful attention to minute details!).

You set I = 0.

Then you do something that doesn't involve I (though it could).

Then you set I = i + 1

What is i equal to?

Ignoring the case mismatch, you at least now would not duplicate effort on the first pass.

But now look at your exit test. Before you exited the loop if the answer to I < N was NO. Now you exit if the answer to I > N is NO. Why did you change it?

Then you haven't addressed the big issue (which is another case of having gotten this part right previously and then, for some unknown reason, changing it so that it no longer works).

Your current flowchart changes the value of Smallest whenever the value in the current location in the array is LARGER than Smallest. Why do you want to do this????

You seem to be making random changes without intent, just hoping that, at some point, it will all just happen to work.
 

simozz

Joined Jul 23, 2017
170
Perhaps the flow charts are not the best way to plan or learn algorithms (e.g. in the book Introduction to Algorithms these are not used).
I would start writing on a piece of paper a simple random series, e.g. {10, 8, 11, -4, 7, 5, 12, 1} then reasoning on the steps to use to order it.
simozz
 

philba

Joined Aug 17, 2017
959
Perhaps the flow charts are not the best way to plan or learn algorithms (e.g. in the book Introduction to Algorithms these are not used).
I would start writing on a piece of paper a simple random series, e.g. {10, 8, 11, -4, 7, 5, 12, 1} then reasoning on the steps to use to order it.
simozz
+1 to that. over the years I've degenerated into using a kind of deviant C pseudo-code but it doesn't have to "compile" or even have pedantic correctness. I save that stage for actual code.
 

xox

Joined Sep 8, 2017
936
Another useful approach is to simply start writing out the code as best you can and just print the value of everything that changes each step of the way. For example:

Code:
#include <stdio.h>
#include <stdlib.h>
/*
   Simple macro for inspecting *integer* values
*/
#define SHOW(expression) SHOW_(#expression, expression)
/*
   Function invoked by macro (not used directly in code)
*/
int SHOW_(const char* text_of_expression, int value_of_expression)
{
   printf("%s :: %d\n", text_of_expression, value_of_expression);
   return value_of_expression;
}

/*
   A little helper function to randomly populate an integer array
*/
#include <time.h>
void randomize_integer_array(int* data, size_t length, int low, int high)
{
   static int initialized = 0;
   if(!initialized)
   {
     srand(time(NULL));
     initialized = 1;
   }
   int range = (high - low) + 1;
   for(size_t index = 0; index < length; ++index)
   {
     int value = rand();
     if(range != 0)
       value %= range;
     data[index] = low + value;
   }
}

/*
   Example program using macro to examine values step-by-step...
*/
int main(void)
{
   const size_t N = 5;
   int a[N];
   randomize_integer_array(a, N, 30, 50);
   puts("~ Values ~");
   for(size_t i = 0; i < N; ++i)
     SHOW(a[i]);
/*
   First attempt to find the smallest number in an array:
*/
   puts("BEGIN");
   int smallest = a[0];
   SHOW(N);
   SHOW(smallest);
   puts("LOOP START");
   for(size_t i = 0; i < N; ++i)
   {
     SHOW(i);
     SHOW(a[i]);
     if(SHOW(a[i] > smallest))
     {
       SHOW(smallest = a[i]);
     }
     SHOW(smallest);
   }
   puts("LOOP COMPLETE");
   SHOW(smallest);
   puts("FINISHED");
/*
   Repeat: inspect output from above, fix code logic accordingly...
*/
   return 0;
}
 
Last edited:

philba

Joined Aug 17, 2017
959
When I was in grad school, I taught a number of programming courses. For the intro course (CS 101) we used actual code for examples rather than flow charts. None of their assignments were terribly complex. For advanced courses, we used pseudo-code. It was unfortunate that in CS 101, 15-20% of the students just never got it. They couldn't mentally connect the pieces together and a code walk through left them baffled. I never did figure out how to reach them.
 
Top