I could use help with "for" structure

• 09-20-2003
ruger
I could use help with "for" structure
I am brand to to C++ and not well versed in programming at all. I have a lab assignment that I would appreciate some help with. I do not expect you to do the assignment, but any suggestions of where to start would be great. The program should use "for" structure to find the smallest of several integers. Assume that the first value read specifies the number of values remaining and that the first number is Not one of the integers to compare. Thanks for any pointers, Ruger
• 09-20-2003
xddxogm3
try this

Code:

```for (x=1;x<=SIZE;x++) {     if(w>y)     {     //fill in remaining of code here.     } }```
• 09-20-2003
major_small
you might want to start by reading the first number into a variable called i, which will be used in the for loop... then you create two variables to hold the data type (int/float)... preset one to 32000 or something so that you can be sure the first value will be written into it... then every time it loops, read in the new value and put it in one memory spot, and then compare it with the one you preset to 32000... if it's smaller, copy it into the box preset to 32000 and then do it all over again until the loop is over...

I probably shouldn't have given you that much info, but try to write some code from that and if you still cant get it, just post your code here...
• 09-20-2003
ruger
Thanks for the pointer so far, i'll let you all know if I get it to work or not!
• 09-21-2003
ruger
This is what I have, but it doesn't find the smallest integer, it prints the last one entered for the smallest everytime! More suggestions please!
Code:

```#include <iostream> using namespace std; int main() {         int num = 0;         int value;         cout << "please enter an integer to determine\n"                 << "the amount of integers I will compare\n"                 << "to find and display the smallest one: ";         cin >> num;         cout << "you may enter your integers now; ";         for ( int count = 1; count <= num; count++)                                 {                         cin >> value;                 }                 if ( value == value )                         cout << value << " is the smallest integer" << endl;                 return 0; }```
• 09-21-2003
major_small
1) you have to compare inside the loop
2) you have to make sure it's lower, not equal

--edit--
pointing out some other stuff:
Code:

```#include <iostream> using namespace std; int main() {         int num = 0;         int value;                 //what about the value to hold the current lowest int?         cout << "please enter an integer to determine\n"                 << "the amount of integers I will compare\n"                 << "to find and display the smallest one: ";         cin >> num;         cout << "you may enter your integers now; "; //what if they enter 4531863486816, meaning 4,531,8,6348,68,16         for ( int count = 1; count <= num; count++)                                 {                         cin >> value;                 }                 if ( value == value ) //change and put inside loop                         cout << value << " is the smallest integer" << endl;                 return 0; }```
• 09-21-2003
ruger
Thanks for all the help, but I'm still stuck. I've been on this for another hour and can't get the new variable to initialize!
And "not all control paths return a value"!
• 09-21-2003
alphaoide
Can I see the codes you're working on?
How do you enter your input for the integers?
• 09-22-2003
write it out in your native language first. Maybe something like this. If you can write it out, it is much easier to code.

declare variable to hold the current smallest value.

declare a variable to hold the number of values to be entered.

obtain the number of values to be entered from user

declare a loop counter to hold the current number of values entered.

use a loop to obtain each value from user, incrementing counter variable by one each time through loop.

in the loop:
if this is the first value entered, then by definition it is the current smallest, so assign the entered value to current smallest varaible.

else if this isn't the first value entered, then compare the current value with the current smallest value. if the current value is smaller than the current smallest value, assign the current value to the current smallest variable.
• 09-23-2003
SpEkTrE
try this....
Newbie here......try this, it may help

Code:

```#include <iostream.h> #include <stdlib.h> int main() { int smallest, times, count, num; count=1; num=0; smallest=0;       cout << "Please enter how many Integers to compare ";       cin >> times;     for (; count <= times; ++count)       {       cout << count << " Enter your Integer ";       cin >> num;           if (num < smallest || count ==1){             smallest= num;             }       }       cout << "\n\n" << smallest << " is the smallest integer";       system("PAUSE");       return 0; }```
• 09-24-2003
ruger
This works but, I wonder if there is another way to code that wouldn't require a preset number to compare to, So no matter what numbers
Code:

```#include <iostream> using std::cout; using std::cin; be using std::endl; int main() {   int count;                int value;    int smallest = 999999999999;   cout << "Enter the total number of integers to read: ";   cin >> count;     cout << "Enter " << count << " integers to compare: ";   for (int i = 0; i < count; i++)   {           cin >> value;         if (value < smallest) {         smallest = value;       }   }     cout << "\nThe smallest value found in this list is: "         << smallest         << endl;   return 0; }```
were entered it would compare them? [/code]
• 09-24-2003
bennyandthejets
Quote:

So no matter what numbers were entered it would compare them?
Your code already does that. Or are you saying, you dont want to have to set smallest to 999999999999?

Code:

```#include <iostream> using std::cout; using std::cin; be using std::endl; int main() {   int count;                int value;    int smallest;   cout << "Enter the total number of integers to read: ";   cin >> count;     cout << "Enter " << count << " integers to compare: ";   cin >> smallest; //take the first integer and put it straight into smallest   for (int i = 1; i < count; i++) //note: initial i changed to 1   {           cin >> value;         if (value < smallest) {         smallest = value;       }   }     cout << "\nThe smallest value found in this list is: "         << smallest         << endl;   return 0; }```
• 09-24-2003
ruger
Thanks for all the help, I've got it!!
• 09-24-2003
bennyandthejets
So what was the problem?
• 09-25-2003
ruger
No, problems just operator error!!