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.
Related
In the code attached, how do I modify it to remove Remove the trailing '+' signs.
int i,j,sum;
sum=1;
for(i=2; i<=10; i++) {
for(j=1; j<(i+1); j++) {
sum = sum + 1;
printf("%d + ",j);
}
printf(" = %d", sum);
printf("\n");
}
return EXIT_SUCCESS;
}
Here is the output:
1 + 2 + = 3
1 + 2 + 3 + = 6
1 + 2 + 3 + 4 + = 10
1 + 2 + 3 + 4 + 5 + = 15
1 + 2 + 3 + 4 + 5 + 6 + = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 + = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + = 45
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + = 55
For example you can do it the following way
for(j=1; j<(i+1); j++) {
sum = sum + 1;
if ( j != 1 ) printf( " + " );
printf("%d",j);
}
You can't 'remove' output; you have to avoid generating it.
One way is to use:
for (int i = 2; i <= 10; i++)
{
int sum = 0;
const char *pad = "";
for (int j = 1; j <= i; j++)
{
sum += j;
printf("%s%d", pad, j);
pad = " + ";
}
printf(" = %d\n", sum);
}
Note that this recalculates sum more directly, setting it to zero before the inner loop. It also minimizes the scope of the variables.
You can set and print the initial value in the outer loop. For my opinion also make it more readable.
Furthermore you can use j instead of sum+1 for the addend
for (int i = 2; i <= 10; i++) {
int sum = 1;
printf("%d", sum);
for (int j = 2; j<(i + 1); j++) {
sum += j;
printf(" + %d", j);
}
printf(" = %d", sum);
printf("\n");
}
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!
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");
}
I have to make a Floyd Triangle that prints in this order:
7 8 9 10
4 5 6
2 3
1
But currently my code print like this:
1
2 3
4 5 6
7 8 9 10
CODE:
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a);
a++;
}
printf("\n");
}
return 0;
}
Can someone help me?
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
int width = 0;
for ( unsigned int tmp = n * ( n + 1 ) / 2; tmp != 0; tmp /= 10 )
{
++width;
}
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = ( n - i ) * ( n - i + 1 ) / 2 - ( n - i - 1 );
for ( unsigned int j = 0; j < n - i; j++ )
{
printf( "%*u ", -width, value++ );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 10
46 47 48 49 50 51 52 53 54 55
37 38 39 40 41 42 43 44 45
29 30 31 32 33 34 35 36
22 23 24 25 26 27 28
16 17 18 19 20 21
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 4
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 0
I solved it by hacking (i.e. trying different things and adjusting the code based on the output):
I started by moving the generation code to a separate function. I then cut and pasted the function, giving it a different name. I then started modifying the for loops.
I went through four versions until I hit the correct one:
#include <stdio.h>
#include <stdlib.h>
void
fwd(int n)
{
int i, c;
int a = 1;
for (i = 1; i <= n; i++) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev1(int n)
{
int i, c;
int a = 1;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev2(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", a);
a--;
}
printf("\n");
}
}
void
rev3(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", (c - i) + a);
a--;
}
printf("\n");
}
}
void
rev4(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", (c - i) + a);
}
a -= i;
printf("\n");
}
}
int
main(int argc,char **argv)
{
int n;
--argc;
++argv;
if (argc > 0)
n = atoi(*argv);
else
scanf("%d", &n);
printf("fwd:\n");
fwd(n);
printf("\nrev1:\n");
rev1(n);
printf("\nrev2:\n");
rev2(n);
printf("\nrev3:\n");
rev3(n);
printf("\nrev4:\n");
rev4(n);
return 0;
}
Here's the program output:
fwd:
1
2 3
4 5 6
7 8 9 10
rev1:
1 2 3 4
5 6 7
8 9
10
rev2:
10 9 8 7
6 5 4
3 2
1
rev3:
10 8 6 4
6 4 2
3 1
1
rev4:
7 8 9 10
4 5 6
2 3
1
The other answers are great, but no one posted a recursive function so I thought I'd add one.
#include <stdio.h>
void recursive(int n, int i);
void straight(int n);
// one way of doing it with a recursive function
void recursive(int n, int i) {
if(n <= 0)
return;
if(i <= n) {
printf( "%-3d", (((n * (n + 1) / 2) - n) + i) );
recursive(n, i + 1);
} else {
printf("\n");
recursive(n - 1, 1);
}
}
// straightforward nested loop way
void straight(int n) {
int i, j, row_sum;
for(i = n; i > 0; --i) {
// the sum: i + (i-1) + (i-2) + ... + 2 + 1 = (i * (i+1)) / 2
row_sum = (i * (i + 1)) / 2;
for(j = i; j > 0; --j) {
printf("%-3d", row_sum - j + 1);
}
printf("\n");
}
}
// entry point
int main(int argc, char **argv) {
int n = 0;
scanf("%d", &n);
printf("Recursive Output for n=%d\n", n);
recursive(n, 1);
printf("\nStraight Output for n=%d\n", n);
straight(n);
return 0;
}
Output:
Recursive Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Straight Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
I have a task to get a positive integer from the user, and then print a triangle that will look like this (for the integer 5 for example):
1
1 2
1 3 5
1 4 7 10
1 5 9 13 17
this is the code that I managed to make:
int num, line,addition,sum,termNum;
printf("Enter a positive integer: ");
scanf("%d",&num);
printf("%d\n",1);
for (line=2;line<=num;line++)
{
addition = line-1;
termNum=1;
printf("%d ",termNum);
for (sum=2;sum<=line;sum++)
{
termNum+=addition;
printf("%d ",termNum);
}
printf("\n");
}
But the output is not aligned to the right, it looks like this:
1
1 2
1 3 5
1 4 7 10
1 5 9 13 17
I cant create functions or use arrays, only loops and ifs, and the various control characters such as %c %s %d %-12c etc...
Variable types must be int,double,char. (not strings or char* etc)
Any ideas?
Thanks.
OP a needs nudge to pre-calculate maximum line width.
Below fill-in-the-blank code calculates the max line width, which seem to be OP's stumbling block.
int main(void) {
int x = 5;
int width = 0;
for (int col = 0; col < ___ ; col++) { // How many iterations?
int value = col* ___ + ___; // Determine each value: 1 5 9 13 17
// find print width
do {
width++; // add 1 per digit
value /= ___; // What base is the number displayed?
} while (value > ___); // When to quit (hint: not much)
width++; // add 1 for ' ' separator
}
printf("%d\n", width); // Should be a dozen
}
Alternative
for (int col = 0; col < ___ ; col++) {
int value = col* ___ + ___;
width += snprintf(NULL, 0, "%d ", value);
}
Maximum printed number will be the very last number. That is line
num and column num. It will thus be 1 + (num - 1) ^ 2.
The printed width of a decimal number N is ceil(log(N+1) / log(10)).
The easiest way to align your numbers is to print them all the same
width, that is the width of the maximum number:
ceil(log(1 + (num - 1) ^ 2) / log(10)).
int num, line, addition, sum, termNum;
printf("Enter a positive integer: ");
scanf("%d", &num);
int width = ceil(log10(1 + 1 + (num - 1) * (num - 1)));
char format[] = "%0000000000d%c";
sprintf(format, "%%%dd%%c", width);
for (sum = 1 + 1; sum <= num; sum++) {
int i;
for (i = 0; i <= width; i++)
putchar(' ');
}
printf(format, 1, '\n');
for (line = 2; line <= num; line++) {
for (sum = line + 1; sum <= num; sum++) {
int i;
for (i = 0; i <= width; i++)
putchar(' ');
}
addition = line - 1;
termNum = 1;
printf(format, termNum, ' ');
for (sum = 2;sum <= line; sum++) {
termNum += addition;
printf(format, termNum, ' ');
}
printf("\n");
}