### For loop to determine a range?

Guys,

I thought I had a solution figured out for an assignment, but finally after getting word back from my instructor, it is incorrect.

Basically, I am having trouble figuring out how to isolate a range of numbers from a list, and display values associated with those numbers. For example, if the list of numbers is:

0 = terminate program
10 = value1
20 = value2
30 = value3
40 = value4
50 = value5
60 = value6
70 = value7
80 = value8
90 = value9
100 = value10

And I prompt the user to enter two numbers, (start and end) how can I display, for example, all the values from 40 to 70?
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````void displayRange (int, int); int main() { int start, end; do { cout << "Enter a number range" << endl; cin >> start >> end; //User enters two numbers which defines the range if (start != 0) { cout << "The range of values include:" << endl; for (start = 1; start < end; value++) //Not sure how to work this For loop to determine the range entered by the user displayRange(start, end); //Function to display values in range } }while (start != 0); //0 Terminates Loop cin.get(); cin.get(); return 0; } displayRange (int start, int end) { if (start >= 10) cout << "value1"; if (start >= 20) cout << "value2"; if (start >= 30) cout << "value3"; if (start >= 40) cout << "value4"; if (start >= 50) cout << "value5"; . . . ``````

Is this list in a file?
No, not from a file
Here is something of the sort

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { cout << "Here is a program to make a number line" << endl; int value1 = 0; int value2 = 0; cout << "Enter two numbers: " << endl; cin >> value1 >> value2; if (value1 < -50000 || value2 > 50000) { cout << "Please enter two numbers between -50000 and 50000" << endl; return 0; } if (value1 < value2) { cout << "Here are the numbers between " << value1 << " and " << value2 << endl; cout << value1 << endl; while (value1 < value2) { value1++; cout << value1 << " \n"; } } if (value1 > value2) { cout << "Here are the numbers between " << value2 << " and " << value1 << endl; cout << value2 << endl; while (value2 < value1) { value2++; cout << value2 << " \n"; } } system("PAUSE"); return 0; } ``````
You'll want to use an array of integers:
 `` `` ``int ValueArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};``

then you'll need to use your displayrange function:
 ``123456789101112`` ``````displayRange (int start, int end) { if (start >= 10) cout << ValueArray[0]; if (start >= 20) cout << ValueArray[1]; if (start >= 30) cout << ValueArray[2]; if (start >= 40) cout << ValueArray[3]; if (start >= 50) cout << ValueArray[4];``````

Kinda make sense?
Arrays will be in the next chapter- havent covered them yet. I will read up on it though. Yes, that seems to make sense, but I am a little confused because I need to print out a string, not a number.
Like this maybe?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include using namespace std; void displayRange (int, int); int main() { int start, end; do { cout << "Enter a number range" << endl; cin >> start >> end; //User enters two numbers which defines the range cin.ignore(); if (start != 0) { cout << "The range of values include:" << endl; displayRange(start, end); //Function to display values in range } }while (start != 0); //0 Terminates Loop cin.ignore(); return 0; } void displayRange (int start, int end) { if (start <= 10 && end >= 10) cout << "value1" << endl; if (start <= 20 && end >= 20) cout << "value2" << endl; if (start <= 30 && end >= 30) cout << "value3" << endl; if (start <= 40 && end >= 40) cout << "value4" << endl; if (start <= 50 && end >= 50) cout << "value5" << endl; }``````
If you have to do it without arrays:
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````for(unsigned int x = Start; x <= End; x += 10) { switch(x) { case 10: std::cout<<"Value 1"<

this loops though x as a multiple of 10 and executes each switch statement. Notice that x can be incremented by something other then one.
Last edited on
@ Yanson: Thats what I had originally, but if a user enters something like 22 and 85, then I need to display all of the values in that range.

@pogrady: That looks good, but I am trying to translate it to my actual problem- I might have given a bad example
removed
Last edited on
removed
Last edited on
Use a lookup table, perhaps?

Something like this:
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include void print_period_names( int start, int end ) { constexpr int N = 12 ; static const char* const period_name[N] = { "Neogene", "Paleogene", "Cretaceous", "Jurassic", "Triassic", "Permian", "Carboniferous", "Devonian", "Silurian", "Ordovician", "Cambrian", "Precambrian" }; static const int period_max[N] = { 65, 135, 192, 225, 280, 345, 395, 435, 500, 570, 4500, 99999999 }; if( start<999999 && end>start ) { int s = 0 ; while( start > period_max[s] ) ++s ; int f = N-1 ; while( end < period_max[f] ) --f ; for( int i = s ; i <= f ; ++i ) std::cout << period_name[i] << '\n' ; } } int main() { print_period_names( 395, 4500 ) ; }``````

Topic archived. No new replies allowed.