473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

calculating min and max

i just wanna know the logic to find minimum and maximum between some random numbers such as
1 2 3 4 5 6 or 43 7 98 0 89 any ideas?PLS!
Nov 10 '07 #1
15 4205
oler1s
671 Expert 512MB
Forget about the code for a moment. Try coming up with an algorithm in plain english that lets you find the minimum and the maximum in a bunch of numbers.

Oh, and we aren't telling you the answer. We'll assist you if you make real attempts at thought and code.
Nov 10 '07 #2
Forget about the code for a moment. Try coming up with an algorithm in plain english that lets you find the minimum and the maximum in a bunch of numbers.

Oh, and we aren't telling you the answer. We'll assist you if you make real attempts at thought and code.
in english language i will compare the numbers like for
1 2 3 4
it will be like
1<2 >>>>>>1min 2max
2<3>>>>>>>2min 3max
3<4>>>>>>>3min 4max
then i will compare mins and maxes together like

1<2>>>>>1min 2max
2<3>>>>>2min 3max
__________
2<3 2min 3max
3<4 3min 4 max
one more time
1<2 1min 2max sooooo min will be 1
____________
3<4 3min 4max sooooo max will be 4

i thought about the code for 3 day can you guys at least give me a hint to statrt with!!!!
Nov 10 '07 #3
Studlyami
464 Expert 256MB
Interesting way of approaching the problem. In that method you work your way down, but what about a line that had over 100 values to compare. That method would take a while to go through.

What are you really interested in? minimum and maximum. Think about that, you only want to know minimum and maximum. How do you see if a number is the smallest number and then how do you see if that number is the largest number?
Nov 10 '07 #4
Interesting way of approaching the problem. In that method you work your way down, but what about a line that had over 100 values to compare. That method would take a while to go through.

What are you really interested in? minimum and maximum. Think about that, you only want to know minimum and maximum. How do you see if a number is the smallest number and then how do you see if that number is the largest number?
i guess i have to compare that number with the rest, then if it is higher it is a maximum and if its less its a minimum.
but in this method i have to compare each # to the rest.
i cant think of any other way, can you?
Nov 10 '07 #5
Studlyami
464 Expert 256MB
you don't have to compare each number to the rest, just compare it to the minimum value and the maximum value

ex:

num1 > maxValue if yes maxValue = num1
else
num1<minValue if yes minValue = num1

num2 > maxValue if yes max Value = num2
else
num1<minValue if yes minValue = num2

now just set this up in a loop and your good to go.
Nov 10 '07 #6
you don't have to compare each number to the rest, just compare it to the minimum value and the maximum value

ex:

num1 > maxValue if yes maxValue = num1
else
num1<minValue if yes minValue = num1

num2 > maxValue if yes max Value = num2
else
num1<minValue if yes minValue = num2

now just set this up in a loop and your good to go.
in here i just dont get what is the value of maxValue in num1>maxValue
i mean i dont know the minimum and maximum values, how can i compare the # to them?
Nov 10 '07 #7
Studlyami
464 Expert 256MB
Each one would have to have an initial value you can either set the first value to it then check each number against it. or you can set min/max to a number that would be easy to be overwritten (the 2nd way is not a good way of doing this sense you are putting constraints on the range of data).

so

max = num1;
min = num1;

now do the
num2 >max
else
num2<min

etc. You just need to set up a loop to loop through each integer value that you have.
Nov 10 '07 #8
Each one would have to have an initial value you can either set the first value to it then check each number against it. or you can set min/max to a number that would be easy to be overwritten (the 2nd way is not a good way of doing this sense you are putting constraints on the range of data).

so

max = num1;
min = num1;

now do the
num2 >max
else
num2<min

etc. You just need to set up a loop to loop through each integer value that you have.
you see the code that i wanna use the min and max is a fstream code which is
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.     ifstream infile;
  4.     ofstream outfile;
  5.     float num1,num2,num3,limit1=0,limit2=0,counter1=0,counter2=0,sum=0;
  6.     float aver;
  7.  
  8.     infile.open("testin.txt");
  9.     outfile.open("testout.txt");
  10.  
  11.     if (infile.fail() || outfile.fail())
  12.     {
  13.         cout<<"failed";
  14.         return 1;
  15.     }
  16. else
  17.     while(!infile.eof())
  18.     {
  19.         infile>>num1>>num2;
  20.         limit1=num1;
  21.         limit2=num2;
  22.  
  23.         while(counter1<limit1)
  24.         {
  25.         sum=0;
  26.         counter2=0;
  27.  
  28.         while (counter2<limit2)
  29.         {
  30.             infile>>num3;
  31.             outfile<<num3<<" ";
  32.             sum+=num3;
  33.             counter2++;
  34.  
  35.  
  36.         }
  37.         outfile<<"\nTotal      ="<<sum<<endl;
  38.         aver=sum/limit2;
  39.         outfile<<"Average    ="<<aver<<endl;
  40.         outfile<<"____________________"<<endl;
  41.         counter1++;
  42.  
  43.  
  44.         }
  45.  
  46.     }
  47.  
  48.         return 0;
  49. }
