1-10/1-12??

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
#include <iostream>
#include <iomanip>
#include <conio>

int main()
{
   int j;
   cout<<"\n\t Multiplication Tables";
   cout<<"\n _____________________________________\n\n";

   for(int n=1; n<=12; n++)
   {

      while(j<=10)
      {
      	cout<<setw(4)<<j*n;
         j++;
      }
      cout<<endl;
   }

   cout<<"\n\tEnd Of Program";
   getch();
   return 0;
}


Can I know why the above program get 1-10? Which command do that? I guess it shud get 1-12 right? TQ...
First of all, you may want to initialise j.

Otherwise there's no real telling what's going on in that while loop. Assuming you initialised j to 0, that while loop will only ever run once because you don't reset j anywhere.

What is it you're trying to do, exactly?

Print out the 12 times table for value j?
Yes...This is the real code...

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
#include <iostream>
#include <iomanip>
#include <conio>

int main()
{
	int j;
   cout<<"\n\t Multiplication Tables";
   cout<<"\n _____________________________________\n\n";

   for(int n=1; n<=12; n++)
   {
      j=1;
      while(j<=10)
      {
      	cout<<setw(4)<<j*n;
         j++;
      }
      cout<<endl;
   }

   cout<<"\n\tEnd Of Program";
   getch();
   return 0;
}


I know that if I don't initialize the j, the while loop should not work, but then why still have value 1-12 in output? TQ...
It seems right for what it's doing to me.

When I run it, it prints out a multiplication table correctly.

12 rows and 10 columns, which is what's specified.

Look at what you're effectively saying:

- First iteration of loop, n=1:
- Set j to 1.
- For all values of j between 1 to 10, print 1 * j.
- Print an endline
- Second iteration of loop, n=2:
- Set j to 1.
- For all values of j between 1 to 10, print 2 * j.

And so on. All the way up to and including n=12.

Functionally, this is fine. What are you expecting it to do?
Last edited on
Actually just now I'm trying to change the inner loop to "for loop" and outer loop to "while loop" but accidently deleted j=1; in line 13, then i compile the program and get the value 0-10 (which I don't know where it comes from)...

but if I deleted the whole while loop(code below), the ouput doesn't shows any value instead of the cout things only...which is true...(meaning that if got while loop but no initialize j=1, the ouput is 0-10<-which I don't know why since while loop is not execute at all if no initialize j right? But there is still a output... This is my question...TQ...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <conio>

int main()
{
	int j;
   cout<<"\n\t Multiplication Tables";
   cout<<"\n _____________________________________\n\n";

   for(int n=1; n<=12; n++)
   {


      cout<<endl;
   }

   cout<<"\n\tEnd Of Program";
   getch();
   return 0;
}
Last edited on
Topic archived. No new replies allowed.