C Program to Find the Largest Number in an Array

xox

Joined Sep 8, 2017
936
(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.)
I've always preferred the "learn-by-example" method myself, mostly because it allowed me to get up and running quickly using a working program, seeing how others go about implementing program logic and of course taking note of the idiosyncrasies of writing code in that particular language. Ultimately though, you're probably better off trying different approaches to see what works best for you.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
I'd recommend putting the corresponding C code right next to the flowchart.
.
OK Look at following example. Does it make any sense ? If everything is right at this time, Then I will try to make some complex flow chart. I want to know How do you rate me out of 10 for flow chart design
 

Attachments

WBahn

Joined Mar 31, 2012
32,876
Both Ex1 and Ex2 have the right topology, but don't reflect the code.

At the level of detail you are working at right now, you should be able to hand someone the flowchart and they should be able to write the C code from it.

As for rating your skills at flow chart design, honestly I'd have to put them at about a 1 or 2. But you are putting in a lot of effort and have made some progress.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Both Ex1 and Ex2 have the right topology, but don't reflect the code.

At the level of detail you are working at right now, you should be able to hand someone the flowchart and they should be able to write the C code from it.

As for rating your skills at flow chart design, honestly I'd have to put them at about a 1 or 2. But you are putting in a lot of effort and have made some progress.
OK. I will do more practice. Now I am able to make flow chart for If else Statement , While loop and For loop. I tried to make some useful flow chart in post #71. But according to you that was not useful example. Now Can you tell me please. Which simple example should I take to make flow charts?
 

WBahn

Joined Mar 31, 2012
32,876
OK. I will do more practice. Now I am able to make flow chart for If else Statement , While loop and For loop. I tried to make some useful flow chart in post #71. But according to you that was not useful example. Now Can you tell me please. Which simple example should I take to make flow charts?
How about something simple to start with and then add some complexity a bit at a time.

Ask the user for how many integer data values they are going to enter (with a max of 10). Then ask them for each value, one at a time. Then output the average of the values.

Next output the min, average, and max.

Next output the values that are above the average.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
How about something simple to start with and then add some complexity a bit at a time.
.
Program that take input from user and print output on screen
C:
#include<stdio.h>
int main (void)
{
     int number;
    /* get number from user */
     printf(" user input " );
     scanf ("%d", &number );
     printf(" output %d \n ", number);
     return 0;
}
How to mention scanf in flow chart. I have never done this before. Which box is use for scanf statement in flow chart. printf and scanf both are statement. I think I can use rectangular box. so for above program I need four rectangular boxes. am I correct ?I am waiting for confirmation. If yes then I will go for further process
 

WBahn

Joined Mar 31, 2012
32,876
Program that take input from user and print output on screen
C:
#include<stdio.h>
int main (void)
{
     int number;
    /* get number from user */
     printf(" user input " );
     scanf ("%d", &number );
     printf(" output %d \n ", number);
     return 0;
}
How to mention scanf in flow chart. I have never done this before. Which box is use for scanf statement in flow chart. printf and scanf both are statement. I think I can use rectangular box. so for above program I need four rectangular boxes. am I correct ?I am waiting for confirmation. If yes then I will go for further process
You are on the right track. At the most basic level, you have four statements, so four boxes.

It is common practice to use a circle as both the entry and exit points, so your "return 0" can go in a circle.

Input and output are commonly put in a parallelogram (usually a rectangle that is tipped to the right) so your scanf() and printf() statements can go in there.

There are lots of other shapes that can be used in a flowchart, but these basic ones are more than sufficient for the level you and I normally work at.
 

WBahn

Joined Mar 31, 2012
32,876
Look at following PDF file. Does it make any sense ?
That's workable.

Normally you don't put variable declarations in a box (or in a flowchart at all) because these aren't tasks that get executed plus they are overhead associated with the programming language. In the ideal world, a flowchart provides a description of the tasks that the program performs in order to solve a problem in a completely language-agnostic way. The person reading it should be free to look at what the program has to do and then choose a language that they think is best suited to performing them. In practice, the details of how algorithms are structured do reflect the target language, sometimes to a very large degree, and so this is not always possible.

