My task is to print out a matrix of integer numbers scanned from the keyboard, and then to print out the numbers under the second diagonal using separate functions. My code looks like this:
#include <stdio.h>
#include <stdlib.h>
//array[n][n] = 2D array of n rows and n columns
//n = number of rows and columns
void printmatrix(int n, int array[n][n]){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i][j]);
}
printf("\n");
}
return;
}
void undersdiagonal(int n, int array[n][n]){
for(int i = 1; i < n; i++){
/**/
for(int j = 1; (j < n); j++){
printf("%d ", array[i][j]);
}
}
return;
}
int main(){
int n;
scanf("%d", &n);
int integermatrix[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &integermatrix[i][j]);
}
}
printf("The entered matrix is:\n");
//calls first function to print matrix
printmatrix(n, integermatrix);
printf("Under the secondary diagonal:\n");
//calls second function to print numbers under main diagonal
undersdiagonal(n,integermatrix);
printf("\n");
return 0;
}
I can't figure out how to print out the numbers under the second main diagonal from the function
void undersdiagonal(int n, int array[n][n]).
My input looks like this:
3
1
2
3
4
5
6
7
8
9
And the output looks like this:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
5 6 8 9
But I want the output to be:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
6 8 9
What could I change in the 'for' loop within the 'for' loop in the function
void undersdiagonal(int n, int array[n][n])
?
The key is with the inner loop of undersdiagonal:
As currently written, for(int j = 1; (j < n); j++){ starts with the second column (column 1) every single time. It needs to start on a different column for each row.
For a 3×3 matrix, the second row (row 1) will begin on the last column (column 2). For a 4×4 matrix, the second row (row 1) will begin on the last column (column 3)... So there is a direct relationship between the starting column (j) and the matrix size: n.
Every time the row number goes up one, the starting column goes down one. So there is an inverse relationship between the starting column (j) and the row: - i.
Take those two pieces together and you have int j = n - i;
Related
I'm trying to check if the inputted array of the user is mirrored or not
Users input:
//for the rows and column
3 3
//elements inside array
1 0 1
2 0 3
4 0 4
the output should tell if the first index and the last index of the array is the same, but the only thing that i have done is mirror it and print mirror even if its not the same
int main()
{
int arr[10][10];
int col,row;
scanf("%d %d", &col, &row);
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
scanf("%d", &arr[i][j]);
}
}
for (int i = 0; i < col; i++) {
for(int j = row - 1; j >= 0; j--) {
printf("Mirror");
}
}
}
the size of the array and the elements depends on the user, thank youuu
This is my solution for your problem, this code can have some isuess but in general do what you ask for. Adapt for your particular needs.
OUTPUT:
This program will check if an array is mirrored. Press enter to proceed!
Insert row*col -> 3*3
Insert row 1 elements, one by one!
Element 1 -> 1
Element 2 -> 0
Element 3 -> 1
Insert row 2 elements, one by one!
Element 1 -> 2
Element 2 -> 0
Element 3 -> 3
Insert row 3 elements, one by one!
Element 1 -> 4
Element 2 -> 0
Element 3 -> 4
Mirror found at row 1!
1 0 1
Mirror found at row 3!
4 0 4
CODE:
#include <stdio.h>
#define MAXCOLUMN 10
#define MAXROW 10
void clean(){
int temp;
while ((temp = getchar()) != '\n' && temp != EOF);
}
void print(int dim,int r, int row[][MAXCOLUMN]){
for(int index = 0; index < dim; index++)
printf("%d ",row[r][index]);
putchar('\n');
}
int main(void){
int arr[MAXROW][MAXCOLUMN];
int col,row,mirror;
puts("This program will check if an array is mirrored. Press enter to proceed!");
do{
clean();
printf("Insert row*col -> ");
}
while(scanf("%d*%d", &row, &col)!=2);
for(int r = 0; r < row; r++){
printf("Insert row %d elements, one by one!\n",r+1);
for(int c = 0; c < col; c++){
do{
clean();
printf("Element %d -> ", c+1);
}while(!scanf("%d",&arr[r][c]));
}
}
for(int r = 0; r < row; r++){
mirror = 1;
for(int cFH = 0, cSH = col-1; cFH < col/2; cFH++, cSH--)//cFH -> column index first half | cSH -> column index second half
if(arr[r][cFH] != arr[r][cSH]){
mirror = 0;
break;
}
if(mirror){
printf("Mirror found at row %d!\n",r+1);
print(col,r,arr);
}
}
return 0;
}
Usually you iterate through the columns of each row, that means that usually i (the outer loop) represents the rows and j the columns.
This piece of code shows a way to check if a 2D array is mirrored horizontally:
int mirror = 1;
for(int i = 0; i < row; i++) {
for(int j = 0; j < col/2; j++) {
// comparing first element with last element of row `i`
// than comparing the second element with the second last
// and so on, till you reach the center of the row, `j==col/2`
// if the comparison is true just one time, the array is not mirrored
if( array[i][j] != array[i][col-j-1] ) {
mirror = 0;
i = row; // to break the outer loop
break;
}
}
}
mirror ? puts("Mirror!") : puts("No mirror.");
At the end of the program if the variable mirror still have a value of 1, you got a mirror array.
Hi I have problem with initializing my function for printing an error message if some numbers in an array are same.
#include<stdio.h>
#include<stdlib.h>
void printRepeating(int arr[], int size)
{
int i, j;
for(i = 0; i < size; i++)
for(j = i+1; j < size; j++)
if(arr[i] == arr[j])
printf("Wrong input. Same numbers in array!\n");
}
int main()
{
int arr[200],i;
int res, num;
while((res = scanf("%d", &num)) == 1)
{
arr[i++] = num;
if(num == 0){
break;
}
}
for(i = 0; i < arr[i]; i++)
printf("%d ", arr[i]);
printf("\n");
int arr_size = sizeof(arr[i])/sizeof(arr[0]);
printRepeating(arr, arr_size);
return 0;
}
If I scan 1 2 3 1 4 5 0, my function printRepeating wont start nevertheless I have numbers 1 1 that are same in the array, Why ? And another problem is when I type 1 2 3 1 5 0 it only prints 1 2 3 and for example I when I scan 1 2 3 4 5 6 7 8 9 0 it prints all numbers except for 0.
There are multiple issues in your code. First, initialize i to be 0, and declare a new variable j alongside i,
int arr[200], i = 0, j;
The size of your array would simply be i, which you increment every time you insert an element in the array, and change this,
for(i = 0; i < arr[i]; i++)
printf("%d ", arr[i]);
to this
for(j = 0; j < i; j++)
printf("%d ", arr[j]);
since the size of your array is stored in variable i. Also, the way you are calculating the size of your array is wrong which returns 1 everytime since the numerator and denominator are the same. Generally, it is sizeof(array)/sizeof(array[0]), and in this case, it too, would return 200, since the size of your declared array is 200, but since you increment your i every time at insertion, simply set your arr_size to be i.
int arr_size = i;
This line
int arr_size = sizeof(arr[i])/sizeof(arr[0]);
does not do what you are expecting. You are only dividing the size of two elements of the array, which are always the same size, thus always giving 1 as result. If you want to give the number of elements to the function, give it a variable that you used to count each number as you read them.
Also this:
for(i = 0; i < arr[i]; i++)
printf("%d ", arr[i]);
Is only printing numbers that are not larger than their indices. Again, your program is missing a variable to count the number of input values.
Below is the program I tried.
I get the 2D array and print that array just after the all elements are scanned
like below
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
int R, C;
scanf("%d %d", &R, &C);
int i[C][R];
for (int row = 0; row < R; row++) {
for (int column = 0; column < C; column++) {
int val = 0;
scanf("%d", &val);
i[row][column] = val;
}
}
for (int row = 0; row < R; row++) {
for (int column = 0; column < C; column++) {
printf("%d \t", i[row][column]);
}
printf("\n");
}
printf("RotatedMatrix\n");
return EXIT_SUCCESS;
}
I give the input as 3,4 indicating 3 rows and 4 columns so totally 12 elements
I give the input from 1 to 12 for matrix
But the output matrix looks like below
1 2 3 5
5 6 7 9
9 10 11 12
Instead of
1 2 3 4
5 6 7 8
9 10 11 12
The elements on last column is changed like above
I can't figure out what I am missing. It would be helpful if I get any help
It should be
int i[R][C];
instead of
int i[C][R];
I'm currently learning C programming, to better my understanding of matrices in C I've tried to make this program.
I seem to be having problems with the output, as you can see the program has 3 functions.
The first one allows you to input the values for the array and then displays it. The second function performs the multiplication and the last should display the output of the multiplied matrix.
However the output is strange. Here is my code. The output is just below the code.
#include <stdio.h>
void read_matrix(int m2[][3] )
{
int i, j;
printf("input values for matrix in order of rows first \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d",&m2[i][j]);
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", m2[i][j]);
}
printf("\n");
}
}
void multiply_matrices(int m1[][3], int m2[][3] ,int m3[][3])
{
int i, j, k;
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
for (k = 0; k < 3; k++){
m3[i][j] +=m1[i][k]*m2[k][j];
}
}
}
}
void write_matrix(int m3[][3] )
{
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", m3[i][j]);
}
printf("\n");
}
}
int main(void)
{
int matrix1[3][3], matrix2[3][3], matrix3[3][3];
read_matrix(matrix1);
read_matrix(matrix2);
multiply_matrices(matrix1, matrix2, matrix3);
write_matrix(matrix3);
return 0;
}
and this is the output!
input values for matrix in order of rows first
1
2
3
2
2
2
1
2
2
1 2 3
2 2 2
1 2 2
input values for matrix in order of rows first
2
1
1
1
2
1
2
1
2
2 1 1
1 2 1
2 1 2
-858993450 -858993452 -858993451 /*This is the multiplied matrix output!*/
-858993450 -858993452 -858993452
-858993452 -858993453 -858993453
Press any key to continue . . .
I fear it may be just a silly mistake; if so I'm sorry, but I can't see where I am going wrong at this moment.
Any help would be greatly appreciated.
You need to initialize all elements of matrix m3 to 0 before performing this operation
m3[i][j] +=m1[i][k]*m2[k][j];
in function multiply_matrices.
Initialize matrix3 in the function multiply matrix like this
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
m3[i][j]=0;
}
}
After this, do the multiplication and everything will work perfectly.
int m3[][]={};
It initially stores 0 for all available index of m3
I'm trying to make a matrix algebra program, so I began with a pre defined 3x3 matrix. Ran into some issues with the elements not being read properly into the 2D array so I isolated one . The issue is that the last number in a row is printed as the first element in the next row. i.e. array[1][2] is printed out as what array [2][0] is.
Again, if I have a 3x3 matrix where we go from 1 to 9, and I want the value at row 1 column 3 (which is 3), it'll give me 4. Same goes for row 2 column 3; the value is 6 but it gives me 7.
I've put in a print statement to show me the counter values before input and they're correct. Something is going on with the scanf I guess but I'm really stumped. Any help would be appreciated!
#include <stdio.h>
int matrix1[2][2];
void main(void)
{
int i = 0, j=0; //Row and column subscripts for the first matrix
//======================================================================================================//
//First Matrix
printf("This is for the first matrix");
for(i=0; i < 3; i++) //Start with row
{
for(j = 0; j < 3; j++)
{
printf("\nInsert the value at row %d, column %d: ", i+1,j+1); //+1 since array's begin at 0
printf("\ni is %d, j is %d", i,j); //Shows what elements the input will go into
scanf("%d", &matrix1[i][j]); //Enter value into row i column j
}
}
printf("\n\n%d", matrix1[1][2]); //test to see what's in the element
}
#include <stdio.h>
#define ROWS 3
#define COLS 3
int matrix1[ROWS][COLS];
void main(void)
{
int i, j; //Row and column subscripts for the first matrix
//================================================================================
//First Matrix
printf("This is for the first matrix");
for(i=0; i < ROWS; i++) //Start with row
{
for(j = 0; j < COLS; j++)
{
printf("\nInsert the value at row %d, column %d: ", i+1,j+1);
printf("\ni is %d, j is %d", i,j);
scanf("%d", &matrix1[i][j]);
}
}
printf("\n\n%d", matrix1[1][2]); //test to see what's in the element
}
I think You are confused with array index numbers and total number of elements in array.
For Example When You have array of 5 integers.
int ary[5] = {11,22,33,44,55};
`------------IN Memory-----------
| 11 || 22 || 33 | | 44 | | 55 |
------------------------------- `
0 1 2 3 4 <--------These are called Index numbers.
They always start with 0,Hence
they always end with(Total Number of elements - 1)
Hence From above explanation you either want to change the size of array
from int matrix[2][2] to int matrix[3][3];
OR
either change your for() loops to
for(i=0; i < 2; i++) //changed i<3 to i<2
{
for(j = 0; j < 2; j++) //changed i<3 to i<2
{
// your Code.....
}
}