I need help with a code for LOOPS!

Apr 30, 2020 at 2:09am
So we are supposed to Write a program that asks for two positive integers from the user and the program will then display all integers from the lower of the two numbers to the higher of the two numbers. Then the program will print all the numbers from the higher number to the lower number.

SO I am not sure how to write my code, here's what I have done so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;
int main() 
{
  cout <<"Intructions: Please enter two positive integers and this program will display 
the smaller one to the larger and the larger one to the smaller one. 
Does not matter in which order you input the positive integers."<<endl;
  int i;
  int first=1;
  int second=1;
  cout <<"Enter the FIRST positive integer: ";
  cin>>first;
  cout <<"Enter the SECOND positive integer: ";
  cin>>second;
  for (i=first; first<second; i++)
  {
    cout <<first<<""<<second<<endl;
  }
  for (i=1; second<first; i++)
  {
    cout <<""<<endl;
  }
  cout <<first<<second<<endl;
  cout <<second<<first<<endl;

  return 0;
}


If someone can help me, that would be awesome! THANKS!!!
Last edited on May 10, 2020 at 4:23am
Apr 30, 2020 at 2:23am
Hello aimen112,

While I work on reading your code here is somthinng you can work on.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.


You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
Apr 30, 2020 at 3:03am
Hello aimen112,

You have a good start on your for loops, but you might have a misunderstanding on how they work.

To start with do not define the loop iterator "i" outside the for loop. You only do this if you have some reason to keep the final value of the loop.

Your start would be: for (int i = start;. Other than the "int" here it is correct.

You fail with the condition of the loop: ; first < second;. If you have to add the space around the "<" it makes it easier to read. The problem here is that "first" is always less than "second" and therefor it is always true making it an endless loop because these values never change.

Since you define "i" as the loop iterator you need to check the value of "i" to see if it is less than the stopping value. Usually this will be ; i < something;, but in this case you will need to use "<=" for it to come out right. Be careful with using "<=" as it can loop one more time than you normally want.

The third part ; i++ is correct. This adds 1 to "i" before checking the condition to determine if it loops again or stops.

Inside the for loop the "cout" statement need changed. You need to print the value of not "first" and "second.

Just so you know ("") does not make a space. What you want is (' '). You put a single character in single quotes and a string in double quotes.

The second for loop is much like the first just reversed. Start with "i" being set to "second" and end with "i--"

As a hint the "cout" statements in the for loops should look like this: cout << i << " "; followed by std::cout << endl; after the closing } of the for loop.

Work with that and see what you come up with.

Andy
May 7, 2020 at 4:20am
Hey Andy,

I took your advice for the errors I've made, I've also added some more stuff to my code, take a took.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;
int main() 
{
  cout <<"Intructions: Please enter two positive integers and this program will display 
the smaller one to the larger and the larger one to the smaller one. 
Does not matter in which order you input the positive integers."<<endl;
  int first = 1;
  int second = 1;
  int value = 0;
  cout <<"Enter the FIRST positive integer: ";
  cin>>first;
  cout <<"Enter the SECOND positive integer: ";
  cin>>second;
  for (int i=first; first < second; i++)
  {
    cout << first << first + 1 << second <<endl;
    cout << second << first - 1 << first <<endl;
  }
  for (int i=second; first > second; i++)
  {
    cout << first << second + 1 << second <<endl;
    cout << second << first - 1 << first <<endl;
  }
  
  return 0;
}
Last edited on May 10, 2020 at 4:23am
May 7, 2020 at 7:40am
Hello aimen112,
I will divide my explanation into 3 parts.
1 part:the Positive section
as you did tell your users to input POSITIVE number and you did add instructions there are users
that may "check the bounds' of your program and input NEGATIVE number.
for that you have to make a loop that insure you that the numbers the user entered are POSITIVE.
for this you can use Do While loop;
if you dont know what it is a Do While loop is a loop that runs the code inside the Do brackets
and then check the while condition if false it will repeat until the while condition will turn out true
*We use this method so you wont get any ERR for the while condition cause the value of first/second are not initialized.
1
2
3
4
5
6
7
8
9
10
11
12
    do{
        cout << "Enter the FIRST positive integer: ";
        cin >> first;
        if (first < 0)
            cout << "*Number recieved is NEGATIVE please enter POSITIVE" << endl;
    } while (first < 0);
    do {
        cout << "Enter the FIRST positive integer: ";
        cin >> second;
        if (second < 0)
            cout << "*Number recieved is NEGATIVE please enter POSITIVE";
    } while (second < 0);

