C Program to Find the Largest Number in an Array

Thread Starter

Parth786

Joined Jun 19, 2017
642
try entering in data that does not have the smallest or largest as the first or last input value.
I have just replaced following lines
Code:
 {
        if (largest< array[i])
            largest = array[i];
       else if(    smallest > array[i] )
            smallest = array[i];              
    }
Result
size of the array
: 6
Enter elements of the array 6
: 4
5
6
7
1
2
largest element in array is 7
smallest element in array is 1
 

WBahn

Joined Mar 31, 2012
32,876
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?

Look at this program. it can find out smallest number in array but I need to enter the size off array.
You are still not indenting your code properly.

Also, add meaningful comments.

In the following code, I have not changed your code one bit. I've merely properly intended it and put in some comment.

C:
#include <stdio.h>

int main(void)
{
   int array[10], size, i, smallest;

   // Get the amount of data from user
   printf("size of the array \n: ");
   scanf("%d", &size);

   // Get data from user
   printf(" Enter  elements of  the array %d \n : ", size);
   for (i = 0; i < size; i++)
      scanf("%d", &array[i]);
  
   // Set the smallest so far to be the first data item
   smallest = array[0];

   // Check each remaining data item to see if it is the new smallest item so far
   for (i = 1; i < size; i++)
   {
      if (smallest > array[i])
      smallest = array[i];
   }

   // Print out the smallest data item seen
   printf(" smallest element in array is %d \n :", smallest);

   return 0;
}
ls it possible to write program that can find out smallest number without entering the size of array?
The only other way to do it would be to have the user be able to indicate when they are finished entering data.

If the data values have to be positive, then they could enter a negative number to do so.

If the data values can be anything, then you can bring them in as a string and check if the string indicates the end of the data. If not, convert the string to an int (or double, as needed).

Go through my code above and remove everything except the comments. Do you see how just the comments make reasonable pseudocode?

One way to code is write the pseudocode as comments and then go back and enter the code to perform the task given by each comment, thus developing your code according to a plan and documenting your code at the same time.
 

WBahn

Joined Mar 31, 2012
32,876
Look at flow chart for smallest and largest numbers in array
Note : I haven't written code for this flow chart. if there is no problem in flow chart then I will write code
That flowchart has serious problems.

Let's tackle them one at a time.

What is your first loop supposed to accomplish? It does nothing except set size equal to i.
 

MrChips

Joined Oct 2, 2009
34,827
@Parth786
Don't do both smallest and largest in the same program for now.
Let's just focus on one, e.g. do largest member.

Focus on the flowchart and get it right. Keep it as simple as possible.
If you are having trouble we will show you how.
 

MrChips

Joined Oct 2, 2009
34,827
You need help. When I said simple, I meant simple.

Here is what your flowchart should look like:

SEARCH.jpg

Change the wording to suit your problem.
 

WBahn

Joined Mar 31, 2012
32,876
does it make any sense ?
This is MUCH better.

Two obvious things looking at it just briefly.

Your initial loop is NOT getting a number from the array. Look at the code fragment that you had previously:

C:
   for (i = 0; i < size; i++)
      scanf("%d", &array[i]);

Here you single statement is accomplish two tasks.

The scanf() is getting a number from the USER. Then it is STORING it into the array. Your flow chart needs to reflect that.

Second, you have a bunch of "Stop" statements in your flowchart. As has been pointed out previously, "Stop" means STOP!!!! You are done! You are not doing ANYTHING more. The program if finished!

Where you have the Stop boxes in your flowchart, you just want the paths to come back together, just like they come back together above the test for the first loop.

Third, look at where you go after incrementing i in the second loop? How can you get out of this second loop? Remember, I'm not asking what you want it to do, I'm asking about what it does.
 
Last edited:

WBahn

Joined Mar 31, 2012
32,876
You need help. When I said simple, I meant simple.

Here is what your flowchart should look like:

View attachment 136798

Change the wording to suit your problem.
What is the circle with the "+" sign? In most block diagram structures it means to add the two incoming signals to produce the outgoing signal. Here I can only assume it means to increment the control variable.

If so, that flowchart isn't going to match up with the normal language constructs. It requires that, after initializing the control variable, you immediately increment it before performing the first test. That would require a do/while() structure. But such a structure will execute the ENTIRE loop body before performing the first test.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You need help. When I said simple, I meant simple.
Change the wording to suit your problem.
I have question. Do you judge effort by number of posts? it has been complete above 100 posts. I am feeling that I am taking so much times. Am I wasting your time. Now should I stop for some time. Please answer.
I have language problem but I am trying to improve
 

spinnaker

Joined Oct 29, 2009
7,830
I have question. Do you judge effort by number of posts? it has been complete above 100 posts. I am feeling that I am taking so much times. Am I wasting your time. Now should I stop for some time. Please answer.
I have language problem but I am trying to improve

Mr Chips would not continue to post if he thought he was wasting his time.

No offense but I think the one wasting their time is you. You are trying to learn programming a small fragment at a time over the internet. That is no way to learn.

