C++ - Building Addition Tables - c++

I am having some troubles with this problem presented from my lab. My goal is to produce an addition table that looks something like this -
(From range(1-5)) :
+ 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
Mine is looking like this, however :
+ 1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
My code looks like this :
if (choice == ADD) {
cout << "+";
for (int i = 0; i < max; i++) {
cout << "\t";
for (int j = min; j <= max; j++) {
cout << i + j << "\t";
}
}
}
(For reference, int max = maximum number in range, int min = minimum number in range, and choice is the decision for user to do either an addition or multiplication table). How can I change my code to fit the proper format? I can't seem to figure it out. Any hints/help would be greatly appreciated :)

#include <iostream>
using namespace std;
int main(){
int max = 5;
int min = 1;
if (true){
cout << "+\t";//print out the initial +
for(int i = min; i <= max; i++) cout << i << "\t";//print out the entire first row
cout << "\n"; //start the next row
//here is the main loop where you do most of the logic
for(int i = min; i <= max; i++){
cout << i << "\t"; //this prints out the first column of numbers
for(int j = min; j <=max; j++){
cout << j+i << "\t"; //this line fills in the body of your table
}
cout << "\n";//creates the space between each row
}
}
}

This code builds the table as explained:
for (int i = 0; i <= max; i++) {
if (i == 0)
cout << '+';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
cout << '\n';
}
Tip: when you want to print only a character, it is more efficient to use single quotes like '+' or '\t'. Double quotes are more expensive because they represent a const char*.

Related

Write a C++ program which will print (half pyramid) pattern of natural numbers

The task:
Write a C++ program which will print (half pyramid) pattern of natural numbers.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
I have tried using this code, but its not giving the output:
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
j in your inner loop is the 1 based index of the element in the current row (1,2,3 etc.).
Instead of printing it, you should print a counter that is increased over all the iterations.
Something like:
#include <iostream>
int main()
{
int rows, i, j;
std::cout << "Enter number of rows: ";
std::cin >> rows;
int n = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
std::cout << n << " ";
n++; // for next iteration
}
std::cout << "\n";
}
return 0;
}
Output example:
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.

Align output in c++

int n, o = 2;
cout << "n="; cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = n; j >= i; j--) {
cout << o << " ";
o += 2;
}
cout << endl;
}
My code outputs (when n is 4)
2 4 6 8
10 12 14
16 18
20
How would I align it to output this?
2 4 6 8
10 12 14
16 18
20
I am trying to align the output to the right in such a way that the numbers are aligned one below another (for example the 4 is aligned to the space between the 10 and 12 and I want to align it to 10). Thanks.
The header <iomanip> provides many input/output manipulators you can play with, one beeing std::setw, which sets the width parameter of the stream for the next item.
Changing the line in the posted snippet that outputs the numbers into
std::cout << std::setw(3) << o;
The output becames
2 4 6 8
10 12 14
16 18
20
Now, to produce the desired output, we can notice that the first number of each row can either be printed with an increasing "width" (3 the first row, 6 the second and so on) or after a loop that prints the right amount of spaces (0 the first row, 3 the second and so on).
just edited the inner loop of your code and this is the edited code:
#include <iostream>
using namespace std;
int main() {
int n, o = 2;
cout << "n="; cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j >= i) {
cout << o << "\t";
o += 2;
}
else
{
cout << "\t";
}
}
cout << endl;
}
}
and this is some example output:
n=4
2 4 6 8
10 12 14
16 18
20
what I did is to make the inner loop, loops from 1 till n like the outer loop and only check if the current column is greater than or equal to the current row, if so, then I print the number and increment it, if not then I just cout a space or a tab to be more specific

How can i write an array starting from right to left in C++

I don't want to reverse or shift the array. What I want is to write the array from right to left.
I did something like
int arr[5] = {0};
for (int i = 0; i < 5; i++)
{
cout << "enter a number : ";
cin >> arr[i];
for (int j = 0; j < 5; j++)
{
if (arr[j] != 0)
{
cout << arr[j] << " ";
}
else
cout << "X ";
}
cout << endl;
}
and on the output screen I see this
enter a number : 5
5 X X X X
enter a number : 4
5 4 X X X
enter a number : 3
5 4 3 X X
enter a number : 2
5 4 3 2 X
enter a number : 1
5 4 3 2 1
Press any key to continue . . .
but i want to see this
enter a number : 5
X X X X 5
enter a number : 4
X X X 5 4
enter a number : 3
X X 5 4 3
enter a number : 2
X 5 4 3 2
enter a number : 1
5 4 3 2 1
Press any key to continue . . .
how can I do that?
I will be glad if you help.
Just change the inner for loop for example the following way
int j = 5;
for ( ; j != 0 && arr[j-1] == 0; --j )
{
std::cout << 'X' << ' ';
}
for ( int k = 0; k != j; k++ )
{
std::cout << arr[k] << ' ';
}
I think you are trying to write into the last index each time, and then move each number backwards in the array?
To write into the last index use cin >> arr[4].
Then After printing out the array copy each value into the previous index. Make sure that your copy only when the J index is less than 4
int arr[5] = {0};
for (int i = 0; i < 5; i++)
{
cout << "enter a number : ";
//write into the last position
cin >> arr[4];
for (int j = 0; j < 5; j++)
{
if (arr[j] != 0)
{
cout << arr[j] << " ";
}
else
cout << "X ";
//don't go out of bounds. This should move each number to the previous index
if(j<4){
arr[j]=arr[j+1];
}
}
cout << endl;
}