Part 2: checking small or large:
If you put in the instructions that you first print SMALL to LARGE you have to stand behind your statement cause that what you told your user.
for that you have to make IF that will check who is bigger and who is smaller.
1
2
3
4
5
6
7
8
    if (first < second) {
        smaller = first;
        larger = second;
    }
    else {
        smaller = second;
        larger = first;
    }

in this way you check what is the large variable and what is the small one.
if you dont want to do it u can delete those instructions and change the code. (i wrote my answer to you as you wrote your first instructions)
another thing is in this way that you save the small and large values in new variable you are able to save the original user input to thier original variables ("first","second") so you may use them in the future if u develop this code to print more than what you intented to.
Part 3:Priting loop
I think you didnt quite understood Handy Andy explanation about the for loop.
in your code the for loop will continue for ever because one simple principal the for loop goes like this
for(Declaring variable with value; putting condition for the variable that we declared; changing the variable that we declared on so the for loop will have end point)
*Example: for(int i = 0; i < 5; i++)
in this example each time the for loop runs "I" will increase by 1 until he will be larger than 5.
in your code each time the for loop runs the "First" variable isnt changing so he will always be LOWER than "Second"
to solve it you can use 2 ways:
First way: using temporary variable to save first or second value than change it to incease or decrease his value to make for loop ends.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    displayCounter = smaller; // using temporary variable that we can change but still keep the user input original value
    cout << "Displaying: small to large" << endl;
    for (int i = 0; i <= (larger-smaller); i++) // for loop that runs aslong i smaller or equal to difference between the large or small inputs
    {
        cout << displayCounter << endl;
        displayCounter++; // changing the temporary number as we like to display on screen
    }
    displayCounter = larger; // ajusting temporary number to our use in this case to display large to small without changing original user input
    cout << "Displaying: large to small" << endl;
    for (int i = 0; i <= (larger-smaller); i++) //we used same for condition but this time we decreasing our temporary variable instread increasing it
    {
        cout << displayCounter << endl;
        displayCounter--;
    }

i added some comments inside the code so you will have less questions and move understanding.
Second way: using the "I" variable that we have to declare and use anyway for the FOR loop to display our difference between the numbers.
1
2
3
4
5
6
7
8
    cout << "Displaying: Small to Large" << endl;
    for (int i = 0; i <= (larger - smaller); i++) {
        cout << smaller + i << endl;
    }
    cout << "Displaying: Large to Small" << endl;
    for (int i = 0; i <= (larger - smaller); i++) {
        cout << larger - i << endl;
    }

