How to delete spaces in matrix? - arrays

My task is to draw a coordinate system and line y+x=n, for n<=10.My code gives the correct result, but there is a condition that spaces or any other signs cannot be drawn after the line x+y=n. That is the only problem I have in solving this task and I hope you could help. I am a beginner.
(The only problem is that my code prints spaces above the line x+y=n and it should not)
#include <stdio.h>
int
main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for(i=1;i<n+1;i++){
if((10-n+i)!=10 && (2+3*i)!=1)
m[10-n+i][2+3*i]='*';
}
m[0][0]='1';
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
//n=5
10+
9 +
8 +
7 +
6 +
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + + + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Sounds like you want to not print the trailing spaces on each line. So you need to find and skip those trailing spaces rather than printing them:
for (i = 0; i < 12; i++) {
int eol = 63;
while (eol > 0 && m[i][eol-1] == ' ') --eol;
for (j = 0; j < eol; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}

Related

How to find neighbors average in matrix and assign it to a real number struct?

I have this assignment for college where i recieve the matrix A and i need to create the matrix B, where in each cell there whould be the neighbor's average of a specific cell from matrix A.
for example: https://i.stack.imgur.com/1ZuRA.png
i have the real number struct which looks like this:
typedef struct fraction
{
int num, numerator, denominator;
} fraction;
The function is yet to be complete but problem is, its easy to find the num field, but i struggling to find the numerator and denominator fields...(below is my function, im not allowed to change the decleration):
fraction neighborFractionAverage(int A[][COLS], int i, int j, int rows, int cols)
{
// your code:
fraction result;
int counter = 0;
int mone;
result.num = 0, result.numerator = 1, result.denominator = 1;
if ((i - 1 >= 0 && i - 1 < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i - 1][j - 1];
}
if ((i - 1 >= 0 && i - 1 < rows) && (j >= 0 && j < cols))
{
counter++;
result.num += A[i - 1][j];
}
if ((i - 1 >= 0 && i - 1 < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i - 1][j + 1];
}
if ((i >= 0 && i < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i][j - 1];
}
if ((i >= 0 && i < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i][j + 1];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i + 1][j - 1];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j >= 0 && j < cols))
{
counter++;
result.num += A[i + 1][j];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i + 1][j + 1];
}
result.num /= counter;
}

Matrices and coordinate system

How can I draw the function x+y=n with n not greater than 10 and the coordinate system using only matrices without any functions or libraries? I am a beginner and I hope you could help me.The only problem is that my code prints spaces above the line x+y=n and it should not.
#include <stdio.h>
int
main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for(i=1;i<n+1;i++){
if((10-n+i)!=10 && (2+3*i)!=1)
m[10-n+i][2+3*i]='*';
}
m[0][0]='1';
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
//n=5
10+
9 +
8 +
7 +
6 +
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + + + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
So first of all its quite difficult for me to understand your logic behind the if statements in the for loop.
With that said, to get the desired 10+ on the y-axis i set m[0][0] = '1'; which seems to work.
As for drawing the line, it seems that:
if (j == 3 * (n + i - 10) && i > 2 && j > 2) m[i][j] = '*';
was causing the issues. I replaced it with:
m[10 - n][2] = '*';
for (i = 1; i < n+1; i++)
{
m[10 - n + i][2 + 3 * i] = '*';
}
and it works fine and in a more understandable way hopefully!
So in the end the code should look like this:
#include<stdio.h>
int main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1) m[i][j] = '0';
if (i == 11 && j == 61) m[i][j] = '2';
if (j == 0 && i != 0 && i != 11) m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11) m[i][j] = '+';
if (i == 11 && j % 3 == 2) m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59) m[i][j] = '1';
if (i == 10 && j % 3 == 2) m[i][j] = '+';
// if (j == 3 * (n + i - 10) && i > 2 && j > 2) m[i][j] = '*';
}
}
m[10 - n][2] = '*'; // sets + on y-axis to *
for (i = 1; i < n+1; i++)
{
m[10 - n + i][2 + 3 * i] = '*'; // draws * diagonally jumping 3 characters (thus 3*i)
}
m[0][0] = '1'; // to have 10 instead of 0 at the top left
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
} printf("\n");
}
}
For n=7 for example you get the desired:
10+
9 +
8 +
7 *
6 + *
5 + *
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + * + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
To not delete the + by the * just offset the line to the right:
m[10 - n][3] = '*';
for (i = 1; i < n+1 ; i++)
{
m[10 - n + i ][3 + 3 * i] = '*'; // draws * diagonally jumping 3 characters (thus 3*i)
}
m[0][0] = '1'; // to have 10 instead of 0 at the top left
So the n=7 example should now look:
10+
9 +
8 +
7 +*
6 + *
5 + *
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + +* + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
In order to not print chars above or after the line try this:
#include <stdio.h>
int main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for (i = 1; i < n + 1; i++) {
if ((10 - n + i) != 10 && (2 + 3 * i) != 1)
m[10 - n + i][2 + 3 * i] = '*';
}
m[0][0] = '1';
for (i = 0; i < 12; i++)
{
for (j = 0; j < 63; j++)
{
if ((i < 10 - n) || (j > 2 + 3 * n))
{
m[i][j] = '\0';
}
}
}
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
and for n=5 you get:
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + +
0 1 2 3 4 5
Hope this helps!

