cannot print the matrix on the console - c

Using the following code I cannot print any of the matrix A, B or C.
As a testing purpose even not an int is printed.
#include"stdio.h"
int main()
{
#define row 3
#define col 3
int A[row][col]={ {1,2,3},{2,3,4},{3,4,5}};
int B[row][col]={ {1,0,0},{0,1,0},{0,0,1}};
int i,j;
int C[row][col]={ {0,0,0},{0,0,0},{0,0,0}};
int temp=2;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]*B[j][i]+C[i][j];
}
printf("%d \n ", C[i][j]);
getchar();
}
}

The problem in your code is here:
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]*B[j][i]+C[i][j];
}
// j contains 3 here therefore you acess the C array out of bounds
printf("%d \n ", C[i][j]); //<<<<<<<<<<<<<<
getchar();
}
You probably want this:
...
// Multiplication of A and B
for (i = 0; i<3; i++)
{
for (j = 0; j<3; j++)
{
C[i][j] = A[i][j] * B[j][i] + C[i][j];
}
}
// Display C
for (i = 0; i<3; i++)
{
for (j = 0; j<3; j++)
{
printf ("%d ", C[i][j]);
}
printf("\n");
}
getchar();
...
Even better: write a Display3x3Matrix and use it:
void Display3x3Matrix(int m[3][3])
{
for (int i = 0; i<3; i++)
{
for (int j = 0; j<3; j++)
{
printf("%d ", m[i][j]);
}
printf("\n");
}
}
...
printf("A\n");
Display3x3Matrix(A);
printf("\nB\n");
Display3x3Matrix(B);
printf("\nC\n");
Display3x3Matrix(C);

This works for me (printing the "A" Matrix):
#define row 3
#define col 3
int A[row][col] = { { 1,2,3 },{ 2,3,4 },{ 3,4,5 } };
int rows, columns;
for (rows = 0; rows<3; rows++)
{
for (int columns = 0; columns<3; columns++)
{
printf("%d ", A[rows][columns]);
}
printf("\n");
}

Change the for loop as follows
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]*B[j][i]+C[i][j];
printf("%d", C[i][j]);
}
printf("\n");
}
Demo

Related

In this question im trying to solve a problem about for loops

In this question I'm looking for any row or column which its sum is more than 2,the first row or column which is found break through the loop and printing yes if there isn't any print no. The code works fine for printing the sum of each row and column but it can't define if the input matrix has any row or column with such quality.
#include <stdio.h>
int main()
{
int n,m,i,j;
int sum_r,sum_c;
int sw = 0;
scanf("%d%d",&m,&n);
int a [10][10];
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0; i<m; i++)
{
sum_r = 0;
for(j=0; j<n; j++)
{
sum_r += a[i][j];
if(sum_r >= 2)
{
sw = 1;
}
}
}
for(i=0; i<n; i++)
{
sum_c = 0;
for(j=0; j<m; j++)
{
sum_c += a[j][i];
if (sum_c >= 2)
{
sw = 1;
}
}
}
if (sw = 1) printf("yes");
else printf("no");
return 0;
}
For starters there is a typo in this if statement
if (sw = 1) printf("yes");
You have to use the comparison operator == instead of the assignment operator =
if (sw == 1) printf("yes");
Or simpler
if (sw) printf("yes");
If I have understood correctly then what you need is something like the following
for(i=0; !sw && i<m; i++)
{
sum_r = 0;
for(j=0; !( sum_r > 2 ) && j<n; j++)
{
sum_r += a[i][j];
if(sum_r > 2)
{
sw = 1;
}
}
}
for(i=0; !sw && i<n; i++)
{
sum_c = 0;
for(j=0; !( sum_c > 2 ) && j<m; j++)
{
sum_c += a[j][i];
if (sum_c > 2)
{
sw = 1;
}
}
}

Sort a matrix row wise (c programming)

