# 'for' programming help..

This topic is 4506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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)

##### Share on other sites
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    }}

##### Share on other sites
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;}

##### Share on other sites
Quote:
 Original post by Servant of the LordIf 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

##### Share on other sites

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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015560
×