Since this is flowchart for a function (namely main()) the arguments that it accepts (which there aren't any here) should be listed in the "Start" block (so that the reader knows what information is being passed in and that they have as their starting point) and the values being returned should be listed in the "End" block. In the bigger picture, if you flowcharted a larger program you would usually have many flowcharts (at least one for every function) and so using the function prototype (possibly without data types) as the text in the "Start" block and the appropriate return statement would be used as the text for the "End" block.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
That's workable.
if you flowcharted a larger program you would usually have many flowcharts (at least one for every function) and so using the function prototype (possibly without data types) as the text in the "Start" block and the appropriate return statement would be used as the text for the "End" block.
  1. If there are the two or more routine in program. how do we make flow chart. For Example LED blinking c program. There are two routine main routine and Delay routine. I think we will make two flow chart one for main routine and other for Delay routine and We will call delay routine via doted line. I will make some complex flow charts
  2. I wrote program for find out smallest and largest number in array. In that program I already know the length of array but what happen when we don't know the length of array if user enter any numbers array we don't know length then how to write program to find out smallest number in array ?
 

WBahn

Joined Mar 31, 2012
32,876
  1. If there are the two or more routine in program. how do we make flow chart. For Example LED blinking c program. There are two routine main routine and Delay routine. I think we will make two flow chart one for main routine and other for Delay routine and We will call delay routine via doted line. I will make some complex flow charts
No need for a dotted line. Have the flowchart call the function in a box. Then, if nothing else, the reader can look at the opening bubbles on the other flowcharts for the one for that function.

  1. I wrote program for find out smallest and largest number in array. In that program I already know the length of array but what happen when we don't know the length of array if user enter any numbers array we don't know length then how to write program to find out smallest number in array ?
What values in your program would need to change if the amount of data is different? Make those values a variable whose value is entered by the user.

There is a caveat on this, in C, because (ignoring variable-length arrays that were added in C99) you have to define the size of arrays at compile time. The way around this is to use dynamically allocated memory. The other way to deal with it is to allocate an array for the largest amount of memory that you will let the person use and include that max in the prompt. If they enter a number bigger than that, print out an error message and end the program. Later, you can modify it to keep asking them for a valid number of data items until they do so. Later, you can remove the max size restriction by dynamically allocating the memory. These are all good incremental steps to take in your learning progression. Don't try to do them all at once. One at a time.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
@Parth786 I have prepared a blog on Structured Programming for your benefit.
Really nice work. After completing smallest and largest number program. I will try for sorting. like arrange numbers in ascending order or descending order. Am I attempting complex example?
What values in your program would need to change if the amount of data is different? Make those values a variable whose value is entered by the user.
Look at this program. it can find out smallest number in array but I need to enter the size off array. ls it possible to write program that can find out smallest number without entering the size of array?
C:
#include <stdio.h>

int main()

{

       int array[10], size, i, smallest;

       printf("size of the array \n: ");

       scanf("%d", &size);

       printf(" Enter  elements of  the array %d \n : ", size);

       for (i = 0; i < size; i++)

        scanf("%d", &array[i]);

        smallest = array[0];

       for (i = 1; i < size; i++)

       {

          if (smallest > array[i])

          smallest = array[i];

       }

         printf(" smallest element in array is %d \n :", smallest);

   return 0;

}
 

MrChips

Joined Oct 2, 2009
34,827
Really nice work. After completing smallest and largest number program. I will try for sorting. like arrange numbers in ascending order or descending order. Am I attempting complex example?
Sorting is an excellent next step from finding the smallest and largest member of an array.
Before you do that, go back and recreate properly the flowchart for finding the largest member.
Next do the same for finding the smallest member.

Now combine the two and find the smallest and largest member together in the same program.

This will give you exercise in algorithm design and flowcharting.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Sorting is an excellent next step from finding the smallest and largest member of an array.
Before you do that, go back and recreate properly the flowchart for finding the largest member.
Next do the same for finding the smallest member.

Now combine the two and find the smallest and largest member together in the same program.

This will give you exercise in algorithm design and flowcharting.
I have just edited my program. This program can find smallest and largest number in array
C:
#include <stdio.h>

int main()

{

  int array[10], size, i, largest, smallest;

  printf("size of the array \n: ");

  scanf("%d", &size);

  printf(" Enter  elements of  the array %d \n : ", size);

  for (i = 0; i < size; i++)

  scanf("%d", &array[i]);

  smallest = array[0];
  largest  = array[0];
  for (i = 1; i < size; i++)

  {

  if (largest< array[i] &&smallest > array[i] )
  largest = array[i];
  smallest = array[i];
   
   
  }
  printf(" largest element in array is %d \n ", largest);
  printf(" smallest element in array is %d \n ", smallest);
 
  return 0;

}
size of the array
: 6
Enter elements of the array 6
: 9
8
7
6
5
4
largest element in array is 9
smallest element in array is 4
 
Last edited:
Top