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

Home Posts Topics Members FAQ

Max and Minimum value in arrays

16
I tried to find max and min value from arrays. and my code so far looks like this:

Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<size2; i++){
  2.           for (j = 0; j < 4; j++){
  3.             by_max[i]=by[i][0];
  4.                 if (by[i][j]>by_max[i]){
  5.                     by_max[i]=by[i][j];
  6.                 }
  7.             by_min[i]=by[i][0];
  8.               if (by[i][j]<by_min[i]){
  9.                 by_min[i]=by[i][j];
  10.                }           
  11.        }
  12.   }   
  13.  
My difficulty is this; the max loop runs perfectly. however the min loop only serves to find the max value somehow. So the min value is same with max value.
I cannot see how it's failing at all, at all. Does anyone have any ideas?

Thank you.
May 23 '09 #1
7 4677
JosAH
11,448 Expert 8TB
Move those lines #3 and #7 before your line #2.

kind regards,

Jos
May 23 '09 #2
mingke
16
Thank you, Jos..

That solve my problem...
May 23 '09 #3
lonbame
10
can do this
Expand|Select|Wrap|Line Numbers
  1. //max
  2. max=arr[0];
  3. for(i=0;i<4;i++)
  4. {
  5.  if(arr[i]>max)
  6.   max=arr[i];
  7. }
  8. //min
  9. min=arr[0];
  10. for(i=0;i<4;i++)
  11. {
  12.  if(arr[i]<max)
  13.   max=arr[i];
  14. }
  15.  
Jun 1 '09 #4
donbock
2,426 Expert 2GB
You can make this more efficient.
There is no reason to scan the array twice.
If the initial values for max and min are copied from arr[0] outside the loop(s) then you don't need to scan the 0th array element.
Jun 1 '09 #5
Bassem
344 100+
And one for loop is enough, Isn't it?
Jun 2 '09 #6
JosAH
11,448 Expert 8TB
@Bassem
The OP wanted to find a min and max value for every row of a matrix; moving the initialization out of the inner loop (as I replied) did the trick. I always prefer to return a pointer to the min or max values just in case there are no values.

kind regards,

Jos
Jun 2 '09 #7
Bassem
344 100+
Sorry for that, I didn't recognize there is a matrix.

I just ate, thanks
Jun 2 '09 #8

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

Similar topics

4
by: Porthos | last post by:
Hi All, I'm trying to find the minimum value of a set of data (see below). I want to compare the lengths of these attribute values and display the lowest one. This would be simple if I could...
2
by: Hennie de Nooijer | last post by:
I have a problem (who not?) with a function which i'm using in a view. This function is a function which calculates a integer value of a date. For example: '12/31/2004 00:00:00" becomes 20041231....
2
by: Hennie de Nooijer | last post by:
Because of an error in google or underlying site i can reply on my own issue. Therefore i copied the former entered message in this message....
3
by: RobG | last post by:
I would like a query that will tell me the minimum non-zero value in a row. Say I have a table with a column called recordID that contains unique record IDs, and have a set of values named V1,...
5
by: Bas Wassink | last post by:
Hi there, I was wondering what the standard (C89) states what the minimum value of FOPEN_MAX should be? K&R just state that FOPEN_MAX is implementation-defined and when googling for an answer I...
11
by: JJLaRocque | last post by:
Hi all, Is there a simple python function to return the list index of the minimum entry in a list of lists? ie, for , , ] to return 2,4. Or, same question but just for a list of numbers, not a...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
18
by: Rose4msm | last post by:
hello can any one please help me i have a test and i need to study the java scripts, i know that i have to use arrays but im still a bit confuse the question is to ask the user how many data items...
13
by: Ioannis Vranos | last post by:
Is there any mentioning in the standard of the number of bits of the various built in types, apart from char/signed char/unsigned char types? Or only about the minimum value ranges of them?
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
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...
1
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
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: 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
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 ...

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.