C Program to Find the Largest Number in an Array

WBahn

Joined Mar 31, 2012
32,875
Flowcharts are good for some things, pseudocode for other things, ad-hoc on-the-fly coding for others. Furthermore, what tool is best for what task varies from person to person, the specifics of the problem at hand, and even for the same person it changes over time.

But if someone can't grasp the basic program flow of simple algorithms presented as a flowchart, or even walk through a flowchart accurately, then their problem isn't the choice of tool.
 

philba

Joined Aug 17, 2017
959
Flowcharts are good for some things, pseudocode for other things, ad-hoc on-the-fly coding for others. Furthermore, what tool is best for what task varies from person to person, the specifics of the problem at hand, and even for the same person it changes over time.

But if someone can't grasp the basic program flow of simple algorithms presented as a flowchart, or even walk through a flowchart accurately, then their problem isn't the choice of tool.
Truth.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You really need to pay attention to detail (especially since programming is all about careful attention to minute details!).
.
I have written code and This code is able to find smallest number in array. does code match with flow chart

C:
#include <stdio.h>
int main(void)
{
     int a[5] = {10,5,3,2,15};
     int N = 5;
     int i = 0;
     int smallest =  a[0];
     for(i=1; i<N; i++)
      {
          if (a[i] < smallest )
               {
                    smallest = a[i];
               }
        else
             {

              }
  }
                printf (" smallest value %d ", smallest);
                 return 0;
}
Result : smallest value 2

Flow Chart
upload_2017-10-4_10-18-28.png
Note : don't be confuse on variable name 'i' and 'I'. Both used for same purpose
 
Last edited:

philba

Joined Aug 17, 2017
959
Not bad.
  1. look up the sizeof() operator and see how you might use it to generalize your code.
  2. what is the purpose of your first assignment of i? Is it necessary?
  3. Your code and flow chart don't match. can you tell us what part doesn't?
  4. The else clause of the if() statement is unnecessary since it has no code.
  5. a stylistic point, indentation should indicate nesting level. Having code at the same nesting level but with different indentation leads to confusion. bonus points to correct your program. Also, be consistent with your bracket positioning.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
what is the purpose of your first assignment of i? Is it necessary?
No it is not necessary. without it program can work. I don't need to assign there I have to declared variable name before loop . because I already assigning i=1; in loop

Your code and flow chart don't match. can you tell us what part doesn't?
There are few points
1. I have not declared and assign array in flow chart
2 I have not made box for return value
3.I have not made box for header file

The else clause of the if() statement is unnecessary since it has no code
No need to do this. program only check one condition that is true condition
 

MrChips

Joined Oct 2, 2009
34,826
Look at your flowchart again.

After I++ your chart has two paths. This is not acceptable.

Related to this, your flowchart shows two ends, [End] and [End Loop].
Again this is not acceptable. All control structures must have a single Entry Point and a single Exit Point.

Please correct this.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Look at your flowchart again.

After I++ your chart has two paths. This is not acceptable.

Related to this, your flowchart shows two ends, [End] and [End Loop].
Again this is not acceptable. All control structures must have a single Entry Point and a single Exit Point.

Please correct this.
Does it make any sense? I have corrected only those things that you have pointed
upload_2017-10-5_0-5-34.png
 

JohnInTX

Joined Jun 26, 2012
4,787
You are not paying attention to @MrChips . You can't have 2 ways out of the i++ box - or any other box - without a test to see which way you have to go. And DONE is DONE. Why would/could/should there be anything after DONE??
 

Attachments

Thread Starter

Parth786

Joined Jun 19, 2017
642
You are not paying attention to @MrChips . You can't have 2 ways out of the i++ box - or any other box - without a test to see which way you have to go. And DONE is DONE. Why would/could/should there be anything after DONE??
I am making some flow charts for basic understanding. I will post as soon as possible
 

WBahn

Joined Mar 31, 2012
32,875
Draw a flow chart for the following building blocks:

A sequence of two statements.

An optional execution of a statement.

A selection between one of two statements.

A loop that executes while a certain condition is true.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Draw a flow chart for the following building blocks:

A sequence of two statements.

An optional execution of a statement.

A selection between one of two statements.

A loop that executes while a certain condition is true.
I have already made some flow chart. Please look at this example. If there is any problem regarding design please let me know? I will take time to make your example.
 

Attachments

xox

Joined Sep 8, 2017
936
At some point you're going to have to start writing code. Lots of it. Eventually the unfamiliar will become familiar and the whole process will flow much more naturally. I promise! You just have to practice, it's simple as that.

For instance, when I first started dabbling in electronics just a couple of months ago I didn't even know how to connect an LED properly. I hooked one up to a power source and fried it. In fact, I destroyed about a half dozen of them in about twenty minutes! Of course nowadays I wouldn't dare connect an LED without a current-limiting resistor or constant-current source. And I'm still very much a beginner but at least when I look at a circuit diagram it doesn't seem completely incomprehensible anymore. The brain just starts to recognize things like voltage dividers and current mirrors, transistor biasing setups and oscillator circuits, etc. Point is, it doesn't happen by magic, it's just a matter of applying yourself day after day.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Draw a flow chart for the following building blocks:

A sequence of two statements.

An optional execution of a statement.

A selection between one of two statements.

A loop that executes while a certain condition is true.
Look at following flow chart. does it work according your statement ?
 

