How to make square of numbers in opposite way? - c++

I have made a code that makes this kind of output:
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 35 36
37 38 39 40 41 42
43 44 45 46 47 48
#include <iostream>
using namespace std;
int main() {
// dimensions:
int x=6,y=8;
int sum=0;
int numery[8][6]={};
for (i = 0; i < y; i++) {
for (j = 0; j < x; j++) {
numery[i][j]=++sum;
if (numery[i][j]<=9) cout << " ";
cout << numery[i][j] << " ";
}
cout << endl;
}
return 0;
}
But I don't know how to edit it to get this kind of output:
6 5 4 3 2 1
7 8 9 10 11 12
18 17 16 15 14 13
19 20 21 22 23 24
30 29 28 27 26 25
31 32 33 34 35 36
37 38 39 40 41 42
43 44 45 46 47 48
I can think of making if statement for each i%2==1 that it should go backwards, but I don't know how to make program do such thing. Otherwise it should go normally. So if it find an even row it should go like 7 8 9 10 11 12, whereas if it's i%2==1 it should go like 6 5 4 3 2 1 and so on.
Some suggestions?

One solution would be the following (I assume that you do not really need to store the numbers). Notice that setw() can be used to print the numbers with fixed width:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// dimensions:
int x=6,y=8;
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
cout << setw(3) << ((i%2) ? i*x+j+1 : i*x+x-j) << " ";
}
cout << endl;
}
return 0;
}

First get rid of the array. It is unnecessary.
#include <iostream>
using namespace std;
int number(int i,int j,int width) { return j+i*width +1 ;}
int main() {
// dimensions:
int x=6,y=8;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
auto n = number(i,j,x);
if (n <= 9) cout << " ";
cout << n << " ";
}
cout << endl;
}
return 0;
}
This has same output as your code.
Next consider what happens to j when you go reverse. You should think that through, the answer is: You replace j with width-j-1.
int number_reverse(int i,int j, int width) { return number(i,width-j-1,width); }
Eventually you just need to call the right function in each iteration of the outer loop:
#include <iostream>
using namespace std;
int number(int i,int j,int width) { return j+i*width +1 ;}
int number_reverse(int i,int j, int width) { return number(i,width-j-1,width); }
int main() {
// dimensions:
int x=6,y=8;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
auto n = (i%2) ? number_reverse(i,j,x) : number(i,j,x);
if (n <= 9) cout << " ";
cout << n << " ";
}
cout << endl;
}
return 0;
}

Here is a solution in case you DO need to store the numbers in an array.
#include <iostream>
using namespace std;
int main() {
// dimensions:
int x=6,y=8;
int sum=0;
int numery[y][x];
for (int i = 0; i < y; i++)
for (int j = 0; j < x; j++)
numery[i][j]=++sum;
for(int i = 0; i < y; i++)
{
if(i%2 == 0)
{
for(int j = x-1; j >= 0; --j)
{
if(numery[i][j] < 10)
{
cout << " ";
}
cout << numery[i][j] << " ";
}
cout << endl;
}
else
{
for(int j = 0; j < x; j++)
{
if(numery[i][j] < 10)
{
cout << " ";
}
cout << numery[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
you assign the numbers to your array first. then you print the numbers from back-to-front or front-to-back based on if are on an even or odd row.

If you are willing to fill the array in non-linear order, you can fill every second line from right to left: array[i][x-1-j]=++sum;.
Another approach is to use one outer loop and two inner loops, noticing that all the rows are of form a*i + b*j + c, where b is either +1 or -1. This simplifies to array[i][j] = 6*i + 6 - j for even rows and to array[i][j]=6*i + 1 + j for the odd rows.
One more option is to have a single inner loop, which will increase or decrease by some amount d = i % 2 == 0 ? -1 : 1;
for (int j = 0; j<x; j++) { arr[i][j]=sum; sum += d;}
sum+=6-d;
After each row we need to compensate the variable sum so that it begins with the correct value for the next index i+1.

Related

A Program that accepts looping limit and will serve as multiplier of itself(per column) using C++

I have a Activity that accepts looping limit and will serve as multiplier of itself(per column) this is the example output
3 9 27
9 27 81
27 81 243
but this is my output
Enter number of rows: 3
3 9 27
81 243 729
2187 6561 19683
This is the code
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
int i, j;
int num = 1;
int plc = 1;
for(i = 1; i <= rows; i++)
{
for(j = 1 ; j<=rows ; j++)
{
plc = plc * rows;
cout << plc << " ";
}
cout << endl;
}
return 0;
}
Where I did wrong?
You are only multiplying the previous value by rows(3) each time and not taking into account the row and columns, the code below will solve your problem:
int main()
{
cout << "Enter number of rows: ";
cin >> rows;
int i, j;
int num = 1;
int plc = 1;
int count = 0;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows; j++)
{
plc = 1;
for (int mult=0;mult< ((i - 1) + j);mult++)
{
plc *= 3;
}
cout << plc << " ";
}
cout << endl;
}
return 0;
}
Gives the expected outout:
3 9 27
9 27 81
27 81 243
Perhaps you forgot to use the num variable?
#include <iostream>
int main( int argc, char* argv[] )
{
int rows = ::atoi(argv[1]);
int num = 1;
for(int i = 1; i <= rows; i++)
{
int plc = num;
num *= rows;
for(int j = 1 ; j<=rows ; j++)
{
plc = plc * rows;
std::cout << plc << " ";
}
std::cout << std::endl;
}
return 0;
}
Produces:
Program stdout
3 9 27
9 27 81
27 81 243
Godbolt: https://godbolt.org/z/sfn119vTs

C++ "Flipping" the rows in an array

I am attempting to print out an array in a specific order, where it is formatted with columns and rows in ascending order, but with the bottom row containing the lowest values. The array is created via a for loop and a pointer.
here is my code so far:
#include <iostream>
#include <iomanip>
int main()
{
// Creation of the array
int* array = new int[24];
for (int i = 0; i < 24; i++)
{
array[i] = i;
}
// Displaying in grid format with 3 rows and 8 columns
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 8; j++)
{
std::cout << std::setw(2) << *array << ' ';
array++;
}
std::cout << '\n';
}
}
The output of my code is:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
The desired output is:
16 17 18 19 20 21 22 23
8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7
How would I go about "reversing" the rows to reach the desired output?
SOLVED WITH THE FOLLOWING
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int* table = new int[40];
for (int i = 0; i < 40; ++i)
{
table[i] = i;
}
for (int i = 4; i >= 0; --i)
{
for (int j = 0; j < 8; ++j)
{
cout << setw(2) << table[j + (8 * i)] << ' ';
}
cout << "\n";
}
}
Thanks for the help.