I wrote this code for sorting an nxn matrix: odd rows in descending order, double rows in ascending order, but it doesn't pass the compiler stage.
What did I do wrong?
It mostly tells me this: assignment to 'int' from 'int *' makes integer from pointer without a cast (how can I solve this problem)?
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if (a < b) {
return 1;
} else {
return 0;
}
}
int comp_d(int a, int b) {
if (a > b) {
return 1;
} else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (comp(a[j], a[j + 1]) == 0) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
int main() {
int n;
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
int *mat = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
for (int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
sort(mat[i], n, &comp_a);
} else {
sort(mat[i], n, &comp_d);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
You should change your matrix allocation to this:
int **mat = (int**)calloc(n, sizeof(int*)); // (1)
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int)); // (2)
}
(1): Declaring an array of pointers of size n x int*.
(2): Where each pointer in this array points to an array of integers of size n x int.
I changed your matrix allocation
int *mat = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
to
int mat[n][n];
I changed for loop of ordred
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1; j++) {
if(comp(a[j], a[j + 1]) == 0)
because he take a lot of time (time of running) to
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0)
my code:
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if(a < b) {
return 1;
}
else {
return 0;
}
}
int comp_d(int a, int b) {
if(a > b) {
return 1;
}
else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0) {
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main() {
int n;
do
{
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
}while(n<1);
int mat[n][n];
for(int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for(int i = 0; i < n; i++) {
if(i%2 == 0) {
sort(mat[i], n, &comp_a);
}
else {
sort(mat[i], n, &comp_d);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}

Printing patterns using arrays in c

The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888
Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}

array placement when user input another data

user input 10 data, after that, user input 1 number. and the program will print out the placement of where the number store in the array.
int main() {
int arr[10];
int data,num;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",&arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < arr[data]; j++) {
if (data==arr[j]) {
printf("Data %d found in array %d\n",data,arr[j] );
break;
}
else {
printf("Data not found in array\n");
break;
}
}
return 0;
}
int main() {
int arr[10];
int data, flag = 0;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < 10; j++) {
if (data==arr[j]) {
printf("Data %d found in array at %dth position\n",data,j);
flag = 1;
break;
}
}
if(!flag){
printf("data not found in the array\n");
}
return 0;
}

When running the code it does not respond (yet it runs correctly in the background)

I built a program that displays the action of a guard. First there are 100 (I put 10 in the code as it was easier to spot errors) (from 1 to 100) lamps in a street. At first they are all were on but :-
after he passed by them for the first time he turned all of them off.
after the second time that he passed by them he changed the lights
that were in place of 2 and its multiples (2 4 6 8 ...) — the
ones that were off now on and the opposite.
third time he changed the ones in third place and multiples of
3 (3 6 9 ....) — the ones that were off now on and the opposite.
fourth time he changed the ones in fourth place and multiples of
4 (4 8 12 ....)
The program displays how many lamps were off at the end.
Now I built the code the compiler shows no warning nor errors yet when run it it stops responding. (Compiler set to show all warnings.)
Does anyone have an idea what is wrong with the code?
#include<stdio.h>
#define N 100
int firstwalk(int light[N]);
int secondtwalk(int light[N]);
int thirdtwalk(int light[N]);
int fourthtwalk(int light[N]);
int main()
{
int light[N] = {0};
int i;
for (i = 0;i < N;i++)
{
light[i] = 1;
}
printf("Lights before the first walk- ");
for (i = 0;i < N;i++)
{
printf("%d ", light[i]);
}
firstwalk(light);
secondtwalk(light);
thirdtwalk(light);
fourthtwalk(light);
printf("the lights that were off are :- ");
for (i = 0;i < N;i++)
{
if (light[i] == 0)
printf("%d ", i+1);
}
}
int firstwalk(int light[N])
{
int i;
for (i = 0;i < N;i++)
{
light[i] = 0;
}
printf("Lights after the first walk- ");
for (i = 0;i < N;i++)
{
printf("%d ", light[i]);
}
printf("\n");
return light;
}
int secondtwalk(int light[N])
{
int i;
for (i = 1;i < N;i= i * 2 + 1)
{
light[i] = 1;
}
printf("Lights after the second walk- ");
for (i = 0;i < N;i++)
{
printf("%d ", light[i]);
}
printf("\n");
return light;
}
int thirdtwalk(int light[N])
{
int i;
for (i = 2;i < N;i= i * 3 + 2)
{
if (light[i] == 1)
light[i] = 0;
else light[i] = 1;
}
printf("Lights after the third walk- ");
for (i = 0;i < N;i++)
{
printf("%d ", light[i]);
}
printf("\n");
return light;
}
int fourthtwalk(int light[N])
{
int i;
for (i = 3;i < N;i= i * 4 + 3)
{
if (light[i] == 1)
light[i] = 0;
else light[i] = 1;
}
printf("Lights after the third walk- ");
for (i = 0;i < N;i++)
{
printf("%d ", light[i]);
}
printf("\n");
return light;
}
Thanks.
The main bug in your code is basically the way you're accessing the arrays, you're going beyond the array boundaries all the time, here's a possible version fixing that:
#include <stdio.h>
#define N 10
void show_lights_all(char *label, int light[N]);
void show_lights_off(char *label, int light[N]);
void firstwalk(int light[N]);
void secondtwalk(int light[N]);
void thirdtwalk(int light[N]);
void fourthtwalk(int light[N]);
int main() {
int light[N] = {0};
show_lights_all("Initial state of lights- ", light);
firstwalk(light);
secondtwalk(light);
thirdtwalk(light);
fourthtwalk(light);
show_lights_off("\nLights that were off are :- ", light);
}
void show_lights_all(char *label, int light[N]) {
printf(label);
for (int i = 0; i < N; i++) {
printf("%d ", light[i]);
}
}
void show_lights_off(char *label, int light[N]) {
printf(label);
for (int i = 0; i < N; i++) {
if (light[i] == 0) printf("%d ", i + 1);
}
}
void firstwalk(int light[N]) {
show_lights_all("\nLights before the first walk- ", light);
for (int i = 0; i < N; i++) {
light[i] = 0;
}
show_lights_all("Lights after the first walk- ", light);
}
void secondtwalk(int light[N]) {
show_lights_all("\nLights before the second walk- ", light);
for (int i = 1; i < N; i += 2) {
light[i] = 1;
}
show_lights_all("Lights after the second walk- ", light);
}
void thirdtwalk(int light[N]) {
show_lights_all("\nLights before the third walk- ", light);
for (int i = 2; i < N; i += 3) {
light[i] = light[i] == 1 ? 0 : 1;
}
show_lights_all("Lights after the third walk- ", light);
}
void fourthtwalk(int light[N]) {
show_lights_all("\nLights before the fourth walk- ", light);
for (int i = 3; i < N; i += 4) {
light[i] = light[i] == 1 ? 0 : 1;
}
show_lights_all("Lights after the fourth walk- ", light);
}
Think about it, let's say you're doing something like for(int i=0;i<N;i++) light[i * 3 + 2], now... when i=3->light[11], i=4->light[14], and so on... and you've allocated only memory for an array of 10 elements.
A better pattern would be replacing something like:
for(int i=0;i<N;i++) light[i*k1+k2]
by something like:
for(int i=k2;i<N;i+=k1) light[i]
it's a safer version which guarantees not going beyond the array boundaries
After you've understood the above explanation, the next step would be refactor the code into something less verbose:
#include <stdio.h>
#define N 10
#define LIGHTS_ALL -1
#define LIGHTS_OFF 0
#define LIGHTS_ON 1
char *lights_str(int light[N], int status_light) {
char s[256] = {0};
int n = 0;
for (int i = 0; i < N; i++) {
if (status_light == LIGHTS_OFF) {
if (light[i] == LIGHTS_OFF) n += sprintf(&s[n], "%d ", i);
continue;
}
if (status_light == LIGHTS_ON) {
if (light[i] == LIGHTS_ON) n += sprintf(&s[n], "%d ", i);
continue;
}
n += sprintf(&s[n], "%d", light[i]);
}
return s;
}
void firstwalk(int light[N]) {
for (int i = 0; i < N; i++) {
light[i] = 0;
}
}
void secondtwalk(int light[N]) {
for (int i = 1; i < N; i += 2) {
light[i] = 1;
}
}
void thirdtwalk(int light[N]) {
for (int i = 2; i < N; i += 3) {
light[i] = light[i] == 1 ? 0 : 1;
}
}
void fourthtwalk(int light[N]) {
for (int i = 3; i < N; i += 4) {
light[i] = light[i] == 1 ? 0 : 1;
}
}
int main() {
int light[N] = {0};
char *walks[256] = {"first", "second", "third", "fourth"};
void (*p[4])(int light[N]);
p[0] = firstwalk;
p[1] = secondtwalk;
p[2] = thirdtwalk;
p[3] = fourthtwalk;
for (int i = 0; i < 4; i++) {
printf("\nLights before the %s walk-\t%s", walks[i],
lights_str(light, LIGHTS_ALL));
p[i](light);
printf("\tLights after the %s walk-\t%s", walks[i],
lights_str(light, LIGHTS_ALL));
}
printf("\n\nFinal status\tLights on\t%s\tLights off\t%s",
lights_str(light, LIGHTS_OFF), lights_str(light, LIGHTS_ON));
}
In fact, don't stop here, try to refactor further till the code becomes better and better... I'll leave that task to you :)

Resources