Drawing with matrices in C

How can I draw the function x+y=n with n not greater than 10 and the coordinate system using only matrices without any functions or libraries? I am a beginner and I hope you could help me.
#include<stdio.h>
int main() {
int n,i,j;
scanf("%d", &n);
char m [12][63]={" "};
for(i=0;i<12;i++){
for(j=0;j<63;j++){
m[i][j]=' ';
if(i==0 && j==1) m[i][j]='0';
if(i==11 && j==61) m[i][j]='2';
if(j==0 && i!=0 && i!=11) m[i][j]='0'+10-i;
if(j==2 && i!=11) m[i][j]='+';
if(i==11 && j%3==2) m[i][j]='0'+((j-2)/3)%10;
if(i==11 && j%3==1 && j>29 && j<59) m[i][j]='1';
if(i==10 && j%3==2) m[i][j]='+';
if(j==3*(n+i-10) && i>2 && j>2) m[i][j]='*';
}
}
for(i=0;i<12;i++){
for(j=0;j<63;j++){
printf("%c", m[i][j]);
} printf("\n");
}
}
//my code doesn't print the correct result(the coordinate system is correct except that it prints 0+ instead of 10+ on y axis and x+y=n is not correct
0+
9 +
8 +
7 +
6 +
5 +
4 +
3 +
2 +
1 +*
0 + +* + + + + + + + + + + + + + + + + + + +
0 1 2* 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
I have no idea of what you were trying with those conditions. Check this one
#include <stdio.h>
#include <string.h>
#define N 100
int main() {
int n;
char m[N][N];
while( scanf("%d", &n) != 1 && n > (N-1)/2 );
memset(m, ' ', N*N);
for(int i = 0; i < n-1; i++) {
m[i][0] = '*';
m[i][i] = '*';
}
for(int i = 0; i < (n-1)*2; i++) m[n-1][i] = '*';
for(int i = 0; i < n; i++) {
for(int j = 0; j < (n-1)*2; j++) printf("%c", m[i][j]);
puts("");
}
return 0;
}
The program could be improved, it wastes a lot of memory and prints a lot of unnecessary blank spaces. Try to improve it by reducing the unused portion of the matrix. In this way you will also reduce the unnecessary prints.

Multiple Ternary in C with comma?

so I saw this code
b[2080];
main(j) {
for (;;) {
printf("\x1b[H");
for (j = 1; j < 2080; j++)
b[j] = j < 2000 ? (b[j + 79] + b[j + 80] + b[j] + b[j - 1] + b[j + 81]) / 5 : rand() % 4 ? 0 : 512, j < 1840 ? putchar((j % 80) == 79 ? '\n' : " .:*#$H#" [b[j] >> 5]) : 0;
usleep(20000);
}
}
so I tried to rewrite it, why even divide 32?
The array got to declare as global else it won't work. any idea?
also why 512?
here is my attempt so far, any problem?
for (int j = 1; j < 2080; j++)
{
if (j < 2000) {
b[j] = (b[j + 79] + b[j + 80] + b[j] + b[j - 1] + b[j + 81]) / 5;
}
else if (rand() % 4 != 0) { // Generate random integers in range 0 to 4
b[j] = 0;
}
else
{
b[j] = 512;
}
}
}
Here's the functionally equivalent code with the ternary operators converted into if/else statements:
if (j < 2000) {
b[j] = (b[j + 79] + b[j + 80] + b[j] + b[j - 1] + b[j + 81]) / 5;
} else if (rand() % 4) {
b[j] = 0;
} else {
b[j] = 512;
}
if (j < 1840) {
if ((j % 80) == 79) {
putchar('\n');
} else {
putchar(" .:*#$H#"[b[j] / 32]);
}
}
As for the question of what does the right shift >> 5 do, it divides by 32 (25), and then indexes the array " .:*#$H#" with that divided value.
edit: As for why the array is global, it's probably just to get it initialised to zero without extra code (the whole thing seems to be written as short as possible, e.g., using implicit int types and j from the argument).
Note that there is an access out of bounds bug in the (original) code: b[j + 81] can be accessed when j is 1999 (since that is < 2000), but 1999 + 81 == 2080, and b[2080] is out of bounds. You can replace the array with a local int b[2081] = { 0 }; but it changes the output slightly while fixing the bug.

