Unfixed multiplication table/ Looping Program

Aug 31, 2013 at 6:52am
Hello, so i did the following code. What I want to do next is to make it unfixed so that I would be able to enter the area I want to start and area I want it to end.
For Example:
Enter the area you want to start : 1
are it will end: 5

So, it will show the multiplication table for 1,2,3,4,5 only up to x10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
using namespace std;

int main ()
{
	cout << "\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10" << endl;

	for (int x=1; x<=10; x++)
	{
		cout << x << "|";
		for (int y=1; y<=10; y++)
		{
			cout << x*y << "\t";
		}
	}
	system ("pause");
		return 0;
}


Also, I couldn't get "\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10" to fit in the command prompt. Any workarounds?


EDIT:
I got another question.

What I want to do now is the program must return to the main menu unless the exit button is selected.

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
int x;
	int sum=0;
	int ave=0;
	cout << "      Program Menu     " << "\n"
		<< "Please choose one of the following programs" << "\n"
		<< "Option 1: Input a number other than 999." << "\n"
		<< "Option 2: Print //d// five times." << "\n"
		<< "Option 3: Print a multiplication table." << "\n"
		<< "Option 4: PassOrFail grade assessment." << "\n"
		<< "Option 5: Count up to what the user enters" << "\n"
		<< "Option 6: Exit" << endl;

	cin >> x;

		switch (x)
	{
		case 1:
			int num;
	
	cout << "Please input a number other than 999" << endl;
	cin >> num;

	while (num<999)
	{
		ave=ave+1;
		sum=num+sum;
		cin >> num;
	}
	if (num==999)
	{
		cout << "The sum is: " << sum << endl;
		cout << "The average is: " << sum/ave << endl;
	}
	break;

this is only a part of the program because it's too long but I think this would be enough for you guys to hel pme.
Last edited on Aug 31, 2013 at 10:48am
Aug 31, 2013 at 7:05am
Probably under your console settings or compiler settings to the amount of characters per line. Or use a few spaces instead of tabs.

You could use basic math and row/column for loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for( int row = 0; row < 10; ++row )
{
    for( int column = 0; column < 10; ++column )
    {
        std::cout << ( column + 1 ) * ( row + 1 ) << '\t';
    }
    std::cout << std::endl;
}
1       2       3       4       5       6       7       8       9       10

2       4       6       8       10      12      14      16      18      20

3       6       9       12      15      18      21      24      27      30

4       8       12      16      20      24      28      32      36      40

5       10      15      20      25      30      35      40      45      50

6       12      18      24      30      36      42      48      54      60

7       14      21      28      35      42      49      56      63      70

8       16      24      32      40      48      56      64      72      80

9       18      27      36      45      54      63      72      81      90

10      20      30      40      50      60      70      80      90      100


Process returned 0 (0x0)   execution time : 0.040 s
Press any key to continue.


*edit if you want an "unfixed" you could just input to two variables and then put those for the maximum row/column



**edit sorry I'm really tired I didn't even look at your code but it looks almost the same as mine if you want an unfixed one try something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main()
{
    int max_rows = 0 , max_columns = 0;
    std::cout << "Please enter the maximum rows : " << std::flush;
    std::cin >> max_rows;
    std::cout << "Please enter the maximum columns : " << std::flush;
    std::cin >> max_columns;
    for( int row = 0; row < max_rows; ++row )
    {
        for( int column = 0; column < max_columns; ++column )
        {
            std::cout << ( column + 1 ) * ( row + 1 ) << '\t';
        }
        std::cout << std::endl;
    }
    return( 0 );
}
Last edited on Aug 31, 2013 at 7:12am
Aug 31, 2013 at 7:29am
I think you're ready to make it into a function kind of like this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void mTable(int start, int end)
{
	for (int x=start; x<=end; x++)
	{
              cout << x << "  ";
        }
}
int main()
{
        mTable(3, 5);
        int wait;
        cin>>wait;

}


I'll leave the actual function programming up to you, but in this case you can type mTable(3, 5); and the numbers 3 through 5 will print to screen, or mTable(1, 10) and 1 through 10 will show up.
Last edited on Aug 31, 2013 at 7:33am
Aug 31, 2013 at 5:41pm
For part two use a do/while loop.

1
2
3
4
5
6
7
int main()
{
    do
    {
        //stuff
    } while( exit_button_not_pressed );
}


*link
http://www.cplusplus.com/doc/tutorial/control/
Last edited on Aug 31, 2013 at 5:42pm
Topic archived. No new replies allowed.