HELP with simple program

Hello everyone, I am new to C++ as well as the board after missing some time in school and am in need of a little help. After missing a few weeks in class due to personal reasons I fell a bit behind. The time off had a bigger impact than I thought but I also think I may be thinking too far into this problem. The assignment we have been working on asks us to modify a program so that it generates a certain output. Below is the program that needs modified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
  using namespace std;
  
  int main()
  {
    int i, j;

    for(i = 1; i <= 5; i++)
    {
      cout << "\ni is now " << i << endl;

      for(j = 1; j <= 4; j++)
        cout << " j = " << j;
    }

    return 0;
}



The code needs modified to generate the following outputs on the screen:

i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 3
i = 2 j = 4
i = 3 j = 4

Any help with this would be greatly appreciated! Thank you.

try this
1
2
3
for(i = 1; i <=5; i++)
for(j = 1; j <=4; j++)
cout << "\ni = " << i << "j = " << j << endl;
1
2
3
4
5
6
 i = 1 j = 2
 i = 1 j = 3
 i = 1 j = 4
 i = 2 j = 3
 i = 2 j = 4
 i = 3 j = 4


It's all about recognizing patterns.

Here, we know that 'i' is the counter for the outer loop. So splitting up that output to see what needs to be printed each loop, we are left with this:

1
2
3
4
5
6
7
8
9
10
 i = 1 j = 2
 i = 1 j = 3
 i = 1 j = 4


 i = 2 j = 3
 i = 2 j = 4


 i = 3 j = 4


So take a look at this output and try to piece together things we know about it. We can see that 'i' counts from 1 to 3... but how does 'j' count? It seems to end at 4, but where does it start?
Last edited on
closed account (iAk3T05o)
Try ++i and ++j rather than i++ and j++. They are the same but sometimes have different effects.
jwilt wrote:
try this
Nathan2222 wrote:
Try ++i and ++j rather than i++ and j++. They are the same but sometimes have different effects.



I don't want to sound like an asshole... I mean I'm sure you guys mean well... but telling him to try random things isn't very helpful.
I tried both things and no-go. I'm still getting a long list like the original program. I think I may be thinking too hard about this one. But then again I am new to this
Okay, I want to help you, but first I want to ask you something, did your teacher taught you about array ?
if yes then you should try experiment with array, you will recognize that using array will make this problem a joke..

In case that your teacher have taught it in your class but because your absent you did not know about it, I give you this code, but you should study it, it's not that hard..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
  using namespace std;

  int main()
  {
    int i; 
    int iNumber[] = {1,1,1,2,2,3}; // declare array iNumber [] and store the data of (i)
    int jNumber[] = {2,3,4,3,4,4}; // declare array jNumber [] and store the data of (j)
    for(i = 0; i <5; ++i) // This for loop only used as a guide to display which data within responsible array
    {
        cout << "i = " << iNumber[i] << " j = " << jNumber[i] << endl; // print the data stored in respective array
    }

    return 0;
}


The 'for loop' above has nothing to do with the value of i, actually you even don't have to make 2 'for loop' for i and j (I know you make that loop to change the value of i and j, am I right ? But in my code above it has nothing to do with that). Usually 'for loop' is not for changing a value of a data, it's simply used to guide which element of array that would be displayed or something similar like that(but it's not impossible the 'for loop' is used to change the value of a data like you did, but it's rare, at least for me.)

Okay hope this help you. Keep smile
If I did something wrong please forgive me, I just want to help, and I'm sorry.. thanks

P.S. : you can try to change variable i inside the'for loop' above with a or b, and then do the same with the i inside cout command (i.e you change it for ( a=0; a<5; a++) then change here to : cout << "i = " << iNumber[a] << " j = " << jNumber[a]
How does it ? don't forget change the declaration of int i into int a
Last edited on
LuminaChen:

We have not covered arrays just yet, we covered up to chapter 6 then skipped over chapter 7 in which arrays are taught. Looking at your code I understand it and comprehend it. However, since we haven't covered it yet, I'm not quite sure if he would prefer us to use material he has not yet taught... Previous assignments have been very basic and we were asked to use material that has already been covered. Chapter 5, which is the section in which the program that needs modified is included, covers while statements, while loops, for statements, and do while statements. Being completely new to programming is making it a little tough after missing time that was unavoidable because I'm essentially trying to learn straight from the book. Big headache! haha
After the second for loop finishes, it kicks back out to the first loop and i = 2. Then it enters the second loop and re-initializes j = 1, therefore j will always output 2, 3, 4. You need to change the second for loop to have j = i and it should work properly.

Best advice with for loops is to run through them like the program would and write you output down. It helps to better understand what the loop is doing.
Is that so ? I'm sorry then, I don't know 'bout that, anyway how was it ? Did you solve the algorthym ? I'm curious myself, but I don't see anyway displaying that i and j value without the use of array.. If you already solve it or maybe your teacher taught you how to solve it, can you share it with me ? thanks rjn
Last edited on
LuminaChen: I used the code that included the arrays, and the output matched that of mine except the last line where i = 3 and j = 4 was not included in the output. Other than that, the output matched.
so in other words this is your output ?
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 3
i = 2 j = 4
i = 3 j = 4 // except this last line, all above lines are works nice..

So basically, you haven't found the correct code to this matter ?
How about now ? Did your teacher have discussed how to solve this in your class ?
I'm curious with your teacher code.. If not yet, then no problem, I will wait..
Thank you rjn
Basically what disch was saying earlier is that the pattern is i starts at 1 and goes until it reaches 3. Then j starts at i + 1 and goes until 4 so you are going to want something like:
1
2
3
4
5
6
7
for(int i = 1; i < 4; ++i) //iterate from 1 till less than 4 (1->3)
{
    for(int j = i + 1; j <= 4; ++j) //iterate from i + 1 till less than equal to 4 (i+1->4)
    {
        std::cout << "i = " << i << " j = " << j << std::endl;
    }
}


[edit]forgot the i + in the comment of (i+1->4)[/edit]
Last edited on
Wow thanks giblit, very helpful
Topic archived. No new replies allowed.