The reverse of all numbers on the 5th column on matrix [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a C problem where I need to reverse all the numbers on the 5th column of a 2x5 matrix.
So if I have
1 2 3 4 89
3 8 6 8 91
This will become
1 2 3 4 98
3 8 6 8 19
The code I've written so far is:
#include <stdio.h>
void inverse() {
int reversedNumber = 0, remainder, mat[10][10], i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
while (mat[i][j] != 0) {
remainder = mat[i][j] % 10;
reversedNumber = reversedNumber * 10 + remainder;
mat[i][j] /= 10;
}
}
printf("Reversed Number = %d", reversedNumber);
}
void main()
{
int mat[10][10], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
inverse(mat[1][5]);
}
After running this I get a ridiculously large number! What should I modify?

There are number of things that can be improved in the code.
First of all you have to set reversedNumber zero inside the innermost loop, this is the reason you get large numbers.
You pass an argument to the function, but the definition is incorrect for the same.
Also, you have stated that you only need to reverse the 5th column, better make call to a function that reverses a single number.
#include<stdio.h>
int inverse(int num) {
int reversednum = 0;
while(num){
reversednum = reversednum*10 + num%10;
num /= 10;
}
return reversednum;
}
void main(){
int mat[10][10],i,j;
printf("Enter your matrix\n");
for(i=0;i<2;i++)
for(j=0;j<5;j++){
scanf("%d",&mat[i][j]);
}
printf("\nHere is your matrix:\n");
for(i=0;i<2;i++){
for(j=0;j<5;j++){
printf("%d ",mat[i][j]);
if(j == 4) mat[i][j] = inverse(mat[i][j]);
}
printf("\n");
}
}

mistakes in your program:
-your function doesnt expect anything i.e empty parameter but you are sending matrix as parameter.
do not unnecessarily use matrix of size [10][10] when your matrix of 2*5
send 'mat' as parameter(i.e address of your matrix) to function inverse
#include <stdio.h>
int main() {
//code
int mat[10][10], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
if(j==4) //only for 5th column
{
// int temp=mat[i][j]; // can use temporary variable instead of changing actual value matrix (better option)
int remainder, reverse =0;
while(mat[i][j]>0)
{
remainder=mat[i][j]%10;
reverse=reverse*10 + remainder;
mat[i][j]=mat[i][j]/10;
}
mat[i][j]=reverse;
}
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Edited: modified code from the question
#include <stdio.h>
void inverse(int mat1[2][5]) {
int i, j;
for (i = 0; i < 2; i++){
int j=4;
int reversedNumber = 0, remainder=0;
while (mat1[i][j] > 0) {
remainder = mat1[i][j] % 10;
reversedNumber = reversedNumber * 10 + remainder;
mat1[i][j] /= 10;
}
printf("Reversed Number = %d\n",reversedNumber);
}
}
void main()
{
int mat[2][5], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
inverse(mat);
}
hope this helps.

You have to pass the matrix to the inverse function so that the matrix (mat) can be modified. If you declare a separate mat array inside inverse then that's a different scope. You also have to figure out how many digits there are in the number. You can use <math.h> functions, or the example below uses basic calculations.
void inverse(int mat[2][5])
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 5; j++)
{
int n = mat[i][j];
int digits = 0;
while(n > 0)
{
digits++;
n /= 10;
}
n = mat[i][j];
int rev = 0;
while(digits > 0)
{
int x = n % 10;
for(int c = 0; c < digits - 1; c++)
x *= 10;
rev += x;
n /= 10;
digits--;
}
mat[i][j] = rev;
}
}
int main(void)
{
int mat[2][5] = {
1, 2, 3, 4, 89,
3, 8, 6, 8, 91 };
inverse(mat);
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 5; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}

Related

Expression must have arithmetic type issue

I need to multiply two square matrixes A and B 15x15.
Unfortunately, I'm getting this kind of error.
I know the problem is in pointers while calculating matrix C.
C[i][j] += *(A + k) * *(B + k)
I hope you can explain me what's wrong. I'm a beginner xD.
Thank you in advance.
#include <stdio.h>
#define N 15
#define _CRT_SECURE_NO_WARNINGS
int main() {
int A[N][N];
int B[N][N];
int C[N][N];
printf("Input matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &A[i][j]);
}
printf("\n");
}
printf("Input matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &B[i][j]);
}
printf("\n");
}
printf("Matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("Matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
C[i][j] = 0;
for (int k = 0; k < 14; k++) {
C[i][j] += *(A + k) * *(B + k);
k++;
}
}
}
printf("Your result:\n");
printf("Matrix C.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
The problem in the multiplication is that A+k and B+k have type int (*)[15] which means dereferencing it once only makes a pointer out of them; furthermore, you need to take row and column items individually, which means A[i][k] and B[k][j], right? (also, there's no point on using confusing syntax, as the underlying operation is exactly the same).
Here's a fixed and improved version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 15
/* Improvement 1 (type abstraction) */
typedef int NxN_int_matrix[N][N];
/* Improvement 2 (input function & wrapper) */
#define input_matrix(var) input_matrix_ex((var), #var)
static void input_matrix_ex(NxN_int_matrix dst, char *name)
{
printf("Input matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 3 (nicer prompt) */
printf("%s[%2d][%2d]: ", name, i, j);
fflush(stdout);
scanf_s("%d", &dst[i][j]);
}
}
printf("\n");
}
/* Improvement 4 (print function) */
#define print_matrix(var) print_matrix_ex(#var, (var))
static void print_matrix_ex(char *name, NxN_int_matrix M)
{
printf("Matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Improvement 5 (move multiplication to a function too, and fix it) */
static void mult_matrix(NxN_int_matrix dst, NxN_int_matrix a, NxN_int_matrix b)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 6 (don't write out intermediate values) */
int tmp = 0;
for (int k = 0; k < N; k++)
tmp += a[i][k] * b[k][j];
dst[i][j] = tmp;
}
}
}
int main()
{
NxN_int_matrix A, B, C;
input_matrix(A);
input_matrix(B);
print_matrix(A);
print_matrix(B);
mult_matrix(C, A, B);
printf("Your result:\n");
print_matrix(C);
return 0;
}
/* Possible further improvements:
* - using a transposed B might make multiplication faster
*/

Finding the highest frequency of an array and all elements which have that frequency

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;
}