Attachments

simozz

Joined Jul 23, 2017
170
Exaple 5 is wrong.
You write that the program should print values (1-9), but following the flow chart, you don't do that.
simozz
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Exaple 5 is wrong.
You write that the program should print values (1-9), but following the flow chart, you don't do that.
simozz
Yes you are correct. I have to mention rectangular box for print number inside loop. This program print number 0 to 10.
C:
#include<stdio.h>
int main ()
{
     int N;
    for (N=0; N<10; N++)
      {
          printf(" number %d \n", N);
      }  
  return 0;
}
 

WBahn

Joined Mar 31, 2012
32,875
I have already made some flow chart. Please look at this example. If there is any problem regarding design please let me know? I will take time to make your example.
I'd recommend putting the corresponding C code right next to the flowchart.

On the first one (the if()), it is usual to place the "True" condition in the straight line path (vertically below the diamond) and the have the "False" path route around the if() block. It is also customary (but not necessary) to put a question mark on the end of the test statement to emphasize that you are asking if N is equal to 5 and then branching based on the Yes/No answer to that question.

On the second one (the if()/else), have the two branches come back together before executing code that is common (i.e., not in one of the two blocks). As you have it, the corresponding code should have the "Stop" command in both the if() block and the else-block.

Your Example #3 makes no sense and you couldn't implement it in C using structured programming (you'd have to resort to a goto statement). The idea of a loop is that you stay in the loop as long as a test condition evaluates as True and, once it evaluates as False, you branch to the first block of code after (below) the loop code.

Your Example #4 is a disaster. You set N = 5 and then ask if N is equal to 6. If it isn't (which it isn't) then you immediately ask again if N is equal to 6. Since there is no means for the value of N to change, you are stuck in an infinite loop. In addition, you have the same problem as in Example #3. When you use a decision block to form a loop, only one branch can go upwards in the code.

Your Example #5 does not do what you indicate it should. It will simply print the value 10.

Your Example #6 looks valid (although the i==? in the one test is meaningless), but since it isn't tied to a task, there's no way to tell if it is doing what you intend. Although technically correct, having multiple arrows entering a block, such as your i++ block, is generally avoided. Instead, have the arrows come back together just above the i++ block and then have a single arrow come down into it.
 

WBahn

Joined Mar 31, 2012
32,875
Yes you are correct. This program print number 0 to 10.
C:
#include<stdio.h>
int main ()
{
     int N;
    for (N=0; N<10; N++)
      {
          printf(" number %d \n", N);
      }  
  return 0;
}
The braces should be incremented to the same level as the controlling statement. It makes it easier to read and avoids the code intentation racing away to the right as quickly.

Keep the levels of indentation clearly aligned. Yours drift all over the place.

You should also declare functions that take no arguments as explicitly having a void argument list.

C:
#include<stdio.h>
int main (void)
{
   int N;
   for (N=0; N<10; N++)
   {
      printf(" number %d \n", N);
   }  
   return 0;
}
This code will only print 0 through 9.

When N is 10, it fails the test since N is NOT less than 10.

Avoid using capitals for variables. These should generally be used for constants.
 

MrChips

Joined Oct 2, 2009
34,826
At some point you're going to have to start writing code. Lots of it. Eventually the unfamiliar will become familiar and the whole process will flow much more naturally. I promise! You just have to practice, it's simple as that.
(With proper fore-knowledge you would not have killed half a dozen LEDs.)

Eventually, yes. You can be self taught and can learn a lot from trial and error. However, you will eventually discover that when you need to write thousands of lines of code you'd wish that someone had shown you a better approach.

That approach is called Structured Programming and is normally taught in first year of any software engineering course. Structured Programming comes first. You write code afterwards.

I suspect that the TS has not been exposed to Structured Programming.

upload_2017-10-5_10-57-31.png

(Reminder to myself to write a blog on this.)
 

WBahn

Joined Mar 31, 2012
32,875
(With proper fore-knowledge you would not have killed half a dozen LEDs.)

Eventually, yes. You can be self taught and can learn a lot from trial and error. However, one will eventually discover that when you need to write thousands of lines of code you'd wish that someone had shown you a better approach.

That approach is called Structured Programming and is normally taught in first year of any software engineering course. Structured Programming comes first. You write code afterwards.

I suspect that the TS has not been exposed to Structured Programming.

View attachment 136625

(Reminder to myself to write a blog on this.)

Your #3 is generally known simply as a "while()" loop. A "do/while()" has the test at the bottom. You can also have an "until()" loop, which has the topological structure of your #3 but with the T/F labels reversed.

Most languages, especially the C-based languages, support while() and do/while() loops. There aren't many the support until() or do/until() loops (Ruby is the only one I can think of off the top of my head, but I know there are others).
 

MrChips

Joined Oct 2, 2009
34,826
Your #3 is generally known simply as a "while()" loop. A "do/while()" has the test at the bottom. You can also have an "until()" loop, which has the topological structure of your #3 but with the T/F labels reversed.

Most languages, especially the C-based languages, support while() and do/while() loops. There aren't many the support until() or do/until() loops (Ruby is the only one I can think of off the top of my head, but I know there are others).
Sorry, I just copied the images off the internet. You are correct.
I will correct these flowcharts when I create my own blog.
Thanks for the heads up.
 
Top