473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To find smallest and largest numbers out of 6 numbers by using not more than 6-8 line

4 New Member
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 I'm not very good at it but I'm good than rest of my class (according to my teacher) and he gives me certain tasks to do. My new task is to find smallest, largest and average of six numbers (we haven't studied arrays yet, so I'm bound not to use it)by usings 6-8 lines of main logic I tried to do it by this way:
Expand|Select|Wrap|Line Numbers
  1.  #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. int n1,n2,l=0,s=0,i;
  7.  
  8. cout<<"Enter Number: ";
  9. cin>>n1;
  10.  
  11. for(i=2; i<=6; i++)
  12. {
  13.     cout<<"\nEnter next number: ";
  14.     cin>>n2;
  15.  
  16.     if(n2>n1)
  17.     {
  18.         l=n2;
  19.         s=n1;
  20.     }
  21.     else
  22.     {
  23.         l=n1;
  24.         s=n2;
  25.     }
  26.     n1=n2;
  27. }
  28. cout<<"\nLargest number is"<<l<<"\nSmallest number is"<<s;
but it gives me wrong results. It seems like the program is making decisions based on the last two values for variable n2. Is my logic correct? or should I make some changes within the program?
Thanks in advance for your valuable time .
May 7 '14 #1
4 1415
stdq
94 New Member
Hello! You could solve this problem like this: use a for statement with i going from 1 to 6. At each iteration, input an integer and add it to a running total (initialized to 0). Also, if i is 1, initialize the largest and smallest variable with the current integer input. If not, check whether the current integer input is smaller than the smallest number so far. Apply the same logic for keeping track of the largest number. At the end of the for statement, find the average by dividing the running total by 6. =)
May 7 '14 #2
Luk3r
300 Contributor
Without overthinking it, it looks like this won't work without using a third integer as your "random" number. Also, your For loop was a little messy. I've written the following code and I hope it helps you!

Expand|Select|Wrap|Line Numbers
  1.  {
  2. int largeNumber;
  3. int smallNumber;
  4. int randomNumber;
  5. int i;
  6.  
  7. cout<<"Enter Number: ";
  8. cin>>randomNumber;
  9. largeNumber = randomNumber;
  10. smallNumber = randomNumber;
  11.  
  12.  for(i=0; i<5; i++)
  13. {
  14.     cout<<"\nEnter next number: ";
  15.     cin>>randomNumber;
  16.  
  17.     if(randomNumber > largeNumber)
  18.     {
  19.         largeNumber = randomNumber;
  20.     }
  21.     else
  22.         if(randomNumber < smallNumber)
  23.     {
  24.         smallNumber = randomNumber;
  25.     }
  26. }
  27. cout<<"\nLargest number is"<<largeNumber<<"\nSmallest number is"<<smallNumber;
  28. std::cin.get();
  29. getchar();

Edit**:
This line of code may not be necessary. I used it in Visual Studio so I could pause the output window:
Expand|Select|Wrap|Line Numbers
  1. std::cin.get();
  2. getchar();
May 7 '14 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Visual Studio will pause the output window automatically if you select "run without debugging" as opposed to "run". No need for code you might have to take out as you move from development to release.
May 7 '14 #4
Luk3r
300 Contributor
@weaknessforcat s, Thanks! I learn something new every day.
May 7 '14 #5

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

Similar topics

2
7355
by: mei xiao | last post by:
Hi, there, I have a small web service project written in C# and compiled using visual studio.net, but how can I compile it using command line? Thank you. -May
16
12772
by: codergem | last post by:
How to add two numbers without using the plus operator?
1
6754
by: Helge's | last post by:
I have a table like this: Numbers 10 20 30 40 How can I make a sql in Access? I want it like this:
2
3319
by: accessjunior | last post by:
I was wondering if there is any way to close Access using command line swithes? Currently I have a batch file that calls the .mdb runs a macro and compacts, then I would like to close it. Any suggestions? Thanks. DL
1
4302
by: swat | last post by:
hi im the beginner of the c++ ...how can i build the programm so that in the output box i can input a number n in the first line and in the second line i input n numbers on the same line with a space in between each number next to it..and the output will displayed the same numbers that i have entered but in a reverse way with space in between the...
2
11310
by: BDthatsme | last post by:
I can't get any of the various examples of keyboard input to work using command line PHP. I have Windows XP Pro SP 2 and PHP 5.1.6 (cli) (built: Aug 23 2006 16:35:53). I can't find any info about why it doesn't work, or a way that does work. Any suggestions? examples of attempted variations: (and I've tried others)
8
3458
by: Sunny | last post by:
Hi, do someone know, How we can find the smallest distance between a bunch of lat 7 long? Like I have 10 Latitude & Longitude. -73.924598,40.879010 -73.924506,40.878978 -73.924506,40.878978 -73.921406,40.878178 -73.921406,40.878178
1
1867
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?
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2353
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.