473,378 Members | 1,389 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,378 software developers and data experts.

Returning largest value from multiple columns using an array

21
Hello folks,

I have a function where I pass numbers from a table, and I want to compare them and return the largest horizontal value. I store them in an array, and try the DMAX function on it, but get stuck there.

Expand|Select|Wrap|Line Numbers
  1. Function landuse(dblmips As Double, dblcie As Double, dblmed As Double, _
  2.     dblpdr As Double, dblret As Double, dblvis As Double)
  3.  
  4. Dim varsqft(5) As Double
  5. Dim varlargest As Double
  6.  
  7. varsqft(0) = dblmips
  8. varsqft(1) = dblcie
  9. varsqft(2) = dblmed
  10. varsqft(3) = dblpdr
  11. varsqft(4) = dblret
  12. varsqft(5) = dblvis
  13. 'the problem seems to be the next line
  14. varlargest = DSum(varsqft)
  15. landuse = varlargest
  16.  
  17. End Function
Any suggestions on how to achieve this, with arrays or otherwise?
Oct 1 '07 #1
5 2808
hariharanmca
1,977 1GB
Explain your problem detail.
Do you mean, you want to check the biggest value in each field or compare a value in each field?
Oct 1 '07 #2
akselo
21
Explain your problem detail.
Do you mean, you want to check the biggest value in each field or compare a value in each field?
Yes, I have six numerical fields with square foot information for different landuse activities. I want the function to identify the largest value in any one row across the six fields and return that value. If I could perform a numerical operation on values in an array, that seems easier than 6 or so different nested if statements.
Oct 1 '07 #3
ADezii
8,834 Expert 8TB
Hello folks,

I have a function where I pass numbers from a table, and I want to compare them and return the largest horizontal value. I store them in an array, and try the DMAX function on it, but get stuck there.

Expand|Select|Wrap|Line Numbers
  1. Function landuse(dblmips As Double, dblcie As Double, dblmed As Double, _
  2.     dblpdr As Double, dblret As Double, dblvis As Double)
  3.  
  4. Dim varsqft(5) As Double
  5. Dim varlargest As Double
  6.  
  7. varsqft(0) = dblmips
  8. varsqft(1) = dblcie
  9. varsqft(2) = dblmed
  10. varsqft(3) = dblpdr
  11. varsqft(4) = dblret
  12. varsqft(5) = dblvis
  13. 'the problem seems to be the next line
  14. varlargest = DSum(varsqft)
  15. landuse = varlargest
  16.  
  17. End Function
Any suggestions on how to achieve this, with arrays or otherwise?
Copy & Paste the following code segment, and you'll be fine:
Expand|Select|Wrap|Line Numbers
  1. Function landuse(dblmips As Double, dblcie As Double, dblmed As Double, _
  2.     dblpdr As Double, dblret As Double, dblvis As Double)
  3.  
  4. Dim varsqft(0 To 6) As Double, dblTemp As Double
  5. Dim intArrayCounter As Integer
  6.  
  7. dblTemp = varsqft(0)    'make the 1st Element a Seed Value
  8. varsqft(1) = dblmips
  9. varsqft(2) = dblcie
  10. varsqft(3) = dblmed
  11. varsqft(4) = dblpdr
  12. varsqft(5) = dblret
  13. varsqft(6) = dblvis
  14.  
  15. For intArrayCounter = 1 To 6
  16.   If varsqft(intArrayCounter) > dblTemp Then
  17.     dblTemp = varsqft(intArrayCounter)
  18.   End If
  19. Next
  20.  
  21. landuse = dblTemp
  22. End Function
Expand|Select|Wrap|Line Numbers
  1. 'Sample Function Call
  2. Debug.Print "The Maximum Value passed to the landuse Function is: " & landuse(89.123,456.0987,74.879,9.213,237.0,81.2345)
  3.  
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. The Maximum Value passed to the landuse Function is: 456.0987
NOTE: The code does neither Data Type nor Null Value Checking - I'll leave that up to you. If it is imperative that all these Fields have Values (DOUBLE), simply set the Required Property in the Table to True, and the Data type to Numeric/DOUBLE. Good Luck!
Oct 1 '07 #4
akselo
21
Copy & Paste the following code segment, and you'll be fine:
Expand|Select|Wrap|Line Numbers
  1. Function landuse(dblmips As Double, dblcie As Double, dblmed As Double, _
  2.     dblpdr As Double, dblret As Double, dblvis As Double)
  3.  
  4. Dim varsqft(0 To 6) As Double, dblTemp As Double
  5. Dim intArrayCounter As Integer
  6.  
  7. dblTemp = varsqft(0)    'make the 1st Element a Seed Value
  8. varsqft(1) = dblmips
  9. varsqft(2) = dblcie
  10. varsqft(3) = dblmed
  11. varsqft(4) = dblpdr
  12. varsqft(5) = dblret
  13. varsqft(6) = dblvis
  14.  
  15. For intArrayCounter = 1 To 6
  16.   If varsqft(intArrayCounter) > dblTemp Then
  17.     dblTemp = varsqft(intArrayCounter)
  18.   End If
  19. Next
  20.  
  21. landuse = dblTemp
  22. End Function
Expand|Select|Wrap|Line Numbers
  1. 'Sample Function Call
  2. Debug.Print "The Maximum Value passed to the landuse Function is: " & landuse(89.123,456.0987,74.879,9.213,237.0,81.2345)
  3.  
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. The Maximum Value passed to the landuse Function is: 456.0987
NOTE: The code does neither Data Type nor Null Value Checking - I'll leave that up to you. If it is imperative that all these Fields have Values (DOUBLE), simply set the Required Property in the Table to True, and the Data type to Numeric/DOUBLE. Good Luck!
This works and is awesome!! Thank you so much even if I can't see how dblTemp/varsqft(0) is assigned a value to begin with. It appears you suggest that it is better to deal with nulls in the source table rather than to make control flow workarounds in the code at runtime?
Oct 4 '07 #5
ADezii
8,834 Expert 8TB
This works and is awesome!! Thank you so much even if I can't see how dblTemp/varsqft(0) is assigned a value to begin with. It appears you suggest that it is better to deal with nulls in the source table rather than to make control flow workarounds in the code at runtime?
Thank you so much even if I can't see how dblTemp/varsqft(0) is assigned a value to begin with.
It is nothing more than a 'Seed' Value. Many times in Arrays, the 1st Element (0) is not used for related data storage but provides some other functionality.
It appears you suggest that it is better to deal with nulls in the source table rather than to make control flow workarounds in the code at runtime?
If all your Fields are required in the first place, why would you want to allow Nulls at the Table level, then check for them later in code? Do not allow the situation to happen in the first place.
Oct 4 '07 #6

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

Similar topics

1
by: Craig | last post by:
Hi, I am storing information being sent to me weekly into a ms sql database. The one twist I am running into is that later down the line the information I recieve may require more columns, or...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
4
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its...
8
by: shan | last post by:
How to return an two dimensional array in user defined function to main function. I think it is posible by using pointers but don't know how to code.or is there any method without using...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
3
by: HEMH6 | last post by:
Who can help solve this problem??? Finding the Largest Value (a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
3
by: akselo | last post by:
Does anyone know a good way to compare several columns and return the largest value, somewhat like what MAX does for a single column?
5
by: Cromulent | last post by:
Okay I'm having a few issues with this and I can't seem to get it sorted out (most likely due to my inexperience with Python). Here is my Python code: def fileInput(): data = s =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.