I'm having a problem with matrix.
When i'm trying to print the last matrix ( Invertible matrix ) to file ( matrixrez.txt) program prints only first column.
Why?
Matrix.txt:
1 0 1 1 2 0
1 0 2 0 3 0
2 1 1 1 2 3
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file, *outf;
int matrixA[3][3], matrixB[3][3], a[3][3];
int trash[3];
int i, j, k, sum;
float determinant = 0;
i = j = k = sum = 0;
file = fopen("matrix.txt", "rt");
outf = fopen("matrixrez.txt", "w+");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixA[i][j]);
}
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
}
fseek(file, 0, SEEK_SET);
for (i = 0; i < 3; i++) {
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixB[i][j]);
}
}
/* Matrix multiplication */
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + matrixA[i][k]*matrixB[k][j];
}
a[i][j] = sum;
sum = 0;
}
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixA[i][j]);
}
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixB[i][j]);
}
}
printf("\n");
printf("The resultant matrix is:: \n");
fprintf(outf,"The resultant matrix is:: \n");
for (i = 0; i < 3; i++) {
printf("\n");
fprintf(outf,"\n");
for (j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
fprintf(outf,"%d ", a[i][j]);
}
}
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
printf("\n");
system("pause");
return 0;
}
I get matrixrez.txt:
The resultant matrix is::
2 4 3
3 6 6
3 9 3
Inversion of Matrix:
4.00
-1.67
-0.67
I think something wrong is with line at the end of a file (if I'm not mistaken) :
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
You need to put braces around what you meant to have as your for loop block at the end:
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
{ /* YOU NEED THIS BRACE */
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
} /* AND YOU NEED THIS BRACE */
fprintf(outf,"\n");
printf("\n");
}
You do not have brackets around the inner for loop. So, it loops through i just fine but each time it cycles through j without calling the fprintf just the printf
The code should be
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
}
Related
Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.
hi this is my code can you help me out not printing output properly how to print output given in description , i am not getting way to sort the given output, output should be sorted from given average.
Implement fragments using array of pointers.
Rows are static and columns are dynamic. Fixed no.of rows and columns will vary for each row.
Example:
Read no.of rows from user and allocate the memory statically for rows.
Read no.of columns for each row and allocate the memory dynamically.
Let us Assume, Row = 3.
Row[0] = 4 columns, Row[1] = 3 columns and Row[2] = 5 columns.
While allocating the memory for columns you have allocate for no.of columns + 1 dynamically.
After that read the values from user and calculate the average for each row separately and store that average in that extra memory block which you added while allocating the memory.
Then sort the array based on the average.
Print the output on the screen.
Example is given below.
ENTER THE NUMBER OF ARRAY YOU WANT TO GIVE AS INPUT: 3
ENTER NO. OF COLUMNS IN ROW[0]: 3
ENTER NO. OF COLUMNS IN ROW[1]: 3
ENTER NO. OF COLUMNS IN ROW[2]: 3
ENTER 3 VALUES OF ROW[0]: 2 3 5
ENTER 3 VALUES OF ROW[1]: 2 4 7
ENTER 3 VALUES OF ROW[2]: 2 1 0
BEFORE SORTING OUTPUT IS:
2 3 5 3.33333
2 4 7 4.33333
2 1 0 1
AFTER SORTING OUTPUT IS:
2 1 0 1
2 3 5 3.33333
2 4 7 4.33333
My code so far:
#include <stdio.h>
#include <stdlib.h>
int fragments(int rows, int *array)
{
int i, j;
int pos[rows];
float *arr[rows], avg, sum;
char *temp[rows];
for (i = 0; i < rows; i++)
{
arr[i] = malloc(sizeof(float*) * array[i]+1);
pos[i] = i;
printf("Enter %d values for row[%d]: ", array[i], i);
for (j = 0; j< array[i]; j++)
{
scanf(" %f", &arr[i][j]);
}
}
printf("Before Sorting output is:\n");
for (i = 0; i < rows; i++)
{
sum = 0;
for (j = 0; j < array[i]; j++)
{
printf("%f ", arr[i][j]);
sum = sum + arr[i][j];
}
printf("%f\n", arr[i][j] = (sum / j));
}
for (i = 0; i < rows - 1; i++)
{
for (j = 0; j < rows - i - 1; j++)
{
if (arr[j][array[j]] > arr[j+1][array[j+1]])
{
temp[j][j] = arr[j][array[j]];
arr[j][array[j]] = arr[j+1][array[j+1]];
arr[j+1][array[j+1]] = temp[j][j];
}
}
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < array[i] + 1; j++)
{
printf("%f ", arr[i][array[j]]);
}
printf("\n");
}
}
int main()
{
int rows, i, j;
printf("Enter no of rows: ");
scanf("%d", &rows);
int array[rows];
int pos[rows];
for (i = 0; i < rows; i++)
{
printf("Enter no of columns in row[%d]:", i);
scanf("%d", &array[i] );
}
fragments(rows, array); //`enter code here`
}
There are multiple problems in your code:
The allocation size is incorrect: arr[i] = malloc(sizeof(float*) * array[i]+1); should read:
arr[i] = malloc(sizeof(float) * (array[i] + 1));
the swapping code should swap the array pointers and the array sizes. The current code does not make sense. Use this instead:
float *temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int temp1 = array[j];
array[j] = array[j+1];
array[j+1] = temp1;
the final print loop is incorrect: instead of printf("%f ", arr[i][array[j]]); you should write:
printf("%f ", arr[i][j]);
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int fragments(int rows, int *array) {
float *arr[rows];
for (int i = 0; i < rows; i++) {
arr[i] = malloc(sizeof(float) * (array[i] + 1));
printf("Enter %d values for row[%d]: ", array[i], i);
for (int j = 0; j < array[i]; j++) {
if (scanf("%f", &arr[i][j]) != 1)
return;
}
}
printf("Before Sorting output is:\n");
for (int i = 0; i < rows; i++) {
float sum = 0;
for (j = 0; j < array[i]; j++) {
printf("%f ", arr[i][j]);
sum += arr[i][j];
}
printf("%f\n", arr[i][j] = sum / j);
}
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < rows - i - 1; j++) {
if (arr[j][array[j]] > arr[j+1][array[j+1]]) {
float *temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int temp1 = array[j];
array[j] = array[j+1];
array[j+1] = temp1;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < array[i] + 1; j++) {
printf("%f ", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
}
int main() {
int rows;
printf("Enter no of rows: ");
if (scanf("%d", &rows) != 1)
return 1;
int array[rows];
for (int i = 0; i < rows; i++) {
printf("Enter no of columns in row[%d]:", i);
if (scanf("%d", &array[i]) != 1)
return 1;
}
fragments(rows, array);
return 0;
}
I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}
I am practicing my C program currently and I want to calculate two 2D matrix.
But I get confused when I use printf() to check these arrays. They break the program.
Code as below:
int matric_multi_main()
{
int m1_row = 0;
int m1_col = 0;
int m2_row = 0;
int m2_col = 0;
int **matric1;
int **matric2;
printf("Please enter Matric 1 row: ");
scanf("%d", &m1_row);
printf("Please enter Matric 1 column: ");
scanf("%d", &m1_col);
printf("Please enter Matric 2 row: ");
scanf("%d", &m2_row);
printf("Please enter Matric 2 column: ");
scanf("%d", &m2_col);
if (m1_col != m2_row)
{
printf("Error matric size!!!\n");
return 0;
}
/* Allovate memory for matric 1 */
matric1 = malloc(m1_row * sizeof(int *));
for (int i = 0; i < m1_row; i++)
{
matric1[i] = malloc(m1_col * sizeof(int));
}
/* Allovate memory for matric 2 */
matric2 = malloc(m2_row * sizeof(int *));
for (int i = 0; i < m2_row; i++)
{
matric2[i] = malloc(m2_col * sizeof(int));
}
/* Init matric 1 */
for (int i = 0; i < m1_row; i++)
{
for (int j = 0; j < m1_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric1+i*m1_row+j);
}
}
/* Init matric 2 */
for (int i = 0; i < m2_row; i++)
{
for (int j = 0; j < m2_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric2+i*m2_row+j);
}
}
for (int i = 0; i < m1_row; i++)
{
printf("[");
for (int j = 0; j < m1_col; j++)
{
//printf("%d ", *(matric1 + i*m1_row + j)); <- correctly
//printf("%d ", &matric1[i][j]); <-- display the data with wrong order
printf("%d ", matric1[j][i]); <- break the program
}
printf("]\n");
}
for (int i = 0; i < m2_row; i++)
{
printf("[");
for (int j = 0; j < m2_col; j++)
{
//printf("%d ", *(matric2 + i*m2_row + j));
printf("%d ", matric2[i][j]);
}
printf("]\n");
}
}
I think I can use matric1[i][j] to get the correct data directly.
But the example on the website always use array[][] directly.
I can understand the different between my program and example.
I'm trying to write a program to calculate the sum and product of two matrices, but I can't get it the product to work. The sum is fine. I am using Visual Studio.
Here's my program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a[2][2], b[2][2], c[2][2], d[2][2], sum;
int i,j,k;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
d[i][j] = 0;
}
}
printf("Enter the elements of 1st matrix\n");
/*
* Reading two dimensional Array with the help of two for loop. If there
* was an array of 'n' dimension, 'n' numbers of loops are needed for
* inserting data to array.
*/
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
printf("\nEnter the elements of 2nd matrix\n");
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j) {
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%f", &b[i][j]);
}
printf("\nMatrix 1:\n");
for (i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
printf("%.1f\t", a[i][j]);
if (j == 1) /* To display matrix sum in order. */
printf("\n");
}
printf("\nMatrix 2:\n");
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
printf("%.1f\t", b[i][j]);
if (j == 1) /* To display matrix sum in order. */
printf("\n");
}
for (i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:\n");
for (i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
printf("%.1f\t",c[i][j]);
if (j == 1) /* To display matrix sum in order. */
printf("\n");
}
for(i = 0 ; i < 2 ; i++) {
for(j = 0 ; j < 2 ; j++) {
sum = 0 ;
for(k = 0 ; k < 2 ; k++) {
sum = sum + a[i][k] * b[k][j];
printf("a: %d; b: %d\n", a[i][k], b[k][j]);
printf("%d", sum);
}
d[i][j]=sum;
}
}
printf("\nThe multiplication matrix is : \n\n") ;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%d \t", d[i][j]) ;
}
printf("\n") ;
}
system("PAUSE");
return 0;
}
Here is the output:
Matrix 1:
1.0 2.0
3.0 4.0
Matrix 2:
5.0 6.0
7.0 8.0
Sum Of Matrix:
6.0 8.0
10.0 12.0
a: 0; b: 1072693248
0a: 0; b: 1073741824
0a: 0; b: 1072693248
0a: 0; b: 1073741824
0a: 0; b: 1074266112
0a: 0; b: 1074790400
0a: 0; b: 1074266112
0a: 0; b: 1074790400
0
The multiplication matrix is :
0 0
0 0
I can't see where the problem is with both matrices A and B.
First off, I would not hardcode the termination of the for loops to a constant, but to sizeof(a)/sizeof(a[0]), for instance. Secondly, the problem is you are trying to print floats as ints - line 68 reads:
printf("a: %d; b: %d\n",a[i][k],b[k][j]);
but it should be
printf("a: %.1f; b: %.1f\n",a[i][k],b[k][j]);
These problems exist on lines 68, 69, and 79. If you change the %d to %f you should be fine.