how to print an array as a matrix in C - arrays

so I´m trying to print this 2d array as a matrix and it´s not working. any hints? no matter what i change i cant get to print an all 0 3x3 matrix
int main()
{
int i, j, m, n, primeira;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* first input */
printf("1 ou 0");
scanf("%d", &primeira);
if (primeira = 0) {
matrix [0][0]=0;
matrix [0][1]=0;
matrix [1][0]=0;
matrix [1][1]=0;}
/* Display the matrix */
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
return 0;
}

You would need to create a nested loop to display the matrix. If you want to display a 3x3 matrix you can run something like this.
int matrix[3][3] = { 0 };
/* Display the matrix */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf ("%d\t", matrix[i][j]);
}
printf ("\n");
}

regarding:
printf("%d\t", matrix[i][j]);
The variables: i and j are not initialized!
Suggest:
for( int i =0; i<10; i++ )
{
for( int j=0; j<20; j++ )
{
printf( "%d ", matrix[i][j] );
}
puts( "" );
}
as that will print all the elements of the matrix and move to a new line after printing each row of the matrix.

Related

How to properly fill an NxN matrix

I have been trying to create an NxN square matrix (the size of the square matrix is determined by the user) but seem to have run into an issue. The displayed matrix is actually a symmetric matrix which is not what I am trying to as I want to be able to have each value hold a unique integer. Any help is greatly appreciated. Here's my code:
#include <stdio.h>
int main()
{
int i;
int j;
int size = 1;
int array [size][size];
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array [i][j]);
}
}
printf ("------------------------\n\n");
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf (" %d ", array [i][j]);
}
printf ("\n");
}
return (0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Create variables
int i, j, size;
// Create 2D array
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
int *array = (int *)malloc(size * size * sizeof(int));
// Fill 2D array with values
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array[i * size + j]);
}
}
printf ("------------------------\n\n");
// Display values of 2D array
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("%d", array[i * size + j]);
}
printf ("\n");
}
free(array);
return (0);
}

Error in calculating resultant matrix in C programming language

I am trying to write a program where I have two matrices and I multiply the two matrices and store it in a resultant matrix named "carr." For some weird reason, the matrix multiplication is not getting executed properly. Tried to find the issue for quite a while but couldn't find the error. Can anyone help? TIA for your time!
Here is the ss of the issue: https://snipboard.io/s9ifP4.jpg
#include <stdio.h>
int main()
{
int row1, column1, row2, column2,i,j,k, sum=0;
//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);
printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];
printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
for(j=0; j < column1; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column1; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");
//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);
printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];
printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
for(j=0; j < column2; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//
//Everything above this part is okay. The problem starts from the Matrix multiplication part//
//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];
if(column1 == row2)
{
for(i = 0; i < row1; i++){
for(j=0; j < column2; j++){
for(k=0; k < row2; k++){
sum = sum + arr[i][k] * barr[k][j];
}
carr[i][j] = sum;
sum=0;
}
}
}
else
{
printf("Matrix multiplication is not possible");
}
printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", carr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
return 0;
}
Changing arr to barr fixes the issue. Thanks to #M Oehm for pointing out the error.

Ascending Order for 2-D Array, Not working..?

