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 .....
#include <iostream>
#include <iomanip>
#include <cctype>
usingnamespace 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
}
}
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:
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.
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.
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??