PLEASE HELP!

Hey guys I need help with this program. I am suppose to print a table from 1 to 80 but make the numbers go down the columns instead of the rows. Like instead of
1 2 3 4 5
6 7 8 9 .....

i'm suppose to do
1 3 5 ....
2 4 6

heres the code i made to print the table:

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 <cctype>
using namespace std;
	
int main(int)
{		
int r, c;
	
	// output the ASCII table
	for (r=1;r<81; r=r+8)	// 16 rows  
							// I'm incrementing by 8 because there will be 8 columns.
							// r is the value of the code to be output in the first column.
	{
		for (c=0;c<8;c++)	// and 8 columns
		{					// output each number, and print the char if it is a printable character
		    int code=r+c;				// ASCII code
					// and as a char
			
				cout << " " <<  " " << setw(3) << code<<"\t";		// char is printable.  
			
		}
		cout << endl;		// output a newline at the end of each row
	}
}
Print only odd numbers on the first line up until 79 inclusive, and print only evens on the next line up to and including 80.
How would i go about doing that? I tried messing with the for (c=0;c<8;c++) line but i cant seem to get it working
Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>  
#include <iomanip>

int main () 
{	
	//For 5 rows and 16 columns. ;)
	for(int i = 0; i < 5; ++i)
	{
		for(int j = 0; j < 16; ++j)
			std::cout << std::setw(4) << i + 5*j + 1;
		std::cout << '\n';
	}
}

Output:
1
2
3
4
5
   1   6  11  16  21  26  31  36  41  46  51  56  61  66  71  76
   2   7  12  17  22  27  32  37  42  47  52  57  62  67  72  77
   3   8  13  18  23  28  33  38  43  48  53  58  63  68  73  78
   4   9  14  19  24  29  34  39  44  49  54  59  64  69  74  79
   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80

Modify for your need. ;)
Last edited on
K, I got that part down but now i need to cap it at 80:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

using namespace std;
	
int main () 
{	
	int columns = 0, rows = 0; 
	cout << "How many columns: ";
	cin >> columns;
	cout << "How many rows: ";
	cin >> rows;

	for(int i = 0; i < rows; ++i) //rows
	{
		for(int j = 0; j < columns; ++j) //columns
			std::cout << std::setw(4) << i + rows*j + 1;
		std::cout << '\n';
	}
}


Please Help!!!
actually, its suppose to be modify it so it prompts you for the maximum number to output, and the number of columns?

I need to change the last program around but does someone know how to do that?
In my post modify lines 6(!), 7, 9 and 10 with suitable values so the product to be 80:
for ex. i < 8 (in line7) , j < 10 (line 9) and the coefficient of j (line 10) to be 8.
The output will be:
1
2
3
4
5
6
7
8
   1   9  17  25  33  41  49  57  65  73
   2  10  18  26  34  42  50  58  66  74
   3  11  19  27  35  43  51  59  67  75
   4  12  20  28  36  44  52  60  68  76
   5  13  21  29  37  45  53  61  69  77
   6  14  22  30  38  46  54  62  70  78
   7  15  23  31  39  47  55  63  71  79
   8  16  24  32  40  48  56  64  72  80

That values must have the product equal 80 ( of course in this case). Good luck!
Last edited on
Actually i'm suppose to ask how many numbers to output. He said: Integer arithmetic and the modulus operator come in handy here for figuring out how many rows to output and how much to add when moving to the next column. Note that if the number of columns does not divide evenly into the total number to output , you’ll have to do something creative to avoid writing more numbers than you should. This will require a little thought, and the use of an if/then/else.

But im stuck like hell?
The product ROWS * COLUMNS gives the number of values in a rectangular table so this number will be a composite number not prime, its divisors may be ROWS or COLUMNS. I think I was quite exactly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main () 
{	
	int columns = 0, rows = 0, numbers = 0;
	cout << "How many numbers to output: ";
	cin >> numbers;
	cout << "How many columns: ";
	cin >> columns;
	rows = numbers/columns;
	for(int i = 0; i < rows; ++i) //rows
	{
		for(int j = 0; j < columns; ++j) //columns
			std::cout << std::setw(4) << i + rows*j + 1;
		    std::cout << '\n';
	}
}


This seems to work but only if the numbers divide evenly. I tried making rows= (numbers/columns)+1; but then it prints too many numbers. Can someone help me figure this out??
Last edited on
Enter the desired number for output and the program chooses the most appropriate number of rows and columns and...populate the table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>  
#include <iomanip>
#include <cmath>

int main () 
{	
	int n;
	std::cout << "How many numbers to output: ";
	std::cin >> n;
	int k = sqrt(n);
	int rows = k;
	int columns = n/k + 1;
	{
		for(int i = 0; i < rows; ++i)
		{
			for(int j = 0; j < columns; ++j)
				{
					if((i + rows*j + 1) > n) break;
					std::cout << std::setw(4) << i + rows*j + 1;
				}
			std::cout << '\n';
		}
	}
}

Samples of outputs:

1
2
3
4
5
6
7
8
9
How many numbers to output: 67
   1   9  17  25  33  41  49  57  65
   2  10  18  26  34  42  50  58  66
   3  11  19  27  35  43  51  59  67
   4  12  20  28  36  44  52  60
   5  13  21  29  37  45  53  61
   6  14  22  30  38  46  54  62
   7  15  23  31  39  47  55  63
   8  16  24  32  40  48  56  64

1
2
3
4
5
6
7
8
9
How many numbers to output: 79
   1   9  17  25  33  41  49  57  65  73
   2  10  18  26  34  42  50  58  66  74
   3  11  19  27  35  43  51  59  67  75
   4  12  20  28  36  44  52  60  68  76
   5  13  21  29  37  45  53  61  69  77
   6  14  22  30  38  46  54  62  70  78
   7  15  23  31  39  47  55  63  71  79
   8  16  24  32  40  48  56  64  72


Now it's ok? ;)

Last edited on
Have to ask for how many numbers to output and number of columns
One more time: ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>  
#include <iomanip>
#include <cmath>

int main () 
{	
	int n, columns;
	std::cout << "How many numbers to output: ";
	std::cin >> n;
	std::cout << "How many columns: ";
	std::cin >> columns;
	int rows = n / columns + 1;
	{
		for(int i = 0; i < rows; ++i)
		{
			for(int j = 0; j < columns; ++j)
				{
					if((i + rows*j + 1) > n) break;
					std::cout << std::setw(4) << i + rows*j + 1;
				}
			std::cout << '\n';
		}
	}
}

with some outputs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
How many numbers to output: 69
How many columns: 8
   1  10  19  28  37  46  55  64
   2  11  20  29  38  47  56  65
   3  12  21  30  39  48  57  66
   4  13  22  31  40  49  58  67
   5  14  23  32  41  50  59  68
   6  15  24  33  42  51  60  69
   7  16  25  34  43  52  61
   8  17  26  35  44  53  62
   9  18  27  36  45  54  63

How many numbers to output: 73
How many columns: 11
   1   8  15  22  29  36  43  50  57  64  71
   2   9  16  23  30  37  44  51  58  65  72
   3  10  17  24  31  38  45  52  59  66  73
   4  11  18  25  32  39  46  53  60  67
   5  12  19  26  33  40  47  54  61  68
   6  13  20  27  34  41  48  55  62  69
   7  14  21  28  35  42  49  56  63  70

All is well when ends well! ;)

And with that I owe you big time sir u are fucking awesome and saved my ass!
Don't mention it! All the best and happy programming!

EDIT: I forgot something to do: please delete line 3 in my program; it's redundant. :))
Last edited on
Topic archived. No new replies allowed.