473,473 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to find smallest and largest number?

7 New Member
Hello there
I have problem with writing a program could you please help me with that ?

Write a program that will find the smallest, largest and average of the values in a collection of N positive integer numbers.

Thanks,
Oct 18 '14 #1
12 4098
weaknessforcats
9,208 Recognized Expert Moderator Expert
Forget about C for the moment. First, how would you find the largest integer in a collection of integers?

Often it's not the programming that hard, it's the method you use for the solution.

If you have no method, then you can never solve the program with code.
Oct 19 '14 #2
Jojy5555
7 New Member
Yes this is my problem I don't know how to get the method .

Could you please help me with that ?
Oct 19 '14 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Pick the first number as the largest number.

Write it on a piece of paper.

Pick another number.

If it's bigger than the number on the paper, erase the number on the paper and replace it with this one.

Pick another number and repeat until you have picked all the numbers.

The largest number will be on the piece of paper.

All you have to do now is translate this method into C.

Don't worry about finding the smallest number (though that is obvious now) or the average. Just get your program to do this one thing.

Post your code if you get stuck.
Oct 19 '14 #4
Jojy5555
7 New Member
Thanks for trying to help me

I understood that what did you say before
but the numbers the program should get them from the user

I've written my code please check it for me if there anything wrong


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #defin sentinel=-1
  3. int main ()
  4. {
  5. Int N1, N2; 
  6. double average; 
  7.  
  8. printf("Enter two numbers or press -1 to exit\n");
  9. scanf ("%d%d",&N1, N2);
  10.  
  11. while (N1!=-1 ||N2!=-1)
  12.  
  13. {
  14. if (N1> 0 && N2> 0)
  15. average=(N1+N2)/2.0;
  16. printf ("the average is %f ,average);
  17. }
  18. if (N1> N2)
  19. {
  20. printf ("the largest is %d , the smallest is %d,N1, N2);
  21. }
  22.  break; 
  23. if (N2> N1)
  24. {
  25. printf ("the largest is %d , the smallest is %d , N2, N1);
  26. }
  27. }//end while
Oct 19 '14 #5
Jojy5555
7 New Member
I'm waiting for your help.

Thanks a lot
Oct 19 '14 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
Expand|Select|Wrap|Line Numbers
  1. printf("Enter two numbers or press -1 to exit\n");
  2. scanf ("%d%d",&N1, N2);
  3.  
  4. while (N1!=-1 ||N2!=-1)
  5.  
  6. {
  7. if (N1> 0 && N2> 0)
  8. average=(N1+N2)/2.0;
  9. printf ("the average is %f ,average);
  10. }
  11. if (N1> N2)
  12. etc...
is a very good start, There is a problem here:
Expand|Select|Wrap|Line Numbers
  1. scanf ("%d%d",&N1, N2);
  2.  
which should be:
Expand|Select|Wrap|Line Numbers
  1. scanf ("%d%d",&N1, &N2);
  2.  
And here:
Expand|Select|Wrap|Line Numbers
  1. while (N1!=-1 ||N2!=-1)
  2. etc...
Why the loop? The values of N1 and N2 won't change.

Unless you intended that they change in which case the scanf needs to be inside the loop rather than outside.

Finally, the average should be an int and not a double. That is the average of 2 and 1 is 3/2 which is 1 and not 1.5. Integers have no decimal portions so 1.5 is not an int. This may look odd to you but integer division works this way.

For an average of 1.5, N and N2 would need to be floating point, like maybe a double. Then you would have 2.0 and 1.0 and the average would be 3.0/2.0 which is 1.5.
Oct 19 '14 #7
Jojy5555
7 New Member
Thanks a lot I understood now when should I use the loop
you was very helpful thank you so much
Oct 19 '14 #8
Snip
8 New Member
Here:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4.  
  5. int i,s,x;
  6. s=0;
  7. for(i=0;i<=5;i++){
  8.  
  9.                    printf("Digite um valor: ");
  10.                    scanf("%d",&x);
  11.                    if(x>=s) s = x;
  12.  
  13.                   }
  14.  
  15.  
  16. printf("MAX: %d \n",s);      
  17. system("pause");  
  18.  
  19. }
  20.  
the user give u 5 inputs and then you show him the maximun value.
How:
You create a variable called "s" with 0 as initial value, then the user give the first value and the variable s eats it. Then if the following value is greater than the first, s will be it, and so on, in the end "s" will be the greater of all.
Oct 19 '14 #9
Snip
8 New Member
Getting the smalles and the largest:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4.  
  5. int i,s,x,lim;
  6. s=0;
  7. for(i=0;i<=5;i++){
  8.  
  9.                    printf("Type a value: ");
  10.                    scanf("%d",&x);
  11.                    if(x>=s) s = x;
  12.                    if(i==0) lim = x;
  13.                    if(x<lim) lim = x;
  14.  
  15.                   }
  16.  
  17.  
  18. printf("MAX: %d, MIN: %d",s,lim);      
  19. system("pause");  
  20.  
  21. }
  22.  
  23.  
Oct 19 '14 #10
Jojy5555
7 New Member
I don't know what do you mean by (system ("pause")) ?

I'm still in the beginning of this program .

Thanks a lot
Oct 19 '14 #11
Snip
8 New Member
Im a beginner in C too, system("pause") is used to stop the screen that appear to show the result. It is from the <stdlib.h> . So the screen doesn't close very fast. But it has nothing to do with the logic of what you want to do.
Oct 19 '14 #12
Jojy5555
7 New Member
What if I used a break ? Is it wrong ?
Oct 19 '14 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Klaus Drechsel | last post by:
My Problem: The Function Dispose on a IDisposable shuld be called on the last reference to this instance. Can I get the number of references. to an object? Klaus Drechsel
4
by: Mahesh BS | last post by:
Hello, I need to write a query to find out a set of missing number in a given sequence. Eg : a Column in some table has the following data
1
by: Simon | last post by:
Dear reader, Is there a function or VBA code available to find the week number out of a date field. You can find the year with Year(date field) but now I need the week number out of a...
1
karthickkuchanur
by: karthickkuchanur | last post by:
how to find the seventh largest number in a column for example 1,2,3,4,5,6,7
1
by: joe22 | last post by:
I made a list of random numbers. From that list, how can I find the largest number and the corresponding month?
4
by: aimi95 | last post by:
Hello all, I'm new here. I found these forums to be very useful so I decided to join. My question is that, my teacher specially gave a task to me. We just started problem solving and C++ coding so...
4
by: irslan rafique | last post by:
Hi, In access 2010,I have one query based on a table.There are 3 different columns: Commission 1, Commission 2 and Commission 3. 4th column is: Total Commission So i want query automatically...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.