which gets an input file i format of
Expand|Select|Wrap|Line Numbers
  1.  2 3   2is the # of rows     and 3 is the number of #per line
  2. and then somthing like
  3. 1 2 3
  4. 4 3 2
  5.  
which can be with different number of rows or groups


and then the output is like
Expand|Select|Wrap|Line Numbers
  1. 1 2 3 
  2. Total      =6
  3. Average    =2
  4. ____________________
  5. 4 3 2 
  6. Total      =9
  7. Average    =3
  8. ____________________
  9.  
now i wanna add the min and max values below the Average for each row!!!!
i tried your suggestion but i could'nt get it right??????
Nov 10 '07 #9
Studlyami
464 Expert 256MB
okay first let me suggest that you rename some of your variables to better describe them. This would make it easier to help out. Sense you aren't storing any of the variables you are going to need a flag to tell you if you are in the beginning of a line or not. Lets use a boolean to determine this.
Expand|Select|Wrap|Line Numbers
  1.   //add to the top of the page with all your other variables
  2.   int min = 0; //initialize variables 
  3.  int max = 0; 
  4.  bool FirstItemInLine = true; 
  5.  
  6. //where you reset sum and the other number 
  7. min = 0;
  8. max = 0
  9. FirstItemInLine = true; // sense we just finished one line we need to assign min/max again
  10.  
  11.         while (counter2<limit2) //this is the loop in which you grab the line information
  12.         {
  13.             infile>>num3;
  14.             outfile<<num3<<" ";
  15.             sum+=num3;
  16.             counter2++;
  17.  
  18.             if(FirstItemInLine == true)
  19.             {
  20.                  FirstItemInLine = false; //only do this once per line
  21.                 min = num3;
  22.                 max = num3;
  23.             }
  24.            else
  25.            {
  26.                  if(num3>max)
  27.                        max = num3;
  28.                  else
  29.                       if(num3<min)
  30.                              min = num3;
  31.             }
  32.         }
  33.  
Nov 10 '07 #10
TANXXXXXXXXXX Studlyami
that worked great
but 1 thing
why do we have to use FirstItemInLine boolean (the flag)??
it wont work without the flag?
and can u explain real quick what happened?
Nov 10 '07 #11
Studlyami
464 Expert 256MB
sure thing. The problem you were running into before is that you grab a number from the file then you compare it to max then to min, but as you stated earlier how do we know what min and max is? Also sense we needed to find the minimum and maximum value per LINE we have to know when we hit a new line so that we can assign the first value grabbed from the file to min and to max. Now we are guaranteed that min and max are values which are contained in our line. Then for the following numbers that we get from the file we compare them to min/max (which starts off as the first value in the line).

The logic looks like this
before each line reset values
grab first number in line and assign it to min and to max.
add first number to sum
grab each number following the first number and check to see if it is smaller than min or greater than max. Then add it to sum (in our case we added it to sum first)


I hope that makes sense if not let me know and ill post a clearer explanation after i get some sleep. Also, sorry for the ramble im a little sleepy :)
Nov 10 '07 #12
Thank u very much
i got it after i review it 2 or 3 times and by reading your post!!!!!
sorry but i still dont get the flag that you have used completely!!!!!
i mean why did you use the flag here
____________________________________
//where you reset sum and the other number
min = 0;
max = 0
FirstItemInLine = true; // sense we just finished one line we need to assign min/max again //I DONT GET IT
________________________________
and then again in here
if(FirstItemInLine == true) //I THINK IVE GOT THIS PART
{
FirstItemInLine = false; //only do this once per line
min = num3; //I DONT GET THIS PART
max = num3;
}
__________________________________
at the end can you please tell me how to read a num in this case num3 twice in fstream?
Nov 10 '07 #13
Studlyami
464 Expert 256MB
Alright think back to the logic we came up with for finding minimum and maximum.
lets say our line looked like so:
100 8 150
25 75 50

