Skip to main content
gamedev.net
🔒 Locked

'for' programming help..

Started by antxsperry Sep 18, 2006 at 10:20 AM 5 replies 900+ views
Original Post
antxsperry
antxsperry
i'm having some trouble with certain exercises in my book it's asking me to write a program that uses a for statement to find the smallest of several integers. the first integer is used to specify the number of values remaining and that it shouldn't be compared.. my only problem is that i have no idea how i would get the program to remember all of the integers. cause i don't think i can make a new variable for each number. if anyone could help me with this i'd appreciate it.
ToohrVyk
ToohrVyk
From the sounds of it, this is either a C exercise, or a very bad and ugly C++ one. In both cases, data would be stored in an array. Read the chapter on arrays in your favourite book to learn how to access the elements of an array.
antxsperry
antxsperry
cool thanks.
and yeah, this book tends to make me read the next chapter to be able to do the exercises in the one before. (C++ how to program 5/e)
Servant of the Lord
Servant of the Lord
A for loop is essentially the same as a while loop, with a few minor differences. If you can find the lowest integer out of a for loop, you can just as easily, find them inside of said loop.
Quote:
the first integer is used to specify the number of values remaining and that it shouldn't be compared.

Good. That integer is the one imiedently in the for loop, that the loop checks for when to exit.

If you have...
for(int intLeft = 5; intLeft > 0; intLeft--)

...you keep track of how many ints you have left and automaticly exit when you reach 0. The best thing to do is create an array, and cycle through the array as you cycle through the for loop, checking the numbers for their values and determining the highest or lowest.

int MyInt[5]; //Your numbers in which you wish to find the greatest//This for loop can be used to cycle through your arrayfor(int intLeft = 5; intLeft > 0; intLeft--){    if( ... ) //Compare your current int to the int below it, in the array    {        //If it's higher, overwrite the lower int with the higher    }    else    {        //If the int below your current int is of greater value, keep it as it is    }}
ToohrVyk
ToohrVyk
In practice, however, do not reinvent the wheel:

#include <iostream>#include <algorithm>int main() {  int values[] = { 5, 1, 3, 2, 1, 5 };  std::cout << "Minimum is: " << * std::min_element(values, values + 6) << std::endl;}

Adam Hamilton
Adam Hamilton
Quote:
Original post by Servant of the Lord

If you have...
for(int intLeft = 5; intLeft > 0; intLeft--)



int MyInt[5]; //Your numbers in which you wish to find the greatest//This for loop can be used to cycle through your arrayfor(int intLeft = 5; intLeft > 0; intLeft--){    if( ... ) //Compare your current int to the int below it, in the array    {        //If it's higher, overwrite the lower int with the higher    }    else    {        //If the int below your current int is of greater value, keep it as it is    }}


To the OP:

Remember that accessing the array would require that you subtract 1 from intLeft like

a = MyInt[intLeft-1];

cause intLeft will be 5, 4, 3, 2, 1 whereas your MyInt indicies are 4, 3, 2, 1, 0
f1engineer
f1engineer

int GetSmalestInt(int* int_arr, int arr_len)
{
int smalest_val = 0;
for(int q = 0; q < arr_len; q++)
{
if(q == 0)
{
smalest_val = int_arr[q];
}
else
{
if(smalest_val >= int_arr[q])
{
smalest_val = int_arr[q];
}
}
}
return smalest_val;
}

//In function where it is necessary to find smalest value type this:

int int_arr[] = { 2, 5, 6, 45, 125, 73 };
int arr_len = 6;

int smalset_val = GetSmalestInt(int_arr, arr_len);

Also if it is possible the better variant is to use CPrtArray and then it is not necessary to pass parameter arr_len.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.