`
//MATRIX INPUT
#include<stdio.h>
int main(void)
{
//declaring variables
size_t row=0;
size_t column=0;
int limit=0;
int en=0;
int mat[row][column];
//starting
printf("ENTRE THE NUMBER OF R
EPETITIONS OF MATRIX
INPUT:");
scanf("%d",&limit);
//starting loop
for(int i=0;i<=(limit-1);++i)
{
printf("\nINITIALIZED THE
MATRIX-%d...\n",i+1);
printf("\nENTER ROW
NUMBER FOR MATRIX:\n");
scanf("%d",&row);
printf("\nENTER COLUMN
NUMBER FOR MATRIX:\n");
scanf("%d",&column);
//entering the entries..
for(int j=0; j<row ;++j)
{
for(int k=0;
k<column;++k)
{
printf("ENTRY (%d,%d):",(j+1),(k+1));
scanf("%d",&mat[k][j]);
}
}
}
//starting the console output
of matrix
int v=0;
printf("Which matrix do you
want to see?\n");
scanf("%d",&v);
for(v=0; v<=limit; ++v)
{
for(int l=0; l<row; ++l)
{
for( int m=0; m<column;
++m)
{
printf("%2d", mat[m][l]);
}
}
}
return 0;
}`
I have written a program in C where the user can firstly define the number of matrices. Then, the dimension of individual matrix is defined. I have successfully proceeded to individual matrix input. But I am stuck in writing the operational codes like multiplying and adding matrices as well as to write code for showing output in matrix style.
How to fix this?
incomplete source code
Console of output
I couldn't get the part why you created an empty static array, and then changed its boundaries at least tried to change. However, I believe dynamic array allocation would serve your purpose better than static one. I implemented a 3D array that has the first index as matrices, the second index as rows of the current matrix, and the third index as columns of the current matrix. Also, fixed the display method, you have been using. However, there can be different-sized matrices for each matrix you have. Therefore, I implemented another list to keep track of the rows and columns. Lastly, I put two links at the top of the code that you can implement addition and multiplication operations by yourself. You can see the code from here:
//MATRIX INPUT
#include<stdio.h>
#include<stdlib.h>
//Add two matrices
//https://www.programmingsimplified.com/c-program-add-matrices
//Multiplying two matrices
//https://www.programiz.com/c-programming/examples/matrix-multiplication
void display_matrix(int** , int , int );
void fillWithNum(int**,int,int,int);
int main(void)
{
//declaring variables
size_t row=0;
size_t column=0;
int limit=0;
int en=0;
int ***mat;
//starting
printf("ENTRE THE NUMBER OF REPETITIONS OF MATRIX INPUT:");
scanf("%d",&limit);
mat = (int***)malloc(sizeof(int**) * limit);
//Two keep track of the rows and columns
//First index of the second dimension will be rows, and
//second index will be columns of matrices at the mat variable.
int **indexingList;
indexingList = (int**)malloc(sizeof(int*) * limit);
for(int i=0;i<limit;++i) {
indexingList = (int*) malloc(sizeof(int) * 2);
}
//starting loop
for(int i=0;i<limit;i++)
{
printf("\nINITIALIZED THE MATRIX-%d...\n",i+1);
printf("\nENTER ROW NUMBER FOR MATRIX:\n");
scanf("%d",&row);
printf("\nENTER COLUMN NUMBER FOR MATRIX:\n");
scanf("%d",&column);
mat[i] = (int**) malloc(row * sizeof(int*));
for (int j = 0;j < row;j++) {
mat[i][j] = (int*)malloc(column * sizeof(int));
}
//entering the entries..
for(int j=0;j < row;j++)
{
for(int k=0;k < column;k++)
{
printf("ENTRY (%d,%d):",(j+1),(k+1));
scanf("%d",&mat[i][j][k]);
}
}
indexingList[i][0] = row;
indexingList[i][1] = column;
}
//starting the console output of matrix
int v=1;
printf("Which matrix do you want to see?\n");
scanf("%d",&v);
//v-1 because if user want to display first matrix it should be 0, so user will enter 1
//therefore, 1-1 will give us 0 to display 0 index matrix
display_matrix(mat[v-1], indexingList[v-1][0], indexingList[v-1][1]);
//Problem at freeing memory for some purpose crash at here.
//I couldn't figure it out as well.
for(int i = 0;i < limit;i++) {
for(int j = 0;j < indexingList[i][0];j++) {
free(mat[i][j]);
}
free(mat[i]);
}
free(mat);
for(int i = 0;i < limit;i++) {
free(indexingList[i]);
}
free(indexingList);
return 0;
}
void display_matrix(int** matrix, int r, int c) {
for(int i = 0 ;i < r;i++){
for(int j = 0;j < c;j++) {
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
}
//Test purpose
void fillWithNum(int** arr,int r, int c, int num) {
for (int i = 0;i<r;i++) {
for(int j = 0;j<c;j++) {
arr[i][j] = num;
}
}
}
However, this code also crashes at the end of the memory-freeing part. I couldn't figure out why, but if someone finds it out as well, I would appreciate it.
Related
I am new to C coding, and am trying to implement standard matrix multiplication. My code works fine for square matrices, but refuses to accept a column vector. Here is my attempt at the code. Any help would be much appreciated.
//---------------------------------------IMPORTING NECESSARY C PACKAGES AND DEFINING EXECUTION CONSTANTS-------------------------------------------//
#include <stdio.h> // Standard input output library
#include <math.h> // Mathematical function library
#include <stdlib.h> // General purpose standard library
#define true 1
#define false 0
typedef long double numeric; // Using the long double datatype to avoid overflows during computations
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------FUNCTION DECLERATION-------------------------------------------------------------//
numeric **create_matrix(int x, int y); // To dynamically allocate memory and create a matrix
void input_matrix(numeric **matrix, int m, int n); // To accept a matrix
void print_matrix(numeric **l, int x, int y); // To print a matrix
numeric **standard_matrix_multiplication(int m, int n, int l); // To multiply two matrices
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------DRIVER CODE--------------------------------------------------------------------//
int main(int argc, char *argv[]) {
int m, n, l; int choice;
printf("Enter the matrix operation to be performed using the corresponding index number.\n");
printf("\n");
printf("1.\tMatrix Multiplication");
printf("\n");
scanf("%d", &choice);
switch(choice) {
case 1 :
printf("Enter the number of rows in the first matrix\n");
scanf("%d", &m);
printf("Enter the number of columns in the first matrix\n");
scanf("%d", &n);
printf("Enter the number of columns in the second matrix\n");
scanf("%d", &l);
printf("Enter both matrices.\n");
numeric **matrix_x;
matrix_x = create_matrix(m, l);
matrix_x = standard_matrix_multiplication(m, n, l);
print_matrix(matrix_x, m, l);
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------MATRIX MULTIPLICATION IMPLEMENTATIONS--------------------------------------------------//
numeric **standard_matrix_multiplication(int m, int n, int l) {
numeric **matrix_a; numeric **matrix_b; numeric **matrix_k;
matrix_a = create_matrix(m, n);
matrix_b = create_matrix(n, l);
matrix_k = create_matrix(m, l);
input_matrix(matrix_a, m, n);
print_matrix(matrix_a, m, n);
input_matrix(matrix_b, n, l);
for(int i = 0; i < m; i++) {
for (int j = 0; j < n; j ++) {
for (int k = 0; k < l; k++) {
matrix_k[i][j] += matrix_a[i][k] * matrix_b[k][j];
}
}
}
return matrix_k;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------HELPER FUNCTIONS------------------------------------------------------------------//
numeric **create_matrix(int x, int y) {
numeric **matrix = (numeric**)malloc(x * sizeof(numeric*)); // Dynamically creating an array of pointers
for (int i = 0; i < y; i++) {
matrix[i] = (numeric*)malloc(y * sizeof(numeric)); // Dynamically allocating memory for each columns of the matrix
}
return matrix;
}
void input_matrix(numeric **matrix, int m, int n) {
printf("Enter the elements of the matrix, row wise.\n"); // Instructing the user on matrix entry
printf("For example, to enter the matrix\n");
printf("\t\t1\t2\n");
printf("\t\t3\t4\n");
printf("enter 1, 2, 3, and 4 in that order.\n");
for (int i = 0; i < m; i++) { // Iterating through the rows and columns of the matrix
for (int j = 0; j < n; j++) {
scanf("%Lf", &matrix[i][j]); // Accepting each element
}
}
}
void print_matrix(numeric **l, int x, int y) { // To print a matrix
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
printf("%0.10Lf\t", l[i][j]); // Printing numeric type values
}
printf("\n");
}
printf("\n");
}
As of now, I have only written one switch case, and that is for matrix multiplication. So I chose 1. I gave 2, 1, 2 as my inputs for the number of rows in the first matrix, number of columns in the first matrix, and number of columns in the second matrix respectively. I have given a print statement in line 52, and it isn't executing it for the above input, giving a segmentation fault instead. Could someone please help me out?
Yes there are some issues with your code that gives segfault error during runtime.
The Matrix multiplication logic part of the code needs to be corrected as follows
for(int i = 0; i < m; i++) {
for (int j = 0; j < l; j ++) {
for (int k = 0; k < n; k++) {
matrix_k[i][j] += matrix_a[i][k] * matrix_b[k][j];
because k should iterate till the no of columns in 1st matrix which is n but not till l.
And there is a slight correction needed in create_matrix function.
You use y (i.e. no of columns in the matrix) in your for instead of x (no of rows in matrix).
If x < y, you end up outside of the memory you allocated.
If x > y, you end up with uninitialized pointers.
So change it as follows
for (int i = 0; i < x; i++) {
matrix[i] = (numeric*)malloc(y * sizeof(numeric));
After these corrections try executing the code, you should get the expected results without any segfault errors
Here is the complete working code
#include <stdio.h> // Standard input output library
#include <math.h> // Mathematical function library
#include <stdlib.h> // General purpose standard library
#define true 1
#define false 0
typedef long double numeric; // Using the long double datatype to avoid overflows during computations
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------------FUNCTION DECLERATION-------------------------------------------------------------//
numeric **create_matrix(int x, int y); // To dynamically allocate memory and create a matrix
void input_matrix(numeric **matrix, int m, int n); // To accept a matrix
void print_matrix(numeric **l, int x, int y); // To print a matrix
numeric **standard_matrix_multiplication(int m, int n, int l); // To multiply two matrices
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------DRIVER CODE--------------------------------------------------------------------//
int main(int argc, char *argv[]) {
int m, n, l; int choice;
printf("Enter the matrix operation to be performed using the corresponding index number.\n");
printf("\n");
printf("1.\tMatrix Multiplication");
printf("\n");
scanf("%d", &choice);
switch(choice) {
case 1 :
printf("Enter the number of rows in the first matrix\n");
scanf("%d", &m);
printf("Enter the number of columns in the first matrix\n");
scanf("%d", &n);
printf("Enter the number of columns in the second matrix\n");
scanf("%d", &l);
printf("Enter both matrices.\n");
numeric **matrix_x;
matrix_x = create_matrix(m, l);
matrix_x = standard_matrix_multiplication(m, n, l);
print_matrix(matrix_x, m, l);
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------------------MATRIX MULTIPLICATION IMPLEMENTATIONS--------------------------------------------------//
numeric **standard_matrix_multiplication(int m, int n, int l) {
numeric **matrix_a; numeric **matrix_b; numeric **matrix_k;
matrix_a = create_matrix(m, n);
matrix_b = create_matrix(n, l);
matrix_k = create_matrix(m, l);
input_matrix(matrix_a, m, n);
print_matrix(matrix_a, m, n);
input_matrix(matrix_b, n, l);
//print_matrix(matrix_b, n, l);
for(int i = 0; i < m; i++) {
for (int j = 0; j < l; j ++) {
for (int k = 0; k < n; k++) {
matrix_k[i][j] += matrix_a[i][k] * matrix_b[k][j];
}
}
}
return matrix_k;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------HELPER FUNCTIONS------------------------------------------------------------------//
numeric **create_matrix(int x, int y) {
numeric **matrix = (numeric**)malloc(x * sizeof(numeric*)); // Dynamically creating an array of pointers
for (int i = 0; i < x; i++) {
matrix[i] = (numeric*)malloc(y * sizeof(numeric)); // Dynamically allocating memory for each columns of the matrix
}
return matrix;
}
void input_matrix(numeric **matrix, int m, int n) {
printf("Enter the elements of the matrix, row wise.\n"); // Instructing the user on matrix entry
printf("For example, to enter the matrix\n");
printf("\t\t1\t2\n");
printf("\t\t3\t4\n");
printf("enter 1, 2, 3, and 4 in that order.\n");
for (int i = 0; i < m; i++) { // Iterating through the rows and columns of the matrix
for (int j = 0; j < n; j++) {
scanf("%Lf", &matrix[i][j]); // Accepting each element
}
}
}
void print_matrix(numeric **l, int x, int y) { // To print a matrix
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
printf("%0.10Lf\t", l[i][j]); // Printing numeric type values
}
printf("\n");
}
printf("\n");
}
You are not allocating enough memory for matrix_k. You are allocating memory for m rows and l columns, but you are trying to access m rows and n columns. You need to allocate memory for m rows and n columns. You can do this by changing the line
matrix_k = create_matrix(m, l);
to
matrix_k = create_matrix(m, n);
This is the same problem you had in your other question.
I have a problem with this program, the program must simply output the minimum value contained in a matrix, this must be done by the function that has as parameter the double pointer (**A) that would be the array , the problem is that after inserting the items , the program finishes and returns nothing.
(I don’t even appear written outuput instruction )
#include<stdio.h>
#include<stdlib.h>
int minium_matrix_value(int **A, int rows, int columns) {
int min=A[0][0];
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++)
if(A[i][j]<min)
min=A[i][j];
}
return min;
}
int main(){
int** A;
int i, j;
int result;
int sizer=100, sizec=100;
A = (int**)calloc(sizer,sizeof(int*));
A = (int**)calloc(sizec,sizeof(int*));
printf("insert rows size");
scanf("%d",&sizer);
printf("insert columns size");
scanf("%d",&sizec);
printf("insert matrix elements");
for( i=0; i < sizer; i++ )
for( j=0; j < sizec; j++ ) {
scanf("%d",((A+i)+j));
}
result=minium_matrix_value(A,sizer,sizec);
printf("the minium elements is: %d",result);
return 0;
}
You only allocated arrays to store pointers to rows. You have to allocate arrays for storing values of each rows.
Using the value of sizer should be after reading value into that/
// move this after scanf()
// A = (int**)calloc(sizer,sizeof(int*));
// remove this, or memory leak occur
// A = (int**)calloc(sizec,sizeof(int*));
printf("insert rows size");
scanf("%d",&sizer);
printf("insert columns size");
scanf("%d",&sizec);
// move here
A = calloc(sizer,sizeof(int*));
printf("insert matrix elements");
for( i=0; i < sizer; i++ ) { // add { to do multiple things
A[i] = calloc(sizec,sizeof(int)); // allocate row array
for( j=0; j < sizec; j++ ) {
//scanf("%d",((A+i)+j));
scanf("%d",&A[i][j]); // use array style, which should be easier to understand
}
} // add } to do multiple things
Also note that:
scanf("%d",((A+i)+j)); is wrong and it should be scanf("%d",(*(A+i)+j)); if you hate [] operator.
Casting result of malloc() family is discouraged in C.
So I'm trying to make a program to row reduce a matrix (there are probably a thousand way better ways to do it but I'm trying to figure it out on my own). I have a function that is supposed to make two rows of different arrays equal. It doesn't even take the row variable as an input or have anything to do with it but for some reason after the function is called the row variable switches from whatever was put into the keyboard to 1072693248. I also just noticed a problem with the col variable where it is set to 0 the second time the for loop it is in happens. I have no clue what is causing these problems so any help would be really appreciated. I'm just going to post the entire code since I'm not sure which part is causing the problem (its probably super messy, my only coding experience is one super basic beginner course in c in university). (I put in some printfs to print row or col in different spots to try and find out where the problem was happening).
#include <stdlib.h>
void printArray(double a[][100], int rows, int columns);
void addRow(double a[][100], int columns, int rowToAdd, int rowAddedTo);
void addRowBetweenArrays(double arrayAddedTo[][100],double arrayAdded[][100], int columns, int rowToAdd, int rowAddedTo);
void multiplyRow(double a[][100], int columns, int rowMultiplied, double mupltiplyBy);
void switchRows(double a[][100], int columns, int rowOne, int rowTwo);
//void makeRowsEqual(double rowMadeEqual[][100], double madeEqualTo[][100], int rowInMadeEqualTo, int RowInMadeEqual, int columns);
void makeTwoArraysRowsEqual(double arrayMadeEqual[][100], double arrayMadeEqualTo[][100], int rowMadeEqual, int rowMadeEqualTo, int column);
int main()
{
int row, col, i, j, checkReduced, rowIsReduced = 0, focus;
double multiplyFactor;
printf("Enter the size of your array (rows columns): ");
scanf("%d %d", &row, &col);
double array[row][100];
double tempRow[1][100];
for(j = 0; j < row; j++)
{
printf("Enter row %d: ", j + 1);
for(i = 0; i < col; i++)
{
scanf("%lf", &array[j][i]);
}
printf("\n");
}
printf("\n\nUnreduced array:\n");
printArray(array, row, col);
//start the reducing
for(focus = 0; focus < row; focus++)
{
rowIsReduced = 0;
for(i = 0; i < col; i++)
{
if(i == focus)
{
i++;
}
if(array[i][focus] != 0)
{
rowIsReduced = 1;
}
}
if(rowIsReduced == 1)
{
//now start the reducing process
for(i = 0; i < row; i++)
{
printf("\n%d\n", row);
if(i == focus)
{
i++;
}
//skip the row we are focusing on
//first check if we even need to manipulate the row
if(array[i][focus] != 0)
{
if(array[focus][focus] * array[i][focus] > 0)
{
printf("\n%d\n", row);
//then check if we need to add or subtract
//make temp row equal to focus row
printf("Column before function: %d", col);
makeTwoArraysRowsEqual(tempRow, array, 1, focus, col);
// to check order: makeTwoArraysRowsEqual(double arrayMadeEqual[][100], double arrayMadeEqualTo[][100], int rowMadeEqual, int rowMadeEqualTo, int column)
printf("\nAfter make rows equal: %d\n", row);
//now multiply both rows
multiplyFactor = array[i][focus];
multiplyRow(tempRow, col, 1, multiplyFactor);//getting stuck here
//multiplyRow(double a[][100], int columns, int rowMultiplied, double mupltiplyBy);
multiplyFactor = -1 * array[focus][focus];
multiplyRow(array, col, i, multiplyFactor);
//then add them
addRowBetweenArrays(array, tempRow, col, 1, i);
printf("Made it to end of the loop");
}
else //could also use: if(array[focus][focus] * array[i][focus] < 0)
{
//then check if we need to add or subtract
//make temp row equal to focus row
//makeRowsEqual(tempRow, array, focus, 1, col); need to switch this to new format
//now multiply both rows
multiplyRow(tempRow, col, 1, array[i][focus]);
multiplyRow(array, col, i, array[focus][focus]);
//then add them
addRowBetweenArrays(array, tempRow, col, 1, i);
}
//now the rows are added so there is a zero in the focus column
//will need to check if any rows are zero and move them to the bottom, have to figure that out
//will also need to divide all the rows by their first non zero term
}
printf("\n%d\n", row);//rows are acting weird, getting to be a really big number after the first run
}
printf("Made it out of i loop");
}
}
printf("\n\Reduced array:\n");
printArray(array, row, col);
return 0;
}
void printArray(double a[][100], int rows, int columns)
{
int i, j;
for(j = 0; j < rows; j++)
{
for(i = 0; i < columns; i++)
{
printf("%6.2lf ", a[j][i]);
}
printf("\n");
}
}
void addRow(double a[][100], int columns, int rowToAdd, int rowAddedTo)
{
int i;
for(i = 0; i < columns; i++)
{
a[rowAddedTo][i] += a[rowToAdd][i];
}
return 0;
}
void addRowBetweenArrays(double arrayAddedTo[][100],double arrayAdded[][100], int columns, int rowToAdd, int rowAddedTo)
{
int i;
for(i = 0; i < columns; i++)
{
arrayAddedTo[rowAddedTo][i] += arrayAdded[rowToAdd][i];
}
}
void multiplyRow(double a[][100], int columns, int rowMultiplied, double mupltiplyBy)
{
int i;
for(i = 0; i < columns; i++)
{
a[rowMultiplied][i] *= mupltiplyBy;
}
}
void switchRows(double a[][100], int columns, int rowOne, int rowTwo)
{
int i, temp;
for(i = 0; i < columns; i++)
{
temp = a[rowOne][i];
a[rowOne][i] = a[rowTwo][i];
a[rowTwo][i] = temp;
}
}
/*
void makeRowsEqual(double rowMadeEqual[][100], double madeEqualTo[][100], int rowInMadeEqualTo, int rowInMadeEqual, int columns)
{
int i;
printf("In function: made = to: %d, made =: %d", rowInMadeEqualTo, rowInMadeEqual);
for(i = 0; i < columns; i++) {
rowMadeEqual[rowInMadeEqual][i] = madeEqualTo[rowInMadeEqualTo][i];
}
printf("\nMade equal:\n");
printArray(rowMadeEqual, 1, columns);
printf("\nMade equal to:\n");
printArray(madeEqualTo, 2, columns);
}*/
void makeTwoArraysRowsEqual(double arrayMadeEqual[][100], double arrayMadeEqualTo[][100], int rowMadeEqual, int rowMadeEqualTo, int column)
{
int i;
for(i = 0; i < column; i++) {
arrayMadeEqual[rowMadeEqual][i] = arrayMadeEqualTo[rowMadeEqualTo][i];
}
printf("Column in function: %d", column);
}
Here is a sample output:
Enter the size of your array (rows columns): 2 2
Enter row 1: 1
2
Enter row 2: 2
4
Unreduced array:
1.00 2.00
2.00 4.00
2
2
Column before function: 2Column in function: 2
After make rows equal: 1072693248
Made it to end of the loop
1072693248
1072693248
1072693248
Column before function: 0Column in function: 0
After make rows equal: 1072693248
Made it to end of the loop
1072693248
1072693248
1072693248
Column before function: 0Column in function: 0
After make rows equal: 1072693248
Made it to end of the loop
1072693248
1072693248
Process returned -1073741819 (0xC0000005) execution time : 3.808 s
Press any key to continue.
P.S. Sorry if this question is formatted poorly or something, this is my first question and I've hardly used stack overflow as of right now.
The problem is this line:
makeTwoArraysRowsEqual(tempRow, array, 1, focus, col);
The third argument is used as the row index in tempRow to copy to. tempRow is declared:
double tempRow[1][100];
Since it only has 1 row, the highest row index is 0, but you're copying to row 1, which is outside the array, resulting in undefined behavior.
You should use row index 0:
makeTwoArraysRowsEqual(tempRow, array, 0, focus, col);
You have similar problems in the calls to other functions that use tempRow. They all pass 1 when it should be 0.
It's not clear why tempRow even needs to be 2-dimensional, since it only has one row. Just make it a 1-dimensional array, and remove the row index from all the functions that use it.
I'm supposed to multiply a certain line (I specify what line exactly in the 4th argument of the function) of a given matrix by a number.
The main function:
int main_tp05(int argc, const char *argv[]){
int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}};
multiply_matrixNx100_line_by_scalar(mNx100,3,3,1,2);
return 0;
}
I've tried to solve it like so:
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns, int line, int scalar){
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
}
}
printf("\n");
}
}
To note that:
1- I can´t change the parameters.
2- MAXCOLS100 is a macro on the .h file. I put it with the value of 3.
3- The scalar is the number I want to multiply the line by.
I WROTE THIS CODING AND VERIFIED BY MYSELF IT WORKS SUCCESSFULLY.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,a[5][5],b,c,d,m,n,r;
printf("Enter the number of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the number to multiply the matrix\n");
scanf("%d",&b);
printf("Enter 0 to multiply row or 1 to multiply column\n");
scanf("%d",&d);
if(d==0)
{
printf("Enter the row number to be multiplied\n");
scanf("%d",&r);
for(i=r,j=1;j<=n;j++)
{
a[i][j]*=b;
}
}
else if(d==1)
{
printf("Enter the row number to be multiplied\n");
scanf("%d",&c);
for(j=c,i=1;i<=m;i++)
{
a[i][j]*=b;
}
}
printf("matrix after multiplied\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT OF THIS CODING
I'm supposed to multiply a certain line (I specify what line exactly in the 4th argument of the function) of a given matrix by a number
Usually, "multiply a row of a matrix by a scalar" means to multiply every element of the row by a certain scalar value. That's not what the posted function does, it multiplies the index of the row by the passed argument:
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
for (int i = 0; i < lines; i++) { // <-- Useless, the row is known
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
// ^^^^^^^^
}
}
printf("\n");
}
}
If the intent is to only print the modified row, the previous function could be rewritten as
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
if (line < 0 || line >= lines)
return;
for (int j = 0; j < columns; j++) {
printf("%d\n", mNx100[line][j] * scalar);
}
printf("\n");
}
If instead the function is supposed to only modify the matrix, without printing it, we could use
mNx100[line][j] *= scalar;
Inside the loop, in place of the call to printf.
The program below is meant to print a square grid of integers based on the dimension which is inputted by the user (as part of the harvard cs50 course on edx).
The array is being initialized correctly, but when it comes to printing it out, the last column always prints incorrectly. I tried debugging by putting 2 printf statements in the innermost for loop of the init() function.
It seems that after the outermost loop runs once, the entry in the last column gets decremented by one, although it was correct just before this.
Why is this happening? Shouldn't it print correctly?
#include <stdio.h>
main()
{
void init(int dim, int arr[dim-1][dim-1]);
int dim;
printf("Enter board dimension(max 10): ");
scanf("%d", &dim);
int arr[dim-1][dim-1];
init(dim, arr);
int i,j;
for(i=0;i<dim;i++)
{
for(j=0;j<dim;j++)
{
printf("%2d ",arr[i][j]);
}
printf("\n");
}
}
void init(int dim, int arr[dim-1][dim-1])
{
int i,j,p;
for(i=0;i<dim;i++)
{
for(j=0;j<dim;j++)
{
arr[i][j] = (dim*dim-1)-i*dim-j;
}
for(p=0;p<dim;p++)
{
printf("%d ", arr[i][p]);
if(i>=1)
printf("%d ", arr[i-1][p]);
}
}
printf("\n");
if(dim%2==0)
{
arr[dim-1][dim-3] = 1;
arr[dim-1][dim-2] = 2;
}
}
EDIT: it should compile now
You define your array with a[dim - 1][dim - 1], which is one short of the desired dimension. If the user enters "4", you create a 3×3 array.
Your array is two-dimensional and of variable length. Therefore, you must pass the dimension alongside the array in functions, at least for the last dimension. You do this correctly, but the code in ´initbehaves as if the array werea[dim][dim], when it's actuallya[dim - 1][dim - 1]`.
Define the array with the actual dimension, dim and access the elements with indices 0 through dim - 1. This is usually done in a loop like this:
for (int i = 0; i < dim; i++) ...
Seeing >= or dim - 1 something similar should make you wary.
Your program now looks like this:
#include <stdio.h>
void init(int dim, int arr[dim-1][dim-1]);
void print(int dim, int arr[dim-1][dim-1]);
int main(void)
{
int dim;
printf("Enter board dimension: ");
scanf("%d", &dim);
int arr[dim][dim];
init(dim, arr);
print(dim, arr);
return 0;
}
void print(int dim, int arr[dim][dim])
{
int i,j;
for(i = 0; i < dim; i++) {
for(j = 0; j < dim; j++) {
printf("%2d ", arr[i][j]);
}
printf("\n");
}
}
void init(int dim, int arr[dim][dim])
{
int i, j;
for(i = 0; i < dim; i++) {
for(j = 0; j < dim; j++) {
int ii = dim - 1 - i;
int jj = dim - 1 - j;
arr[i][j] = ii*dim + jj;
}
}
}