our logic said to grab the first item (100) and assign it to min and max
so after the first item min = 100 and max = 100;
grab the next item (8) 8>max(which is 100) no. is 8<min(again which is 100) yes so min = 8; now max = 100, min = 8
grab the next item (150) is 150>max (still 100) yes so max = 150;
Okay our line is done. we need to grab the first item in the next line to assign the value to max and min (rather than comparing the first number to max and min). So the flag tells us that wee need to assign the value to max /min

I check the flag it is true(just started a new line)
{
set flag false
grab first item and ASSIGN it to max & min . So max = 25, min = 25.
}
I check the flag its false
{
grab next item (75) and COMPARE it to max & min. so 75> max? yes max = 75
grab next item (50) and compare it to max 50> max(75) no. 50<min(25) no.
max = 75, min = 25.

Expand|Select|Wrap|Line Numbers
  1. min = 0;
  2. max = 0
  3. FirstItemInLine = true; //start of new line so the first number in this line we need to ASSIGN the value to min and max rather than COMPARE it.
  4.  
  5. if(FirstItemInLine == true)// ASSIGN value grabbed from the text file(num3) and ASSIGN it to min and max 
  6.             {
  7.                  FirstItemInLine = false; //We just grabbed the first item in the line so    this must turn to false. 
  8.                 file>>num3;// num3 will retain this value untill we do a file>>num3 again, or until you assign a different value to it like num3=4;
  9.  
  10.                //lets say we grabbed a  45 from the file and that value is stored in num3.  so right now num3 = 45;
  11.                 min = num3;        //ASSIGN the first item in the line to min
  12.                 max = num3;       //ASSIGN the first item in the line to max
  13.  
  14.                 // Right now     min = 45.  max = 45
  15. }
  16. else //FirstItemInLine is false
  17. {
  18.      //Grab next item and compare it to min and max.
  19.      file>>num3;   //lets say we grabbed 30;
  20.     if(num3> max)  // 30>45    NO
  21.              max = num3;
  22.    else
  23.         if(num3<min)// 30<45  YES
  24.              min = num3;
  25.  
  26.             // now max = 45,  min = 30
  27. }
  28. __________________________________
  29.  
at the end can you please tell me how to read a num in this case num3 twice in fstream?
why do you need to
when you do a file>>num3. that value in the file is stored as the integer num3. That value wont change untill you tell the program to change it by doing another file>>num3 or a num3 = 5. I tried to address that when i broke the program down above.
Nov 10 '07 #14
Just do a loop and compare one to the next and have a new variable hold the max number, its easy to do with an array.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6.  
  7.       int grades[10]={1,5,3,7,5,9,7,11,9,8};
  8.       int max=0;
  9.       for(int i=0;i<=9;i++)
  10.       {
  11.           if(grades[i]>max)
  12.           {
  13.           max=grades[i];
  14.           }
  15.           cout<<"At loop "<<i<<" max is "<<max<<endl;
  16.       }
  17.  
  18.       cout<<endl<<"Final Max is "<<max<<endl;
  19.  
  20. }
Nov 11 '07 #15
Tanx Very Much
I Got It
Iwrote The Program Perfectly!!!!
Nov 11 '07 #16

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

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
0
by: Kasp | last post by:
Hi there, I am trying to make an OLAP cube on a table having two columns (datetime, Number_of_times_an_event_occured). My dimension is time and I want to measure the Min and Max times an event...
1
by: Joe Bongiardina | last post by:
What does the message "calculating...." mean in the lower left status area of a form? I have a form with no calculated, concatenated or lookup fields, yet it displays this msg. The form takes...
1
by: jlm | last post by:
I have a form which feeds table (TblEmpLeave) of Employee Leave Time (time taken off for Administrative, Annual, Sick, Compensation leave). I have EmpID, LeaveDate, LeaveType, LeaveHours fields on...
0
by: robin9876 | last post by:
In an Access 2000 database on some forms 'Calculating...' is continuously displayed in the status bar window and the text of the control is automatically selected. The only workaround is switching...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
25
by: Umesh | last post by:
i want to calculate the time required to execute a program. Also i want to calcute the time remaining for the execution of the program. how can i do that? pl mention some good websites for...
1
by: laredotornado | last post by:
Hi, Can anyone recommend a free script for calculating UPS shipping? I am familiar with the script written in 2000 by Jason Costomiris, but UPS is using an XML interface and I wondered if...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
4
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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
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,...

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.