Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




first availiable place in array

 
Reply to this topicStart new topic

first availiable place in array

yiodoc
6 Mar, 2008 - 09:24 AM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 9


My Contributions
CODE
        for (m=0; m<7; m++)
        
            if (best_schedule[m] < check)  
            {
                                                                check=best_schedule[m];      
                best_schedule[m]=p;    
            }
        
        printf("%d , %d, %d, %d, %d, %d",best_schedule[0],best_schedule[1],best_schedule[2],best_schedule[3],best_schedule[4],best_schedule[5],best_schedule[6]);
        getchar();


By the former I give a price to the first availiable place in an array. What I want to do is to give price to the last availiable place of the array. I thought that I could do that by for ( m=6; m>-1; m--); but it doesn't seem to work. Any ideas?

Thanks

This post has been edited by yiodoc: 6 Mar, 2008 - 09:50 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: First Availiable Place In Array
6 Mar, 2008 - 09:42 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
As you loop through the array, test each one to see if it is available. If you mean empty spot, just check to see if it is NULL. If they have values, check the value to see if it should be replaced with the new value of P. Once you have found the spot to place it, you can break the loop using the break statement.

I hope that makes sense to you. Enjoy! smile.gif
User is online!Profile CardPM
+Quote Post

yiodoc
RE: First Availiable Place In Array
6 Mar, 2008 - 09:52 AM
Post #3

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 9


My Contributions
QUOTE(Martyr2 @ 6 Mar, 2008 - 10:42 AM) *

As you loop through the array, test each one to see if it is available. If you mean empty spot, just check to see if it is NULL. If they have values, check the value to see if it should be replaced with the new value of P. Once you have found the spot to place it, you can break the loop using the break statement.

I hope that makes sense to you. Enjoy! smile.gif



How could I do the opposite? Put the price in the last null space of the array?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: First Availiable Place In Array
6 Mar, 2008 - 09:55 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Start your looping to run from the bottom (start your for loop instead of zero as the index of the last spot in the array, check to see if i >= 0 and decrement i instead of incrementing it).

cpp

// if array was 10 slots (index 0 - 9)...

// Run through the loop backwards
for (int i = 9; i >= 0; i--) {
//do checks
}


smile.gif
User is online!Profile CardPM
+Quote Post

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

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