How to get symetrical matrix?

I have to write function:
void function (int * a [], int sq) checking if the sq-degree square matrix is ​​symmetrical
(relative to the main diagonal upper left - lower right) and performing any transposition of this matrix.
My main :
#include <iostream>
using namespace std;
void function(int* a[], int sq);
int main() {
const int sq = 10;
int a[sq][sq];
int *tab[sq];
for (int i = 0; i < 11; i++) {
tab[i] = a[i];
}
function(tab,sq);
return 0;
}
My function:
void function(int* a[], int sq) {
cout << "tell me the number and i give you (sq x sq) matrix:";
cin >> sq;
for(int i = 0; i < sq; i ++) {
for(int j = 0 ; j < sq; j++) {
cin >> a[i][j];
}
}
// 3 7 8 2
// 7 4 5 5
// 8 2 1 6
// 2 5 6 1
cout << "Matrix is " << sq << " degree:" << endl;
for(int i = 0; i< sq; i++) {
for(int j = 0; j < sq; j++) {
if(a[0][j] == a[i][0] && a[i-1][j] == a[i][j-1] ) {
cout << a[i][j] << " "; // if symetrical
} else {
cout << a[j][i] << " "; //if not, lets do transposition
}
}
cout << endl;
}
}
Example:
// 3 7 8 2
// 7 4 5 5
// 8 2 1 6
// 2 5 6 1
This is symetrical and should give me output:
Matrix is 4 degree:
3 7 8 2
7 4 5 5
8 2 1 6
2 5 6 1
But it's doing transposition and give me:
Matrix is 4 degree:
3 7 8 2
7 4 2 5
8 5 1 6
2 5 6 1
If i understand contents of exercise...
replace the for loop like below
your code :
for(int i = 0; i< sq; i++) {
for(int j = 0; j < sq; j++) {
if(a[0][j] == a[i][0] && a[i-1][j] == a[i][j-1] ) {
cout << a[i][j] << " "; // if symetrical
} else {
cout << a[j][i] << " "; //if not, lets do transposition
}
}
cout << endl;
}
Replace with below code :
for(int i = 0; i< sq; i++) {
for(int j = i; j < sq; j++) {
if(a[i][j] == a[j][i] ) {
cout << a[i][j] << " "; // if symetrical
} else {
cout << a[j][i] << " "; //if not, lets do transposition
}
}
cout << endl;
}

Wrong output while printing a multidimensional array

i have some trouble while printing this pseudo-multidimensional array , with elements that are set already.
And the point of it is to swap the first and third row and 2nd and 4th column, but the output looks weird...
#include <iostream>
using namespace std;
int main()
{
int arr[12]= {
1,2,3,4,5,6,7,8,6,4,5,3
};
cout << "Before change: "<<endl;
for (int row=0;row<3;row++){
for (int col=0;col<4;col++){
cout << arr[row*col]<<" ";
}
cout <<endl;
}
cout << "After the row change: "<<endl;
for (int row=2;row>=0;row--){
for(int col=0;col<4;col++){
cout<<arr[row*col]<<" ";
}
cout<<endl;
}
cout << "After the column change: "<<endl;
int temp;
for(int row=0;row<3;row++){
temp=arr[row*1];
arr[row*1]=arr[row*3];
arr[row*3]=temp;
for (int col=0;col<4;col++){
cout<<arr[row*col]<<" ";
}
cout<<endl;
}
Instead of having an output like this:
1 2 3 4
5 6 7 8
6 4 5 3
6 4 5 3
5 6 7 8
1 2 3 4
6 3 5 4
5 8 7 6
1 4 3 2
i get this :
1 1 1 1
1 2 3 4
1 3 5 7
1 3 5 7
1 2 3 4
1 1 1 1
1 1 1 1
1 4 3 2
1 7 5 3
You wrong how to calculate the element inside the array arr[row*col] will be always 0 for the first row ( row = 0). So you have to do something like this:
#include <iostream>
using namespace std;
int main()
{
int arr[12] = {
1, 2, 3, 4, 5, 6, 7, 8, 6, 4, 5, 3
};
**int dimCol = 4;**
cout << "Before change: " << endl;
for (int row = 0; row < 3; row++){
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
cout << "After the row change: " << endl;
for (int row = 2; row >= 0; row--){
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
cout << "After the column change: " << endl;
int temp;
for (int row = 0; row < 3; row++){
temp = arr[row * 1];
arr[row * 1] = arr[row * 3];
arr[row * 3] = temp;
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
}
The formule will be: array[row*numberOfColumn + Column]
You got a wrong argument definition:
Instead of
array[row*col]
write this
array[row*4 + col]
So the formula is:
array[row*total_col + col]
The loop you are using multiplies each time your variable with Zero that's why you are getting 1 at the beginning of every line as your first element is 1, and arr[0] is 1.
*and your line 1 is
1 1 1 1*
because value of outer loop is zero and any value of variable of inner loop multiplied will result in 0.
the reason you are not getting correct output is your logic to print all element is not correct.
go for
array[row*4 + col]