473,585 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I can't get the Largest and Smallest numbers using functions in C++.

2 New Member
#include<iostre am>
using namespace std;
int computeTotal();
int findSmallest();
int findLargest();
int main()
{
int n1,n2,n3,n4,n5, total,average,s mallest,largest ;

cout << "Number 1:";
cin >> n1;
cout << "Number 2:";
cin >> n2;
cout << "Number 3:";
cin >> n3;
cout << "Number 4:";
cin >> n4;
cout << "Number 5:";
cin >> n5;

total = n1 + n2 + n3 + n4 + n5;
cout << "Total:"<<total <<endl;

average = (n1 + n2 + n3 + n4 + n5)/5;
cout << "Average:"<<ave rage<<endl;


cout << "Smallest:"<<sm allest<<endl;

cout << "Largest :"<<largest<<en dl;

system("pause") ;
return 0;
}//end of main()
int computeTotal(in t n1,int n2,int n3,int n4,int n5)
{
int total=0;
total = n1+n2+n3+n4+n5;
cout << "Total :"<<total<<"\n" ;
return total;
}
int computeAverage( int n1,int n2,int n3,int n4,int n5)
{
int average;
average = (n1+n2+n3+n4+n5 )/5;
cout << " Average :"<<average<<"\ n";
return average;
}
int findSmallest(in t n1,int n2,int n3,int n4,int n5)
{
int smallest;

smallest = n1;

if(n2<smallest)
smallest = n2;
if(n3<smallest)
smallest = n3;
if(n4<smallest)
smallest = n4;
if(n5<smallest)
smallest = n5;

return smallest;
}
int findLargest(int n1,int n2,int n3,int n4,int n5)
{
int largest;
largest = n1;

if(n2>largest)
largest = n2;
if(n3>largest)
largest = n3;
if(n4>largest)
largest = n4;
if(n5>largest)
largest = n5;


return largest;
}
Feb 6 '15 #1
4 1294
weaknessforcats
9,208 Recognized Expert Moderator Expert
You display your largest and smallest numbers in main() but you never call the functions findSmallest and findLargest.
Feb 6 '15 #2
ArtStar
2 New Member
I'm new of C++ programming. if you know better ways that can help me display the exact value for largest and smallest. thanks ;)
Feb 6 '15 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
There are better ways to display the largest and smallest numbers but, as you say, you are just beginning. The first requirement of a program is that it works. After that, it's just fine points. I would not worry about this but I would just continue on. You will get better as time goes by.
Feb 6 '15 #4
jaseel97
16 New Member
use this:
int n[4];
cout<<"Enter the four numbers:";
cin>>n[0]>>n[1]>>n[2]>>n[3];

int largest=a[0];
int smallest=a[3];//these are temporary.

for(int i=0;i<4;i++)
{
if(a[i]<smallest)
smallest=a[i];
if(a[i]>largest)
largest=a[i];
}
you may split it up into two loops if you want them as separate functions
Feb 10 '15 #5

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

Similar topics

5
3330
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run...
1
2018
by: sekitoleko | last post by:
I need some help on representing rational numbers using structures
6
2622
by: Nirjhar Oberoi | last post by:
Hi, Can you add two numbers using a Single Variable? :-) If yes then show me the code!!! Regards Nirjhar
4
2074
by: GeekBoy | last post by:
I am reading a file of numbers using for loops. The numbers are in a grid as follows: 8 36 14 11 31 17 22 23 17 8 9 33 23 32 18 39 23 25 9 38 14 38 4 22 18 11 31 19 16 17 9 32 25 8 1 23
6
4638
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might...
3
7918
by: jishara | last post by:
How to write a program in a one page to find the sum of two numbers using php $_POST method.
1
2722
by: sathyak83 | last post by:
i want to format the numbers using css for each country ie if country is france the format=0.0.0 if country is UK=00.00 i use the below code <LINK REL=stylesheet HREF="<%= new XMLReaderAdapter().setLocale((Locale)request.getAttribute("locale")) %>" TYPE="text/css"> but i get a exception
0
7908
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
7836
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
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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
8212
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...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.