Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,271 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,458 people online right now. Registration is fast and FREE... Join Now!




Arrays: Finding Smallest Array

 
Reply to this topicStart new topic

Arrays: Finding Smallest Array

aznballerlee
5 Nov, 2006 - 06:57 PM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I'm trying to return the index of the <= string in the array. If more than one string, return the smallest index of the string. Return -1 if no elements in array.


Here's my progress so far .. I tried assert and it gave me the wrong result .. I tried many times to alter the code but no success in finding the bug.

The assert I tried was this: assert(indexOfMin(a, 2) == 2); // The return value should be 0, not 2.
What's wrong??

CODE

int indexOfMin(const string a[], int n);
int indexOfMin(const string a[], int n)
{

if (n==0)
{
    return -1;
}

else
{    
    int i = 0;
    string min = a[i];
    

    for (i=1; i<n; i++)
    {    
        if (a[i] <= min)
        {
            min = a[i];
        }
    }
        
        return i;
}


User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: Arrays: Finding Smallest Array
5 Nov, 2006 - 08:21 PM
Post #2

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
Actually this is my updated code:
CODE

int indexOfMin(const string a[], int n);
int indexOfMin(const string a[], int n)
{

    for (int i=0; i<n; i++)
    {    
        if (a[i] >= a[i+1])
        {
            int min = i+1;
        }
    }
        
        if (n>=0)
        {    
            int min;
            return min;
        }
        else
            return -1;


}


There was no compilation error, just that I get the wrong assert value. I listed my task above .. can anyone check if I wrote the code correctly according to my specs??
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Arrays: Finding Smallest Array
5 Nov, 2006 - 08:36 PM
Post #3

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
probably you declare your function for the greatest index...


How about some declaring it to your desire...index...



User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: Arrays: Finding Smallest Array
5 Nov, 2006 - 08:44 PM
Post #4

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions

I don't get your suggestion ..
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Arrays: Finding Smallest Array
5 Nov, 2006 - 09:07 PM
Post #5

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Probably i said that you designate a variable for your min index and to your max index...


then compare it ....
User is offlineProfile CardPM
+Quote Post

aznballerlee
RE: Arrays: Finding Smallest Array
5 Nov, 2006 - 09:18 PM
Post #6

D.I.C Head
**

Joined: 14 Oct, 2006
Posts: 61


My Contributions
I don't know why I would need a max index .. I don't need a max .. but I do want a min variable.

I designated min as the min index already.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Arrays: Finding Smallest Array
6 Nov, 2006 - 01:09 AM
Post #7

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(aznballerlee @ 5 Nov, 2006 - 10:18 PM) *

I don't know why I would need a max index .. I don't need a max .. but I do want a min variable.

I designated min as the min index already.

Hi Az

If I understand you right, you want to find the array index of the smallest value in the array.

Given an array something like this:

45,75,24,57,2,99

You need to "scan" with code and compare each value both with the last known and smallest and current index. It can be done with just two variables to manage the scan and remember the location of the last known smallest.

You need to "seed" the current_low with the first value, then proceed from position 2 (index 1) and compare with the seeded index, somthing like:
CODE

int idx = 1 ,seed = 0;

while ( idx < num_records )
{
   if ( arr[idx] < arr[seed] )
      seed = idx;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month