473,509 Members | 4,901 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<iostream>
using namespace std;
int computeTotal();
int findSmallest();
int findLargest();
int main()
{
int n1,n2,n3,n4,n5,total,average,smallest,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:"<<average<<endl;


cout << "Smallest:"<<smallest<<endl;

cout << "Largest :"<<largest<<endl;

system("pause");
return 0;
}//end of main()
int computeTotal(int 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(int 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 1291
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
3326
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...
1
2002
by: sekitoleko | last post by:
I need some help on representing rational numbers using structures
6
2617
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
2066
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
4630
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...
3
7909
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
2709
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...
0
7237
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
7137
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
7347
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,...
1
7073
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...
0
7506
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...
1
5062
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...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
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 ...

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.