I am trying to get my program print out the rows, columns or diagonals if they don't equal the magic square rule, for example,
if the matrix is
1 9 5
2 4 3
6 8 7
Row 1 [2, 4, 3] doesn't work
Row 2 [6, 8, 7] doesn't work
Column 0 [1, 2, 6] doesn't work
Diagonal 1 [1, 4, 7] doesn't work
I've tried print("%d", matrix[row])
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
int main()
{
//declared variables
int size = 3;
int matrix[3][3];
int row, column = 0;
int sum0, sum1, sum2;
int flag = 0;
//ask user to input 1-9 and scans it
printf("Enter in the values: \n");
for (row = 0; row < size; row++){
for (column = 0; column < size; column++)
scanf("%d", &matrix[row][column]);
}
//enters number into magic square format
printf("You entered: \n");
for (row = 0; row < size; row++) {
printf("\n");
for (column = 0; column < size; column++) {
printf("%d ", matrix[row][column]);
}
}
//diagonal calculations
sum0 = 0;
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
if (row == column)
sum0 = sum0 + matrix[row][column];
}
}
//row calculations
for (row = 0; row < size; row++) {
sum1 = 0;
for (column = 0; column < size; column++) {
sum1 = sum1 + matrix[row][column];
}
if (sum0 == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//column calculations
for (row = 0; row < size; row++) {
sum1 = 0;
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[row][column];
}
if (sum1 == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
printf("\nAnalyzing...\n");
if (flag == 1) {
sleep(2);
printf("This is a magic square!\n");
}
else {
sleep(2);
printf("This is not a magic square!\n");
}
return 0;
}
You have to use loops to print each character separately.
printf(3) offers no way to print an array of integers.
it seems weired that initailzied sum1 in colum calculations
//column calculations
for (row = 0; row < size; row++) {
sum1 = 0; // <= sum2 = 0; maybe
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[row][column];
}
if (sum1 == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
and if row calcuations used loop as below
for(row = 0; row < size; row++){
for(column = 0; column < size; column++){
// access to matrix[row][column]
}
}
column calculations loop will be revised
for(column = 0; column < size; column++){
for(row = 0; row < size; row++){
// access to matrix[row][column]
}
}
plus, you can write the diagonal calculations simply
//diagonal calculations
sum0 = 0;
for (int diag = 0; diag < size; diag++) {
sum0 += matrix[diag][diag];
}
I think you get the logic:
const int x = 5;
const int y = 7;
int matrix[x][y];
for (int j = 0; j < x; ++j)
{
for (int i = 0; i < y; ++i)
{
matrix[j][i] = j + i;
printf("%3d ", matrix[j][i]);
}
putchar('\n');
}
puts("\nPrint a column: ");
for(int i = 0; i < x; i++)
{
printf("%3d ", matrix[i][0 /*constant*/]);
}
puts("\nPrint a row: ");
for (int k = 0; k < 7; ++k)
{
printf("%3d ", matrix[0/*constant*/][k]);
}
puts("\nPrint a diagonal");
for (int l = 0, m = 0; l < x && m < y; ++l, ++m)
{
printf("%3d ", matrix[l][m]);
}
Related
I have a problem. I need to write a program that checks if every row has the same sum, and if every column has same sum.
Example:
3 3 3
3 3 3
In this example output should be "YES" for rows and "YES" for columns.
Another example:
4 5 6
6 4 5
In this example, output should be "YES" for rows because sum of 1st and 2nd row is the same (15), and it should be "NO" for columns because sum is not the same (10,9,11).
I have made code that checks first row and first column than it compares if they are same as the other ones. I have made it to check if its not but I don't know how to check it if it is, I mean how to output "YES" for both cases.
This is my code:
#include <stdio.h>
int main() {
int mat[100][100];
int n, m;
int i, j;
int sumak = 0;
int sumar = 0;
int sumarp = 0;
int sumakp = 0;
do {
printf("Unesite brojeve M i N: ");
scanf("%d %d", &m, &n);
} while (m < 0 || m > 100 || n < 0 || n > 100);
printf("Unesite clanove matricee: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// suma of first row
for (i = 0; i < 1; i++) {
for (j = 0; j < n; j++) {
sumarp = sumarp + mat[i][j];
}
sumarp = sumarp + mat[i][j];
}
// sum of all rows
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
sumar = sumar + mat[i][j];
}
if (sumarp != sumar) {
printf("NE");
} else {
sumar = 0;
continue;
}
}
// sum of the first column
for (j = 0; j<1; j++) {
for (i = 0; i< m;i++ ) {
sumakp = sumakp + mat[i][j];
}
sumakp = sumakp + mat[i][j];
}
// sum of every column
for (j = 1; j < n; j++) {
for (i= 0; i < m; i++) {
sumak = sumak + mat[i][j];
}
if (sumakp == sumak) {
sumak=0;
continue;
}
if(sumakp!=sumak)
{
printf("NE");
return 0;
}
else{
printf("DA");
}
}
}
So if someone can explain me how to do the rest of it.
Thank you!
Use a variable to indicate whether any of the rows or columns didn't match. Then check it at the end of the loop.
Here's how to do it for rows. Columns are similar, just switch the order of the i and j loops.
int all_matched = 1;
for (int i = 0; i < m; i++) {
int sumar = 0;
for (int j = 0; j < n; j++) {
sumar += mat[i][j];
}
if (sumar != sumarp) {
all_matched = 0;
break;
}
}
if (all_matched) {
printf("EQ\n");
} else {
printf("NE\n");
}
I need to delete (not skip while printing) the rows and columns of a matrix that appear more than once in program, and I should print only first row from the top that appears more than once or the first column from the left that appears more than once.
Example input:
1 2 3 2
4 5 6 5
1 2 3 2
7 8 9 8
After deleting:
1 2 3
4 5 6
7 8 9
Here's my code:
#include <stdio.h>
int main() {
int i, j, m, n,row,col, mat[200][200];
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
scanf("%d", &mat[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < m; j++) {
if (mat[i][j] == mat[i++][j++])
row--;
if (mat[j][i] == mat[j++][i++])
col--;
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Do you have any idea how to make the algorithm work for this task? Mine has mistakes.
Would you please try the following:
#include <stdio.h>
#define ROWS 200
#define COLS 200
#define TRUE 1
#define FALSE 0
/*
* delete k'th row from the m x n matrix
*/
void deleterow(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (i = k; i < m - 1; i++) {
for (j = 0; j < n; j++) {
mat[i][j] = mat[i + 1][j];
}
}
}
/*
* delete k'th columns from the m x n matrix
*/
void deletecol(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (j = k; j < n - 1; j++) {
for (i = 0; i < m; i++) {
mat[i][j] = mat[i][j + 1];
}
}
}
int main() {
int i, j, m, n,row,col, mat[ROWS][COLS];
int iref, jref; // reference indexes to compare
int match; // flag to show if the row/col duplicates
// read input matrix
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &mat[i][j]);
// examine row by row
for (iref = 0; iref < m; iref++) {
// compare rows below iref and remove the row if duplicates
for (i = iref + 1; i < m; i++) {
match = TRUE;
for (j = 0; j < n; j++) {
if (mat[i][j] != mat[iref][j]) {
match = FALSE;
break;
}
}
if (match) {
deleterow(mat, m, n, i);
m--;
}
}
}
// examine column by column
for (jref = 0; jref < n; jref++) {
// compare columns more right than jref and remove the col if duplicates
for (j = jref + 1; j < n; j++) {
match = TRUE;
for (i = 0; i < m; i++) {
if (mat[i][j] != mat[i][jref]) {
match = FALSE;
break;
}
}
if (match) {
deletecol(mat, m, n, j);
n--;
}
}
}
// see the result
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%2d%s", mat[i][j], j == n - 1 ? "\n" : " ");
}
}
return 0;
}
Output with the provided example:
1 2 3
4 5 6
7 8 9
[Explanation]
As for the operations of rows:
First, focus on the top row as a "reference". The row is indexed by
the variable iref which is assigned to 0 at first.
Then compare the remaining rows with the reference, changing
the row index i from iref+1 (just below the reference row) to n-1
(the bottom row).
If a row duplicates with the reference, remove the row with the
deleterow() function and decrement the row size m by one.
The modification of m affects the for loops which compare the
loop variables with m, meaning the matrix size is updated immediately.
This is a preferable nature of the for loop (IMHO).
If the comparizon reaches the bottom row, increment iref and repeat
the comparisons again.
Finally every row has been compared to each other and the duplicates have
been deleted.
Then perform the similar operations with columns.
well, i need to store the fibonacci sequence in a 2x5 matrix but what im doing its not working. here's my attempt
int main()
{
int i,j,k;
int mat[2][5];
mat[0][0]=0;
mat[0][1]=1;
k=2;
for (i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
mat[i][k]=mat[i][j]+mat[i][j+1];
k++;
}
}
for(i=0; i<2; i++)
{
for(j=0; j<5; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
return 0;
}
How about using the row and column of the matrix to calculate the "position" of that element at the fibonacci sequence?
#include <stdio.h>
#define ROWS 2
#define COLS 5
int fibonacci(int position)
{
if (position == 0)
return 0;
if (position == 1)
return 1;
return fibonacci(position - 1) + fibonacci(position - 2);
}
int main()
{
int mat[ROWS][COLS];
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
/*
[0,0] = 0 [0,1] = 1, [0,2] = 2, [0,3] = 3, [0,4] = 4
[1,0] = 5 [1,1] = 6, [1,2] = 7, [1,3] = 8, [1,4] = 9
*/
mat[row][col] = fibonacci(COLS * row + col);
printf("%3d, ", mat[row][col]);
}
printf("\n");
}
return 0;
}
Would you please try the following:
#include <stdio.h>
int main()
{
int i, j, k;
int mat[2][5] = {{0, 1}};
for (k = 2; k < 10; k++) {
mat[k / 5][k % 5] = mat[(k - 1) / 5][(k - 1) % 5] + mat[(k - 2) / 5][(k - 2) % 5];
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
This exercise asks me to fill the first row of the matrix then put the last element of each row in the first box of the next row and transfers the elements of the previous row to the next row like the following example:
5 7 9 2
2 5 7 9
9 2 5 7
7 9 2 5
5 7 9 2
The problem is start from the second row to the end in my code(the program does not return the last value of row 1 to the row 2) , can you help me
my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows=5,cols=4;
int T[rows][cols];
int p=1;
printf("The filling of this matrix :\n");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(i==0)
{
scanf("%d",&T[i][j]);
}
T[p][0]=T[p-1][cols-1];
for(int k=1;k<rows;k++)
{
T[k][j+1]=T[k-1][j];
}
}
p++;
}
printf("\nThe display of this matrix :\n");
for(int i=0;i<rows;i++)
{printf("\n");
for(int j=0;j<cols;j++)
{
printf("[%d]",T[i][j]);
}
}
}
#include <stdio.h>
int main(void){
int rows = 5, cols = 4;
int T[rows][cols];
printf("Filling the first row\n");
for(int i=0; i < cols; i++){
printf("Enter T[0][%d] = ",i)
scanf("%d",&T[0][i]);
}
printf("Filling the rest matrix\n");
for(i=1; i < rows; i++){
for(j=0; j < cols; j++){
if(j == 0){
T[i][j] = T[i-1][cols-1];
}
else{
T[i][j] = T[i-1][j-1];
}
}
}
printf("display the matrix\n");
for(i=0 ; i < rows; i++){
for(j=0 ; j < cols; j++){
printf(" %d ",T[i][j]);
}
printf("\n")
}
}
You are accessing outside of the bounds of the array here:
T[k][j+1]=T[k-1][j]; // j + 1 = 4 in the last iteration in a range [0 ... 3]
You are over-complicating things, you don't need an extra loop (k) nor an extra variable (p), all the information needed is already in the inner loop, just check the position of i and j:
#include <stdio.h>
int main(void)
{
int rows = 5, cols = 4;
int T[rows][cols]; // Notice that this is a VLA, avoid using
// them and use enum {rows = 5, cols = 4}
// when values are known before hand
printf("The filling of this matrix :\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (i == 0)
{
scanf("%d", &T[i][j]);
}
else
{
if (j == 0)
{
T[i][j] = T[i - 1][cols -1];
}
else
{
T[i][j] = T[i - 1][j - 1];
}
}
}
}
printf("\nThe display of this matrix :\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("[%d]", T[i][j]);
}
printf("\n");
}
}
You can simplify:
if (j == 0)
{
T[i][j] = T[i - 1][cols -1];
}
else
{
T[i][j] = T[i - 1][j - 1];
}
using a ternary operator:
T[i][j] = T[i - 1][(j == 0) ? cols - 1 : j - 1];
I have a program that is getting a segmentation fault. The program is a magic square program and asks users for the size of a square (a matrix) and then for the numbers of the square by row. I am using a pointer to point to the array in the declareArray function to reference where it is declared in the main function. I want to keep the pointer in order to practice using them, even though I know I can make the program work without pointers. I think the pointer is the problem here but I cant find what I am doing wrong with it.
here is the code:
int main(void)
{
int *arr[SIZE][SIZE] = "\0";
declareArray(&arr);
declareArray();
return 0;
}
//main logic
int declareArray(int *arr[SIZE][SIZE])
{
//variables
int rowNumber = 0;
int dimension = 0;
//int arr[SIZE][SIZE] = {0};
int row, col;
int sum, sum1, sum2;
int flag = 0;
//ask for input of squares
printf("Please enter the dimension of the square: ");
//makes sure the size is between 1 and 15 for the dimension of the square
if(scanf("%d", &dimension) != 1 || dimension >= 15 || dimension < 1)
{
printf("invalid input\n");
return 1;
}
//enter the data
//array rows
for(row = 0; row < dimension; ++row)
{
printf("Please enter the data for row %d: ", ++rowNumber);
//array columns
for(col = 0; col < dimension; ++col)
{
//store the user input
scanf("%2d", &*arr[row][col]);
}
}
printf("\n");
printf("Here is the square");
printf("\n");
//print the square
//array rows
for(row = 0; row < dimension; ++row)
{
//array columns
for(col = 0; col < dimension; ++col)
{
printf("%d", *arr[row][col]);
}
printf("\n");
}
//Checks Sum of diagonal elements
sum = 0;
for (row = 0; row < dimension; row++) {
for (col = 0; col < dimension; col++) {
if (row == col)
sum = sum + *arr[row][col];
}
}
//Checks Sum of Rows
for (row = 0; row < dimension; row++) {
sum1 = 0;
for (col = 0; col < dimension; col++) {
sum1 = sum1 + *arr[row][col];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//Checks sum of Columns
for (row = 0; row < dimension; row++) {
sum2 = 0;
for (col = 0; col < dimension; col++) {
sum2 = sum2 + *arr[col][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
//if the sums match, it will print a success message with the constant
//if the sums dont match, a fail message will appear
if (flag == 1)
printf("\nTHE SQUARE IS A MAGIC SQUARE WITH A CONSTANT OF %d \n", sum);
else
printf("\nThe Square is not a magic square \n");
return 0;
}
I can see few issues here
int *arr[SIZE][SIZE] = "\0"; // this wont compile
declareArray(&arr); // needless
declareArray();
---
int arr[SIZE][SIZE] = {};
declareArray(arr);
//declareArray();
Declare function
int declareArray(int *arr[SIZE][SIZE]) // because function call changed
---
int declareArray(int arr[SIZE][SIZE])
finally in printf and scanf remove the * operator not needed anymore
as in
scanf("%2d", &*arr[row][col]); // remove *
---
scanf("%2d", &arr[row][col]);
printf("%d", *arr[row][col]); // remove *
---
printf("%d", arr[row][col]);
sum = sum + *arr[row][col]; // remove *
---
sum = sum + arr[row][col];
Note that when you declare an array, the name of the array is a pointer to the first element of the array:
&arr[0] == arr.
The argument passed to the declareArray function is an array of pointers to integers, therefore the space needed for the pointers was allocated yet the space for the actual ints was not, so when you are trying to scan an integer to the address pointed to by arr[row][col], you are trying to write to the address that it holds, 0 in your case , and address 0 is most likely out of the data segment, hence the segment_fault.
What should you do then?
Allocate the space needed with malloc(), assign the address returned to arr[row][col] and then scanf() as shown below, or alternatively, and quite simpler and better use an int array and simply assign the integer to arr[row][col] as shown in the answer above
#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
int declareArray(int * arr[SIZE][SIZE]);
int main(void)
{
int * arr[SIZE][SIZE] = {0};
declareArray(arr);
return 0;
}
//main logic
int declareArray(int * arr[SIZE][SIZE])
{
//variables
int rowNumber = 0;
int dimension = 0;
int row, col;
int sum, sum1, sum2;
int flag = 0;
int * myVal;
//ask for input of squares
printf("Please enter the dimension of the square: ");
//makes sure the size is between 1 and 15 for the dimension of the square
if(scanf("%d", &dimension) != 1 || dimension >= 15 || dimension < 1)
{
printf("invalid input\n");
return 1;
}
//enter the data
//array rows
for(row = 0; row < dimension; ++row)
{
printf("Please enter the data for row %d: ", ++rowNumber);
//array columns
for(col = 0; col < dimension; ++col)
{
printf("insert data to row %d col %d: ", rowNumber, col+1);
arr[row][col] = (int *) malloc(sizeof(int));
scanf("%2d", arr[row][col] );
}
}
printf("\n");
printf("Here is the square");
printf("\n");
//print the square
//array rows
for(row = 0; row < dimension; ++row)
{
//array columns
for(col = 0; col < dimension; ++col)
{
printf("%d", *arr[row][col]);
}
printf("\n");
}
//Checks Sum of diagonal elements
sum = 0;
for (row = 0; row < dimension; row++) {
for (col = 0; col < dimension; col++) {
if (row == col)
sum = sum + *arr[row][col];
}
}
//Checks Sum of Rows
for (row = 0; row < dimension; row++) {
sum1 = 0;
for (col = 0; col < dimension; col++) {
sum1 = sum1 + *arr[row][col];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//Checks sum of Columns
for (row = 0; row < dimension; row++) {
sum2 = 0;
for (col = 0; col < dimension; col++) {
sum2 = sum2 + *arr[col][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
//if the sums match, it will print a success message with the constant
//if the sums dont match, a fail message will appear
if (flag == 1)
printf("\nTHE SQUARE IS A MAGIC SQUARE WITH A CONSTANT OF %d \n", sum);
else
printf("\nThe Square is not a magic square \n");
return 0;
}