Arranging columns in a matrix lexicographically

I've been trying to sort columns in a matrix (the dimensions are m,n <= 10) via the lexicographical order (if the columns share the same element, then we compare the elements in the row beneath etc.) with some additional conditions. I need to use functions to print the matrix, input random integers up to 5 as its elements and finally arrange the matrix. I think I got the printing and random inputs correctly but I can't figure out the sorting. Plus I can't use global variables which I have no idea how to do, since I haven't been shown. An example matrix would be :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
int mat[10][10];
void print_matrix()
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (mat[i][j] < mat[k][j])
{
a = mat[i][j];
mat[i][j] = mat[k][j];
mat[k][j] = a;
}
}
}
}
}
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int(mat[m][n]);
print_matrix(mat[m][n]);
arrange(mat[m][n]);
print_matrix(mat[m][n]);
return 0;
}
Try this solution(will work for input 0-8 only), also used global variables:
There have multiple solutions. but is the easiest one.
I have converted each of the columns as an integer value. then bubble sorted the integers. After sorting. I have then converted the integer value to digits. (You have to know how to convert individual digits to multiple digit integer and multiple digit integers to single-digit.
Note I have added one(1) with each digit. Because the input can be zero(0). if you convert 0 0 2 1 to an integer will be only 21. the first two digits lost. So I have added 1. so 0 0 2 1 will be converted to 1132. I have done (added 1) for each input(deducted 1 after sorting). so it will not affect other inputs. Be careful input have to be from(0 to 8)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int m, n;
int mat[10][10];
int generatedNumber[10];
void print_matrix()
{
printf("The matrix is:\n");
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
int number = 0;
for (i = 0; i < m; ++i)
{
number = number * 10 + mat[i][j] + 1;///Adding one for avoiding zero(0)
}
generatedNumber[j] = number;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( generatedNumber[j] > generatedNumber[j+1])
{
// swap the elements
int temp = generatedNumber[j];
generatedNumber[j] = generatedNumber[j+1];
generatedNumber[j+1] = temp;
}
}
}
for(i = 0; i < n; i++)///columwise
{
int generatedColumnvalue = generatedNumber[i];
for(j = m -1; j>= 0; j--)///row wise and fro last vaue to first
{
mat[j][i] = (generatedColumnvalue%10)-1;///removing extra added 1
generatedColumnvalue/=10;
}
}
}
int main()
{
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int();
print_matrix();
arrange();
print_matrix();
return 0;
}

How do I change the array Input Order in C

I need to input five numbers and then a positive or negative. The order will be changed like the number.
For example, if three (3) is the number then:
1 2 3 4 5 6 7 8 9 10
will become:
4 5 6 7 8 9 10 1 2 3
Please do not use pointers. I have made the following code and would like to be able to improve on it. Can I use a mathematical formula using the modulus operator % ? If so, how would I be able to do it.
Thanks, this is my code.
#include<stdio.h>
#define n 10
int main(void)
{
int num[n] = { 0 }, assist[n] = { 0 }, i = 0, j = 0, variable = 0, k = 0;
for (i = 0; i < n; i++)
{
printf("please enter index. %d\n", i );
scanf("%d", &num[i]);
}
printf("please enter the circle number\n");
scanf("%d", &variable);
printf("\n\n");
if (variable >= 0)
{
for (i = variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < variable, j < n; i++, j++)//to assist//
{
assist[j] = num[i];
}
}
if(variable < 0)
{
for (i = n + variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < n + variable, j < n; i++, j++)
{
assist[j] = num[i];
}
}
for (i = 0; i < n; i++)//output//
{
printf("%d\n", assist[i]);
}
return 0;
}
Something like this. I did not compile; any small mistakes are for you.
#include<stdio.h>
#define N 10
int main(void)
{
int numbers[N];
int offset;
for (int i = 0; i < N; i++)
{
printf("please enter number %d: ", i);
scanf("%d", &num[i]);
}
printf("\nplease enter the circle number: ");
scanf("%d", &offset);
printf("\n\n");
for (int i = 0; i < N; i++)
{
printf("%d ", num[(i + offset) % N]);
}
printf("\n");
}

#EMERGENCY!!! Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted

I know this seems like an old question, but none answered questions I searched work.
I have kept receiving "Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted." when I was trying to do a [4][2]*[2][3] matrix multiplication.
Does anyone spot the problem?
#include <stdio.h>
int main() {
int a[4][2] = {0};
int b[2][3] = {0};
int c[3][3] = {0};
int i, j;
printf("Please enter first matrix value\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Please enter second matrix value\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
printf("\n the result is :\n");//
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
printf(" %4d ", c[i][j]);
}
printf("]\n");
}
return 0;
}
I haven't checked your code thoroughly, but you define c as 3x3, and here
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
...you access c[3], which is c's fourth element, and does not exist. This is bound to write somewhere else.
So check your indexes (as #ptb observed, c's should actually be four rows deep).

Resources