making a circle around a two dimensional array

I am having trouble with making a turn inside a two dimensional array to output the elements in spiral. I tried this code, but it is outputting not enough elements, I tried to make some if statements outside of the loop to cover all cases for which the general algorithm doesn't output. Can you help suggesting some way to manage the correct output.
CODE
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c) / 2; runs--;) {
for (int i = c_beg; i < c_end; i++)
cout << m[r_beg][i] << " ";
for (int i = r_beg; i < r_end; i++)
cout << m[i][c_end] << " ";
for (int i = c_end; i > c_beg; i--)
cout << m[r_end][i] << " ";
for (int i = r_end; i > r_beg; i--)
cout << m[i][c_beg] << " ";
r_beg++;
c_beg++;
r_end--;
c_end--;
}
if (r <= c && c_beg <= c_end) {
for (int i = c_beg; i <= c_end; i++)
cout << m[r_end][i] << " ";
}
else if (r >= c && r_beg <= r_end) {
for (int i = r_beg; i <= r_end; i++)
cout << m[i][c_end] << " ";
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5
If you have for example 3x10 matrix. It doesn't output.
Input:
3
7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
Output: should get to 13, but it stops at 8.
The code, as presented, loops towards center until the smaller of the two dimensions is consumed up. However, if that smaller dimension has odd size, then parts of the central row or column respectively haven't been printed out. You can cover that one with some special case handling after your outer loop:
for (int runs = std::min(r, c) / 2; runs--;)
{
// ...
}
if(c < r)
{
if(c & 1)
{
for (int i = r_beg; i <= r_end; i++)
// ^ (!)
// don't forget to print last element: there's no second loop
// that would print the corner element a second time now!
std::cout << m[i][c_end] << " ";
}
}
else
{
// handles the square matrix case as well
if(r & 1)
{
for (int i = c_beg; i <= c_end; i++)
std::cout << m[r_beg][i] << " ";
}
}
This can be solved by carefully fine-tuning the bail-out conditions / ranges of the for loops:
#include <iostream>
using namespace std; // :-(
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c);;)
{
for (int i = c_beg; i <= c_end; i++)
cout << " " << m[r_beg][i];
++r_beg;
for (int i = r_beg; i <= r_end; i++)
cout << " " << m[i][c_end];
--c_end;
if (!--runs) break;
for (int i = c_end; i >= c_beg; i--)
cout << " " << m[r_end][i];
--r_end;
for (int i = r_end; i >= r_beg; i--)
cout << " " << m[i][c_beg];
++c_beg;
if (!--runs) break;
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Input:
3 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Output:
1 2 3 4 5 6 7 14 21 20 19 18 17 16 15 8 9 10 11 12 13
Live Demo on ideone
Input:
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Notes:
I changed the bail-out of the for loop:
Instead of min(r, c) / 2, I use min(r, c) and decrement/check runs twice in the body.
I adjusted the update of r_beg, r_end, c_beg, and c_end.

Showing sort simulation

I'm trying to show the simulation of this bubble sort and I used a function swapper that has reference values but when I try to print it after the swap it also prints out the memory address. How can I fix this?
void swapper(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return;
}
int main(){
//Bubble sort
int arr[] = {-2, 45, 0, 11, -9};
int n = 5;
for(int step = 0; step < n-1; step++) {
for(int i = 0; i < n; i++) {
if(arr[i] > arr[i + 1])
swapper(arr[i], arr[i + 1]);
}
for(int x = 0; x < n; x++)
cout << arr[x] << " ";
cout << endl;
}
Fixing the bugs
Try this:
#include <iostream>
using namespace std;
void swapper(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return;
}
int main(){
//Bubble sort
int arr[] = {-2, 45, 0, 11, -9};
int n = 5;
for(int step = 0; step < n-1; step++) {
for(int i = 0; i+1 < n; i++) {
if(arr[i] > arr[i + 1])
swapper(arr[i], arr[i + 1]);
}
for(int x = 0; x < n; x++)
cout << arr[x] << " ";
cout << endl;
}
}
I added a missing } and changed the condition of the inner loop to i+1 < n to avoid array access out of bounds.
This is the output:
-2 0 11 -9 45
-2 0 -9 11 45
-2 -9 0 11 45
-9 -2 0 11 45
Aligning the output
You can make the output aligned more nicely by replacing
cout << arr[x] << " ";
with
cout << setw(2) << arr[x] << " ";
and writing
#include <iomanip>
at the top of your file. Then the output is
-2 0 11 -9 45
-2 0 -9 11 45
-2 -9 0 11 45
-9 -2 0 11 45

C++ Magic Square from txt: Only reads first square

This is related to another question I've asked, but now I've gotten a little farther. I am trying to create a Magic Square program based on a text file input. The program will read the first square but then prints out gibberish for the rest of them. Where there is an individual number, it's supposed to set the parameters for the square following it.
So here's an example from the text file:
3 <- this indicates the rows&columns
4 9 2 <- row 1
3 5 7 <- row 2
8 1 6 <- row 3
5 <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Here's the program that I have so far.
I'm trying to follow the assignment instructions, which is why I have 2d arrays and functions set up the way I do
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int SIZE = 20;
void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);
int main()
{
int n;
int square[SIZE][SIZE];
ifstream inputFile;
inputFile.open("Prog2Input.txt");
while (inputFile >> n)
{
readSquare(n, square);
printSquare(n, square);
if (checkMagic(n, square))
{
cout << "Magic Square" << endl;
}
else
{
cout << "NOT Magic Square" << endl;
}
system("pause");
}
system("pause");
return 0;
}
void readSquare(int n, int square[][SIZE]) {
ifstream inf("Prog2Input.txt");
int x;
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
inf >> x;
square[i][j] = x;
}
}
}
void printSquare(int n, int square[][SIZE]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << square[i][j] << " ";
}
cout << endl;
}
}
bool checkMagic(int n, int square[][SIZE]) {
int total = ((1 + n*n) / 2)*n;
for (int i = 0; i < n; i++) {
if (sumRow(i, n, square) != total) {
return false;
}
}
for (int i = 0; i<n; i++) {
if (sumColumn(i, n, square) != total) {
return false;
}
}
if (sumDiagonal1(n, square) != total) {
return false;
}
if (sumDiagonal2(n, square) != total) {
return false;
}
return true;
}
int sumRow(int row, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[row][i];
}
return sum;
}
int sumColumn(int col, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][col];
}
return sum;
}
int sumDiagonal1(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][i];
}
return sum;
}
int sumDiagonal2(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][(n - i) - 1];
}
return sum;
}
All, I appreciate your help in trying to resolve this. I took your comments in combination with my professors to make the below code. This will process an unknown number of magic square matrices specified by an initial size integer.
Sample text file first:
3 <- this indicates the rows&columns
4 9 2 <- row 1
3 5 7 <- row 2
8 1 6 <- row 3
5 <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Now the final product of the code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int SIZE = 20;
ifstream inf;
void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);
int main()
{
int n;
int square[SIZE][SIZE];
inf.open("Prog2Input.txt");
while (inf >> n)
{
cout << "Matrix Size: " << n << endl;
readSquare(n, square);
printSquare(n, square);
if (checkMagic(n, square))
{
cout << "Magic Square" << endl;
}
else
{
cout << "NOT Magic Square" << endl;
}
cout << endl;
}
system("pause");
return 0;
}
void readSquare(int n, int square[][SIZE]) {
int x;
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
inf >> x;
square[i][j] = x;
}
}
}
void printSquare(int n, int square[][SIZE]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << square[i][j] << " ";
}
cout << endl;
}
}
bool checkMagic(int n, int square[][SIZE]) {
int total = ((1 + n*n) / 2)*n;
for (int i = 0; i < n; i++) {
if (sumRow(i, n, square) != total) {
return false;
}
}
for (int i = 0; i<n; i++) {
if (sumColumn(i, n, square) != total) {
return false;
}
}
if (sumDiagonal1(n, square) != total) {
return false;
}
if (sumDiagonal2(n, square) != total) {
return false;
}
return true;
}
int sumRow(int row, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[row][i];
}
return sum;
}
int sumColumn(int col, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][col];
}
return sum;
}
int sumDiagonal1(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][i];
}
return sum;
}
int sumDiagonal2(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][(n - i) - 1];
}
return sum;
}