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

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;
}

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

How to make square of numbers in opposite way?

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.

Matrix column comparison

I have to compare matrix columns.
I tried many variations, but as far as I got, is when I compare "next to each other" columns.
// N rows
// M columns
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int N, M, ok = 1;
const int maxn = 1000;
const int maxm = 1000;
short H[maxn][maxm];
cin >> N >> M;
for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> H[i][j];
}
}
for (int j = 1; j < M; ++j)
{
ok = 1;
for (int i = 0; i < N; ++i)
{
if (H[i][j-1] >= H[i][j])
{
ok = 0;
}
}
if (ok)
{
cout << j+1 << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
I have to give back the index of the first column where is true, that the column's every element is greater than ANY OTHER column's elements. (Not summarized.)
For example:
10 10 12 15 10
11 11 11 13 20
12 16 16 16 20
It returns with 4, because the 4th column's every element is greater, than the 1st column's elements.
If there are none, it has to return with -1.
You need another inner loop to go through every other column:
#include <ios>
#include <iostream>
int main()
{
using namespace std;
ios_base::sync_with_stdio(false);
int N, M, ok = 1;
const int maxn = 1000;
const int maxm = 1000;
short H[maxn][maxm];
cin >> N >> M;
for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> H[i][j];
}
}
for (int j = 0; j < M; ++j)
{
for (int j2 = 0; j2 < M; ++j2)
{
if (j == j2) continue;
ok = 1;
for (int i = 0; i < N; ++i)
{
if (H[i][j] <= H[i][j2])
{
ok = 0;
break;
}
}
if (ok)
{
cout << j + 1 << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
Given the input:
3 5
10 10 10 15 10
11 11 11 13 20
12 16 16 14 20
This prints:
4
See it on Rextester

what would be the logic for the given output

here is an output of a pattern program. I'm finding it hard to find the logic for the below ouput. please write the code in C++ ...
output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n+1][n+1];
int st, ed, inc;
int num = 1;
for(int j = 1; j <= n; j++) {
if(j%2==1) {
st = j, ed = n+1, inc = 1;
} else {
st = n, ed = j-1, inc = -1;
}
for(int i = st; i != ed; i += inc) {
arr[i][j] = num;
num++;
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Input: 5
Output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15

C++ 2D Array returning from a function, Error: exited with non-zero status

This code takes 3 parameters as input.
Input : InitialValue, Row, Col
Output : 2D array
Ex: Input 1, 3, 3
Output :
1 2 3
4 5 6
7 8 9
Where am i going wrong here ?
Is there any better way to modify whatever i've written
#include<bits/stdc++.h>
using namespace std;
int** func(int s,int r,int c);
int main()
{
int s,row,col;
cin >> s >> row>> col;
int** res;
res = func(s,row,col);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout << res[i][j]<<" ";
}
cout << endl;
}
return 0;
}
int** func(int s,int r,int c)
{
int** arr;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
arr[i][j]=s;
s = s+1;
}
}
return arr;
}
You have to use malloc for arr.
int **arr = (int **) malloc(r * sizeof(int **));
for(int k = 0; k<c; k++)
arr[k] = (int *) malloc(sizeof(int))
Then you can use new
int **arr = new int*[r];
for(int k = 0; k<c; k++)
arr[k] = new int[c];
And don't forget to use delete.
What about something like this?
#include <iostream>
#include <memory>
void FillArray(int start, int rows, int cols, std::unique_ptr<int[]> &result)
{
int size = rows * cols;
result = std::make_unique<int[]>(size);
for (int i = 0; i < size; ++i)
result[i] = start + i;
}
void PrintArray(int rows, int cols, std::unique_ptr<int[]> &arr)
{
int size = rows * cols;
for (int i = 0; i < size; ++i)
std::cout << (i % cols == 0 ? "\n" : " ") << arr[i];
std::cout << std::endl;
}
int main()
{
int start, rows, cols;
std::unique_ptr<int[]> arr;
std::cout << "Enter params (start cols rows): ";
std::cin >> start >> cols >> rows;
FillArray(start, rows, cols, arr);
PrintArray(rows, cols, arr);
return 0;
}
For example:
Enter params (start cols rows): 10 10 5
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 49
50 51 52 53 54 55 56 57 58 59