I am trying to output my 2D array into an Ascending order.
I have already output it into the order which the user put in. But can not get it to print to Ascending Order.
Could anybody possibly help with this or know a solution? My code is below for any question regarding code.
int main() {
/* 2D array declaration and size of each Array in the Programme*/
int Array[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &Array[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , Array[i][j]);
if(j==2)
{
printf("\n");
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < 2; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
return 0;
}
First Use bubble sort algorithm
for(int i=0; i<size; ++i)
{
for(int j=i+1; j<size; ++j)
{
if(a[i]>a[j]))
{
int temp=a[i];
a[i]=array[j];
a[j]=temp;
}
}
}
Then print your array in an ascending order.
Try to apply your own way, you can use some features of arrays like arrays contiguity.
int *ptr=a;
same with
*(ptr+i), *(ptr+j)
To use a simple one dimensional bubble sort algorithm to sort 2D array you have to add another loop taking care of the rows.
In your case:
for(k =0; k< 2; k++) {
for (i = 0; i < 3; i++) {
for (j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
The program:
#include <stdio.h>
int main(void) {
/* 2D array declaration and size of each Array in the Programme*/
int a[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j, k;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
// SORT:
for( k = 0; k< 2; k++) {
for ( i = 0; i < 3; i++) {
for ( j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
printf("\n\nAscending : ");
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
return 0;
}
Output:
***** Bubble Sort Assessment 2 *****
Enter numeric values for each Array [0][0]:
1
Enter numeric values for each Array [0][1]:
0
Enter numeric values for each Array [0][2]:
2
Enter numeric values for each Array [1][0]:
5
Enter numeric values for each Array [1][1]:
4
Enter numeric values for each Array [1][2]:
3
The 2-D Array contains :
1 0 2
5 4 3
Ascending :
The 2-D Array contains :
0 1 2
3 4 5

How to delete duplicated values in array in C?

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

Create a basic matrix in C (input by user !)

I'm trying to ask the user to enter the number of columns and rows they want in a matrix, and then enter the values in the matrix... I'm going to let them insert numbers one row at a time.
How can I create such function ?
#include<stdio.h>
main(){
int mat[10][10],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
scanf("%d",&mat[i][j]);
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d",mat[i][j]);
}
This works for entering the numbers, but it displays them all in one line... The issue here is that I don't know how many columns or rows the user wants, so I cant print out %d %d %d in a matrix form...
Any thoughts?
Thanks :)
How about the following?
First ask the user for the number of rows and columns, store that in say, nrows and ncols (i.e. scanf("%d", &nrows);) and then allocate memory for a 2D array of size nrows x ncols. Thus you can have a matrix of a size specified by the user, and not fixed at some dimension you've hardcoded!
Then store the elements with for(i = 0;i < nrows; ++i) ... and display the elements in the same way except you throw in newlines after every row, i.e.
for(i = 0; i < nrows; ++i)
{
for(j = 0; j < ncols ; ++j)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
You need to dynamically allocate your matrix. For instance:
int* mat;
int dimx,dimy;
scanf("%d", &dimx);
scanf("%d", &dimy);
mat = malloc(dimx * dimy * sizeof(int));
This creates a linear array which can hold the matrix. At this point you can decide whether you want to access it column or row first. I would suggest making a quick macro which calculates the correct offset in the matrix.
need a
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d",mat[i][j]);
}
printf("\n");
}
#include<stdio.h>
int main(void)
{
int mat[10][10],i,j;
printf("Enter your matrix\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
scanf("%d",&mat[i][j]);
}
printf("\nHere is your matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
This is my answer
#include<stdio.h>
int main()
{int mat[100][100];
int row,column,i,j;
printf("enter how many row and colmn you want:\n \n");
scanf("%d",&row);
scanf("%d",&column);
printf("enter the matrix:");
for(i=0;i<row;i++){
for(j=0;j<column;j++){
scanf("%d",&mat[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++){
for(j=0;j<column;j++){
printf("%d \t",mat[i][j]);}
printf("\n");}
}
I just choose an approximate value for the row and column. My selected row or column will not cross the value.and then I scan the matrix element then make it in matrix size.
int rows, cols , i, j;
printf("Enter number of rows and cols for the matrix: \n");
scanf("%d %d",&rows, &cols);
int mat[rows][cols];
printf("enter the matrix:");
for(i = 0; i < rows ; i++)
for(j = 0; j < cols; j++)
scanf("%d", &mat[i][j]);
printf("\nThe Matrix is:\n");
for(i = 0; i < rows ; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d",mat[i][j]);
printf("\t");
}
printf("\n");
}
}
//R stands for ROW and C stands for COLUMN:
//i stands for ROW and j stands for COLUMN:
#include<stdio.h>
int main(){
int M[100][100];
int R,C,i,j;
printf("Please enter how many rows you want:\n");
scanf("%d",& R);
printf("Please enter how column you want:\n");
scanf("%d",& C);
printf("Please enter your matrix:\n");
for(i = 0; i < R; i++){
for(j = 0; j < C; j++){
scanf("%d", &M[i][j]);
}
printf("\n");
}
for(i = 0; i < R; i++){
for(j = 0; j < C; j++){
printf("%d\t", M[i][j]);
}
printf("\n");
}
getch();
return 0;
}

Resources