Matrices and coordinate system - arrays

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!

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

How to delete spaces in matrix?

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

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.

I get a segmentation fault when I compile and try to run this code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is a part of my code:
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
for(k = 0; k < rows - i && k < columns - j; k++)
{
if(i + k > rows || j + k > columns) break;
if(b->board[i + k][j + k] == 1) counter++;
if(b->board[i + k][j + k] == 2) counter = 0;
if(counter > max_pd)
{
plh = counter;
jkee = j;
kkee = k;
}
}
counter = 0;
}
if(plh > max_pd)
{
max_pd = plh;
plh = 0;
for(n = 0; n < 1; n++)
{
if(i + kkee > rows || jkee + kkee + 1 > columns) break;
if(b->board[i + kkee + 1][jkee + kkee + 1] == 0 && b->board[i + kkee][jkee + kkee + 1] != 0) play_pd = jkee + kkee + 1;
}
}
counter = 0;
plh = 0;
jkee = 0;
kkee = 0;
}
I get a segmentation fault when I try to run this code at the line :
if(b->board[i + kkee + 1][jkee + kkee + 1] == 0 && b->board[i + kkee][jkee + kkee + 1] != 0) play_pd = jkee + kkee + 1;
However in the exact above line I clearly state that if the numbers put in board are not in there, break.
(the board is defined in a struct like this: b->board[rows][columns])
The problem is here:
if(i + kkee > rows || jkee + kkee + 1 > columns) break;
if(b->board[i + kkee + 1][jkee + kkee + 1] == 0 && b->board[i + kkee][jkee + kkee + 1] != 0) play_pd = jkee + kkee + 1;
The index for the row an column of the bord must be respectively < rows and < columns.
The test to break should be
if(i + kkee + 1 >= rows || jkee + kkee + 1 >= columns) break;
This will ensure that the next instruction will not use indexes out of bounds.
You have the same problem above with the instructions:
if(i + k > rows || j + k > columns) break;
if(b->board[i + k][j + k] == 1) counter++;
if(b->board[i + k][j + k] == 2) counter = 0;
The test for the break should be
if(i + k >= rows || j + k >= columns) break;

how to sort arrays that pointed by pointers array?

I need to sort arrays that pointed by pointers array
with this function (i need only with pointers, without these [ ])
void getSort(int** p2a, int arrSizes[]) //p2a is a array of pointers to diffrent arrays
I tried bubble sort:
if (i < 5)
{
for (j = 1; j < arrSizes[i]; j++) // I started with 1 bcz i dont want to sort the first value
{
for (k = j + 1; k < arrSizes[i]; k++)
{
if (*(*(p2a + i) + j) > *(*(p2a + i) + k))
{
temp = *(*(p2a + i) + j);
*(*(p2a + i) + j) = *(*(p2a + i) + k);
*(*(p2a + i) + k) = temp;
}
}
i++;
}
}
but it not sorting well...
exampels:
Before: 3 9 3 7
After: 3 3 9 7
Before: 3 6 1 7
After: 3 1 6 7
Before: 4 9 7 1 7
After: 4 9 7 1 7
change the if with for solved the problem
for (i = 0; i < NUM; i++)
{
for (j = 1; j < arrSizes[i]; j++)
{
for (k = j + 1; k < arrSizes[i]; k++)
{
if (*(*(p2a + i) + j) > *(*(p2a + i) + k))
{
temp = *(*(p2a + i) + j);
*(*(p2a + i) + j) = *(*(p2a + i) + k);
*(*(p2a + i) + k) = temp;
}
}
}
}

Resources