for my opinion this way is much easier and saves you to create another variable and re declare his value few times on the program.
but i want you to understand easier so for the total program code example i will use the first way.
here what it should look like at the end:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;
int main()
{
    cout << "Intructions: Please enter two positive integers and this program will display the smaller one to the larger and the larger one to the smaller one. Does not matter in which order you input the positive integers." << endl;
    int first;
    int second;
    int smaller;
    int larger;
    int displayCounter;

    do{
        cout << "Enter the FIRST positive integer: ";
        cin >> first;
        if (first < 0)
            cout << "*Number recieved is NEGATIVE please enter POSITIVE" << endl;
    } while (first < 0);
    do {
        cout << "Enter the FIRST positive integer: ";
        cin >> second;
        if (second < 0)
            cout << "*Number recieved is NEGATIVE please enter POSITIVE" << endl;
    } while (second < 0);
    if (first < second) {
        smaller = first;
        larger = second;
    }
    else {
        smaller = second;
        larger = first;
    }

    displayCounter = smaller; // using temporary variable that we can change but still keep the user input original value
    cout << "Displaying: small to large" << endl;
    for (int i = 0; i <= (larger - smaller); i++) // for loop that runs aslong i smaller or equal to difference between the large or small inputs
    {
        cout << displayCounter << endl;
        displayCounter++; // changing the temporary number as we like to display on screen
    }
    displayCounter = larger; // ajusting temporary number to our use in this case to display large to small without changing original user input
    cout << "Displaying: large to small" << endl;
    for (int i = 0; i <= (larger - smaller); i++) //we used same for condition but this time we decreasing our temporary variable instread increasing it
    {
        cout << displayCounter << endl;
        displayCounter--;
    }
    while (true); // forever loop just to prevent the "end of program" message (there are way to do it cleaner but i dont want to complicate you
}


Hope i was clear enough for you to understand tried to simplfied it as much as i can.
if you have any questions im here

Have a nice day, sean. :)
May 7, 2020 at 9:23pm
Hello Seanronen11,

As I was reading through what you said I came to this:
You wrote:

*Example: for(int i = 0; i < 5; i++)
in this example each time the for loop runs "I" will increase by 1 until he will be larger than 5.


Headed in the right direction, but wrong implementation. "4 < 5" is true and the loop iterates. "5 < 5" and the condition fails, so the loop fails. At this point "i" can never be "> 5".

Andy
May 7, 2020 at 9:47pm
Hello aimen112,

So your idea of revising the code if to adjust the "cout" statements to try and compensate for an incorrect for loop?

I ran your code and it produced this:

Enter the FIRST positive integer: 15
Enter the SECOND positive integer: 5
1565
51415
1565
51415
1565
51415
1565
51415
1565
51415
1565
51415
1565


Very hard to read and figure out what is what, but I guess that is what you want.

I think the output you are looking for is:

Intructions:
 Please enter two positive integers and this program will display the smaller
 one to the larger and the larger one to the smaller one.
 Does not matter in which order you input the positive integers.

 Enter the FIRST positive integer: 15
 Enter the SECOND positive integer: 5

 5 6 7 8 9 10 11 12 13 14 15

 15 14 13 12 11 10 9 8 7 6 5



And for dealing with negative numbers it looks like this:

Intructions:
 Please enter two positive integers and this program will display the smaller
 one to the larger and the larger one to the smaller one.
 Does not matter in which order you input the positive integers.

 Enter the FIRST positive integer: -9

     Number must be greater or equal to (0)!

 Enter the FIRST positive integer: 0
 Enter the SECOND positive integer: -9

     Number must be greater or equal to (0)!

 Enter the SECOND positive integer: 10

 0 1 2 3 4 5 6 7 8 9 10

 10 9 8 7 6 5 4 3 2 1 0



Your instructions are fine, but try to keep the width of what you display shorter than the screen width. It is a small thing, but it makes it easier to read. Also a a blank line or 2 breaks up what you display and easier for the user to work with.

I have already shown you what the for loop should be as did Seanronen11. If this is still a problem have a look at http://www.cplusplus.com/doc/tutorial/control/#for If you are still confused let me know.

If the middle output block is not what you want give an example of what you need.

Andy
May 8, 2020 at 1:02pm

Headed in the right direction, but wrong implementation. "4 < 5" is true and the loop iterates. "5 < 5" and the condition fails, so the loop fails. At this point "i" can never be "> 5".

You right Andy when I becomes equal to 5 the loop stop cause we want to run this program
(for example) 5 times.
so when I becomes equal to 5 the loop stops and keeps going on to next code.
In aimen loop he made condition that will never be false so his loop was forever.

have a nice day, Sean
May 8, 2020 at 9:13pm
@Seanronen11,

Sometimes it is the small things that get you

Andy
May 10, 2020 at 5:08am
Thank you Sean and Andy.
Topic archived. No new replies allowed.