give the location of matrix cell with 0 around

i need to input 6 on 6 matrix and to out put the location of the
cell that all the cells around him has the value 0.
let's say:
1 1 1 1 1 1
1 1 1 1 1 1
0 0 1 1 1 1
2 0 1 1 1 1
0 0 1 1 1 1
1 1 1 1 1 1
the out put will be row 3 col 0
this is the code i made.. how i solve this ?
i tried a lot please help me
#include<stdio.h>
#define N 6
int main()
{
int arr[N][N] = { 0 }, i = 0, j = 0, x = 0, y = 0, counter = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)//input//
{
scanf("%5d", &arr[i][j]);
}
printf("\n");
}
for (i = 0; i < N; i++)//output//
{
for (j = 0; j < N; j++)
{
printf("%5d", arr[i][j]);
}
printf("\n");
}
for (i = 0; i < N; j++)
{
for (j = 0; j < N; j++)
{
for (x = i - 1; x <= i + 1; x + 2)
{
for (y = j - 1; y <= j + 1; y + 2)
{
if (x >= 0 && x < N &&y >= 0 && y < N&&arr[x][y] == 0)
{
counter++;
}
}
}
if (i == 0 && j == 0 || i == 0 && j == N - 1 || i == N - 1 && j == 0
|| i == N - 1 && j == N - 1)
{
if (counter == 3)
{
printf("the row is %d the col is %d\n", i, j);
}
}
else if (i == 0 && j >= 1 && j < N - 1 || i == N - 1 && j >= 1 && j
< N - 1 && j == 0 && i >= 1 && i <= N - 1 && j == N - 1 && i >= 1 && i<N -1)
{
if (counter == 5)
{
printf("the row is %d the col is %d\n ", i, j);
}
}
else
{
if (counter == 8)
{
printf("the row is %d the col is %d\n ", i, j);
}
}
}
}
}
There are problems with your x and y loops:
for (x = i - 1; x <= i + 1; x + 2)
{
for (y = j - 1; y <= j + 1; y + 2)
{
You aren't incrementing x or y in these loops. The expression x + 2 merely evaluates the value x + 2. It doesn't do anything with it. If you want to actually set x to x + 2, then you need to use x = x + 2, or more concisely, x += 2.
Incrementing x and y by 2 is incorrect. It will only examine 4 points: (i-1,i-1), (i-1,i+1), (i+1,i-1), and (i+1,i+1). It will skip the following 4 points: (i-1,i), (i,i-1), (i,i+1), (i+1,i). You need to increment x and y by 1 each time, i.e. use x++ and y++ in the loop incremement instead of adding 2. The, inside the loop, add an additional test for x == i && y == i and skip that point (the center point).
I believe this is what you want (note that you can do eveything in two loops if you nest the conditionals in a smart way to avoid checking areas outside the matrix):
#include <stdio.h>
#define ROWS 5
#define COLS 5
void printBlockeds(int M[ROWS][COLS])
{
int i, j;
for(i = 0; i < ROWS; ++i)
{
for(j = 0; j < COLS; ++j)
{
int isBlocked = 1;
if(i > 0)
{
isBlocked = isBlocked && !M[i-1][j];
if(j > 0) isBlocked = isBlocked && !M[i-1][j-1];
if(j < COLS-1) isBlocked = isBlocked && !M[i-1][j+1];
}
if(j > 0) isBlocked = isBlocked && !M[i][j-1];
if(j < COLS-1) isBlocked = isBlocked && !M[i][j+1];
if(i < ROWS-1)
{
isBlocked = isBlocked && !M[i+1][j];
if(j > 0) isBlocked = isBlocked && !M[i+1][j-1];
if(j < COLS-1) isBlocked = isBlocked && !M[i+1][j+1];
}
if(isBlocked) printf("(%d, %d)\n", i, j);
}
}
}
int main()
{
int M[ROWS][COLS] = {{1, 1, 1, 0, 1},
{1, 0, 0, 0, 0},
{1, 0, 1, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 1, 0, 1}};
printBlockeds(M);
return 0;
}
Output for the case above:
(0, 4)
(2, 2)
(4, 2)

Resources