In my opinion, the best way to learn is an instructor led course. They will help you build your programming skills. They will have you write programs that are simple at first then help you build on what you learn. The classic I remember from those days is to find the palindromes (words that are spelled the same forward and backward) . Each of these little homework exercises help you to learn to think programing.

If you can't afford an instructor lead class then try to find one online. There are plenty of them.

Programming takes a long time to learn. It does not happen overnight. And some people just never get it. Very smart people just lousy programmers. Their brain just does not think that way.
 

nerdegutta

Joined Dec 15, 2009
2,689
If you can't afford an instructor lead class then try to find one online. There are plenty of them.
I think that this is exactly what he is doing.
MrChips and Wbahn tell him to keep it simple. I'm sure that they'll step it up a notch as soon as he mange to match his flowchart with the code.

As a by-stander, I think he has come a long way in a few days. If he only focused on this thread ...
 

WBahn

Joined Mar 31, 2012
32,876
I have question. Do you judge effort by number of posts? it has been complete above 100 posts. I am feeling that I am taking so much times. Am I wasting your time. Now should I stop for some time. Please answer.
I have language problem but I am trying to improve
You definitely seem to be struggling with basic concepts a lot more than most people do. That MAY indicate that you will never be able to develop the skills needed to successfully write programs -- and if that's the case then it's good to know that and to also know that a lot of very successful people also can't do so. The way of thinking that lends itself to being a successful programmer can actually be a hindrance in other fields of endeavor. Very good programmers tend to be highly field independent, but the more field independent someone is, the harder a time they typically have performing tasks that require keeping "the big picture" always in mind. Highly successful project managers, CEOs, and politicians are seldom strongly field independent.

But I still only said MAY indicate. It's also possible that you will have an "ahah" moment and suddenly things will click and you will almost immediately look back and wonder how the heck you struggled so much with these concepts.

You are putting in the effort (and, no, number of posts is a poor indicator of level of effort -- it's the content of the posts that matter) and something just isn't clicking yet. Maybe it never will, maybe it will today or tomorrow. There's no way to know. At some point YOU will need to decide if it is time to throw in the towel. You HAVE made progress, so certainly not all hope is lost.
 

spinnaker

Joined Oct 29, 2009
7,830
You definitely seem to be struggling with basic concepts a lot more than most people do. That MAY indicate that you will never be able to develop the skills needed to successfully write programs -- and if that's the case then it's good to know that and to also know that a lot of very successful people also can't do so. The way of thinking that lends itself to being a successful programmer can actually be a hindrance in other fields of endeavor. Very good programmers tend to be highly field independent, but the more field independent someone is, the harder a time they typically have performing tasks that require keeping "the big picture" always in mind. Highly successful project managers, CEOs, and politicians are seldom strongly field independent.

But I still only said MAY indicate. It's also possible that you will have an "ahah" moment and suddenly things will click and you will almost immediately look back and wonder how the heck you struggled so much with these concepts.

You are putting in the effort (and, no, number of posts is a poor indicator of level of effort -- it's the content of the posts that matter) and something just isn't clicking yet. Maybe it never will, maybe it will today or tomorrow. There's no way to know. At some point YOU will need to decide if it is time to throw in the towel. You HAVE made progress, so certainly not all hope is lost.
I think the TS might be jumping into the deep end of the pool without first learning the basics in the shallow end.
 

MrChips

Joined Oct 2, 2009
34,827
What is the circle with the "+" sign? In most block diagram structures it means to add the two incoming signals to produce the outgoing signal. Here I can only assume it means to increment the control variable.

If so, that flowchart isn't going to match up with the normal language constructs. It requires that, after initializing the control variable, you immediately increment it before performing the first test. That would require a do/while() structure. But such a structure will execute the ENTIRE loop body before performing the first test.
The circle with a + sign is simply a junction or program node. You can remove it if it makes you feel better.
 

MrChips

Joined Oct 2, 2009
34,827
Here is my suggestion of words to add to the flowchart.

Normally, the pseudocode on the right would go in the box. I present them outside so that you can see how they fit into the control structure.
For completeness, each of the IF statements should be expanded into an IF-THEN-ELSE structure. ( I will show this in my next post.)

At this stage, there is still no code. Code will come later.
I make sure that what is shown is valid and flawless. If this program flow is incorrect then your code will also be incorrect.

The test for N > 0 is a safety check. If one can assume that N will always be greater than 0 then this is unnecessary. What would be the point of searching an empty array?

Notice the simplicity of the algorithm, flowchart and pseudocode.

largest_example.jpg
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I Made a Mistake. I Sincerely apologise for my mistakes. I feel very bad to know that I am wasting your time. I am really sorry for repeating same mistake. I have been told you english is not my native language so that's why I struggle at some point

Please look at once again my work.
 

Attachments

MrChips

Joined Oct 2, 2009
34,827
I Made a Mistake. I Sincerely apologise for my mistakes. I feel very bad to know that I am wasting your time. I am really sorry for repeating same mistake. I have been told you english is not my native language so that's why I struggle at some point

Please look at once again my work.
Well done!
 
Top