Problem Scanning Integers with Comma Between Them into a 2d Array - c

I am trying to write a C program that accepts a 2d matrix in the form of axb:{{a,b,c},{d,e,f}...} where a determines the number of rows and b determines the number of columns, the subunits {} declaring rows and the elements of rows are declared as a,b,c... between {}. The problem is the program only accepts matrices without commas between the elements so only matrices in the format axb:{{a b c},{d e f}...}} work. I want the program to be able to accept an input with commas between the variables. Here is the code for reference:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a,b;
scanf("%dx%d:{", &a, &b);
int matrix[a][b];
int r,c;
for (r = 0; r < a; r++) {
scanf("{%d", &matrix[r][0]);
for(c = 1; c < (b -1); c++) {
scanf("%d", &matrix[r][c]);
}
scanf("%d},", &matrix[r][c]);
}
printf("%dx%d:{", b, a);
for (c = 0; c < (b - 1); c++) {
printf("{");
for(r = 0; r < (a - 1); r++) {
printf("%d,",matrix[r][c]);
}
printf("%d",matrix[r][c]);
printf("},");
}
printf("{");
for(r = 0; r < 3; r++) {
printf("%d,",matrix[r][c]);
}
printf("%d",matrix[r][c]);
printf("}");
printf("}\n");
return 0;
}

What I would do is scan the whole input and work with that afterwards.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// scan the input matrix
int a, b;
char input[256];
scanf("%dx%d:%256[^\n]", &a, &b, input);
int mat[a][b];
// populate matrix:
char* p = input;
int i = 0;
while (p != input + 256)
{
// scan until the character is not a separator
if (*p == '{' || *p == '}' || *p == ',')
{
++p;
}
else
{
int n;
sscanf(p, "%d", &n);
mat[0][i] = n;
++i;
// scan until we find a separator character
while (p != input + 256 && (*p != '{' && *p != '}' && *p != ','))
{
++p;
}
if (i >= a*b)
break;
}
}
printf("your matrix:\n");
for (i = 0; i < a*b; ++i)
{
if (i % b == 0)
printf("\n");
printf("%d ", mat[0][i]);
}
printf("\n");
return 0;
}
Example input and output:
Input:
3x2:{{a, b},{c, d},{e, f}}
Output:
a b
c d
e f
where a, b, c, d, e and f are numbers (well, integers).
However this might not be an elegant solution, but it does what you'd asked for.
At the least it can give you some ideas. Hope this can be of help.
Note:
When indexing the array I used a single index i. You can do this because arrays like these are stored in an 1D array anyway. However this is just my laziness. Feel free to correct it.
If this is not what you had in mind or I made some mistakes feel free to correct me. Might have misunderstood something.

Related

printing * corresponding to the number of iteration

So in C I'm supposed to let the user input an integer n from the interval [5, 25]. And then, for every number from 1 to n, in a new line print that many stars so it would look something like this:
*
**
***
I tried doing it like this, but it's not working. What am I doing wrong here?
#include <stdio.h>
int main(void)
{
int n, i;
char star = '*';
do {
printf("Input an int from [5, 25]");
scanf("%d", &n);
} while (n < 5 || n >= 25);
for (i=0; i < n; i++){
star += '*';
printf("%c", star);
}
return 0;
}
You cannot write star += '*'; because you declared star as a char, C is strongly typed, a char is a char not a table of char.
You have to use nested loop, like this for example:
#include <stdio.h>
int main(void)
{
int n, i, j;
char star = '*';
do
{
printf("Input an int from [5, 25]");
scanf("%d", &n);
} while (n < 5 || n >= 25);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
You need nested loops
for (int i=0; i < n; i++)
{
for(int j = 0; j <= i; j++)
printf("*");
printf("\n");
}
or if you want to use strings:
char str[n + 1];
for (int i=0; i < n; i++)
{
str[i] = '*';
str[i + 1] = 0;
puts(str);
}
https://godbolt.org/z/aT8brP1ch
The statement
star += '*';
is not the correct way to concatenate two strings in C. In order to do this, you can define an array with sufficient space for the string and use the function strcat, like this:
#include <stdio.h>
#include <string.h>
int main(void)
{
int n;
//initialize "stars" to an empty string
char stars[20] = {0};
do {
printf("Input an int from [5, 25]: ");
scanf("%d", &n);
} while (n < 5 || n >= 25);
//build the string containing the stars using repeated
//string concatentation
for ( int i = 0; i < n; i++ ) {
strcat( stars, "*" );
}
//print the string
printf( "%s\n", stars );
return 0;
}
This program has the following behavior:
Input an int from [5, 25]: 5
*****
However, this is highly inefficient and unnecessarily complicated. Instead of first building the string in an array before printing it out all at once, it is usually easier to simply print it one character at a time:
#include <stdio.h>
#include <string.h>
int main(void)
{
int n;
do {
printf("Input an int from [5, 25]: ");
scanf("%d", &n);
} while (n < 5 || n >= 25);
//print the stars one character at a time
for ( int i = 0; i < n; i++ ) {
putchar( '*' );
}
//end the line
putchar( '\n' );
return 0;
}
This program has the same output as the first program.
You now have the solution for printing out a single line. However, your task involves printing out several lines. This will require a nested loop. In accordance with the community guidelines on homework questions, I will not provide the full solution at this time, as you should attempt to do this yourself, first.
char is an integral type - that is, it represents a number. '*' is a Character Constant, which actually has the type int.
char star = '*';
star += '*';
In ASCII, this is no different from
char star = 42;
star += 42;
A string is a series of nonzero bytes, followed by a zero byte (the null terminating character, '\0'). You cannot build a string by adding two integers together.
To build a string, you must place each byte in a buffer in sequence, and ensure a null terminating byte follows.
#include <stdio.h>
#define MIN 5
#define MAX 25
int main(void)
{
int n;
do {
printf("Input an int from [%d, %d): ", MIN, MAX);
if (1 != scanf("%d", &n)) {
fprintf(stderr, "Failed to parse input.\n");
return 1;
}
} while (n < MIN || n >= MAX);
char buffer[MAX + 1] = { 0 };
for (int i = 0; i < n; i++) {
buffer[i] = '*';
buffer[i + 1] = '\0';
puts(buffer);
}
}
Aside: never ignore the return value of scanf.
Or you can avoids strings, and just print the characters directly.
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++)
putchar('*');
putchar('\n');
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int n,i,j;
printf("enter a number between 5 & 25");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
String concatenation does not work like that in C, instead use strcat().

C: Is there a good way to limit/restrict output from unrestricted scanf string

Is there a good way to limit/restrict output from scanf string? If I test it with one restiction only one restriction like '.' it shows what I expected.
if (b[i] == '.')
{
printf("%c", b[i]);
}
But once I try to add more it doesn't restrict anything anymore.
if (b[i] == '.','-')
{
printf("%c", b[i]);
}
Is there a good way to define what characters are allowed for output after getting an unrestricted input?
#include <stdio.h>
int main(int argc, char *argv[])
{
char b[5];
int i;
printf("enter characters (only first 5 allowed characters will be shown):");
scanf("%s", b);
for(i = 0; i < 5; i++)
{
if (b[i] == '.','-')
{
printf("%c", b[i]);
}
else
{
i++;
}
}
printf("\n");
return 0;
}
if (b[i] == '.','-') acts like:
b[i] == '.'; // Compare and ignore result
if ('-') // Test if '-' is non-zero (which it is)
Clearly not what OP wants.
Instead
if (b[i] == '.' || b[i] == '-') // perform up to 2 compares
or
if (memchr(".=", b[i], 2)) // Test if b[i] is in the 2 char of ".="
or
if (strchr(".=", b[i])) // Test if b[i] is in the 3 char of ".="
This last one is true when b[i] == '\0', so is not quite the same.
If the loop was for(j = 0; b[j]; j++), then the test strchr(".=", b[j]) would not occur when b[j] == '\0', and then strchr(".=", b[j]) would be an equivalent test.
Do not use scanf("%s", b); as it is prone to buffer over-run. Use fgets() or a width limit like scanf("%4s", b);.
Code also needs to separate the indexes used to walk the input string from the count of allowed characters.
#include <stdio.h>
#include <string.h>
int main(void) {
char b[100];
int i = 0;
printf("Enter characters (only first 5 allowed characters will be shown):");
if (fgets(b, sizeof b, stdin)) {
for (int j = 0; b[j] && i < 5; j++) {
if (strchr(".=", b[j])) {
printf("%c", b[j]);
i++;
}
}
}
printf("\n");
return 0;
}

Spaces and adjacent letters

i have 2 exercises that i need to do i did most but i am stuck now .
for the first one i need the pc to detect spaces and if there is to type incorrect input but idk how can i let the pc detect them .
for the second i need it to type if there are two letters adjacent in the alphabet
example 8#a8d?g.## i need it to type ad . another example 7mK#e*gb!c type (eg) plus if they are already types in the first line then type No such two letters
like b?a0a the first line b?a0a should be the answer and second line if there are 2 letters adjacent (they have same answer)so type No such two letters.
the second question should order the alphabet and if there already is 2 letters in order then type them .
first Qs:-
#include<stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
int a, b, c, d, e, f, decimal;
printf("Enter 5 bits");
scanf("%d", &a);
b = a / 10000;
c = a % 10000 / 1000;
d = a % 1000 / 100;
e = a % 100 / 10;
f = a % 10;
if (b <= 1)
if (c <= 1)
if (d <= 1)
if (e <= 1)
if (f <= 1)
{
f = f*1;
e = e*2;
d = d*4;
c = c*8;
b = b*16;
decimal = b + c + d + e + f;
printf(" your decimal value is %d",decimal);
}
else
{
printf("incorrect output");
}
getch();
2) second Question :-
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char string[100];
int length;
int i;
char largest = 96; // Init to a value not in ASCII table
char smallest = 121; // Init to a value not in ASCII table
printf("enter string number ");
scanf("%d", &length);
printf("enter the string ");
scanf("%s", string);
//length = strlen(string);
for (i = 0; i < length; i++) {
if (string[i] >= 'a' && string[i] <= 'z') {
if (string[i] < smallest) {
smallest = string[i];
}
if (string[i] > largest) {
largest = string[i];
}
}
}
for (i = smallest; i <= largest; i++) {
printf("%c", i);
}
printf("\n");
getch();
}
Thanks everyone for your help.

Find combination of groups and letters

I have to find a combination of groups of letters, second letter in first group should be the same as first letter in second group etc.
For example, solution for this group: AA, CB, AC, BA, BD, DB
is this: CB, BD, DB, BA, AA, AC
I have this code so far, it works, but if there is a lot of groups, it takes ages to compute. I need to make it more efficient.
In the input file, there's this input
10
C D
B C
B B
B B
D B
B B
C A
A B
B D
D C
My code
#include <stdio.h>
#include <stdlib.h>
void permutation(char group[][2], int buffer, int sum) {
int i, j;
char temp;
if (buffer == sum && group[1][1] == group[sum][2]) {
for (i = 1; i < sum; i++)
if (group[i][2] != group[i+1][1]) break;
if (i == sum) {
FILE *output;
output = fopen("output.txt", "a");
for (j = 1; j <= sum; j++) {
fprintf(output, "%c %c\n", group[j][1], group[j][2]);
}
exit(1);
}
} else {
for (i = buffer; i <= sum; i++) {
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
temp = group[buffer][2];
group[buffer][2] = group[i][2];
group[i][2] = temp;
permutation(group, buffer + 1, sum);
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
temp = group[buffer][2];
group[buffer][2] = group[i][2];
group[i][2] = temp;
}
}
}
int main() {
FILE *input;
input = fopen("input.txt", "r");
int sum, i;
fscanf(input, "%d", &sum);
char group[sum][2];
for (i = 1; i <= sum; i++) {
fscanf(input, "%s", &group[i][1]);
fscanf(input, "%s", &group[i][2]);
}
permutation(group, 1, sum);
}
EDIT So I have made some changes in my program (thanks to your help, I'm very new to programming so I'm sorry for mistakes), I use permutations no more and I'm just finding path. It works well, but now my input has 100000 groups and it takes a lot of time once again (about 2 hours and I need to make it done in 1 hour in maximal). I will probably have to do that in other way once again xD Any ideas ?
#include <stdio.h>
int find(char group[][2], int buffer, int sum, int path[]) {
int i, j;
for (i = 0; i < sum; i++) {
for (j = 0; j < buffer; j++)
if (path[j] == i)
break;
if (buffer == 0 ||
(group[path[buffer-1]][1] == group[i][0] && buffer == j)) {
printf("%d\n", buffer); // just for me to know what program is currently computing
path[buffer] = i;
find(group, buffer + 1, sum, path);
if (path[sum-1] != 0)
return;
}
}
}
int main() {
FILE *input = fopen("input.txt", "r");
if (input != NULL) {
int sum, i;
fscanf(input, "%d", &sum);
char group[sum][2];
int path[sum];
for (i = 0; i < sum; i++)
fscanf(input, " %c %c", &group[i][0], &group[i][1]);
for (i = 0; i < sum;i++)
path[i] = 0;
find(group, 0, sum, path);
FILE *output = fopen("output.txt", "a");
for (i = 0; i < sum; i++)
fprintf(output, "%c %c\n", group[path[i]][0], group[path[i]][1]);
} else
printf("Input file was not found.");
}
In C array indices start at 0, so an array of size N has valid indices from 0 to N-1. In the code above you are accessing the array group out of bounds, since it has size 2 (valid indices are therefore 0 and 1), yet you are trying to access indices 1 and 2.
Either change:
char group[sum][2];
to:
char group[sum][3];
or use indices 0/1 rather than 1/2.
Note also that your code lacks error checking, e.g. on the call to fopen.
Your program as several issues:
you use 1 based indexing, which causes confusion and leads to referencing arrays and subarrays beyond their defined ends.
you parse the input with fscanf using the %s specifier: this is unsafe and will write 2 bytes for each of your inputs, writing beyond the end of each subarray and beyond the end of the last array.
You already know how to fix these, preferably by using 0 based indexing
Your algorithm is very ineffective, complexity O(n!) because you enumerate all possible permutations and check for validity only on complete permutations. You can drastically improve the performance by only enumerating permutations which already verify the constraint for their initial elements. The complexity is substantially lower, still quadratic but n is quite small.
Here is a modified version of your code that does this:
#include <stdio.h>
int permutation(char group[][2], int buffer, int sum) {
if (buffer == sum)
return group[sum-1][1] == group[0][0];
for (int i = buffer; i < sum; i++) {
if (group[buffer-1][1] == group[i][0]) {
char temp = group[buffer][0];
group[buffer][0] = group[i][0];
group[i][0] = temp;
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
if (permutation(group, buffer + 1, sum))
return 1;
temp = group[buffer][0];
group[buffer][0] = group[i][0];
group[i][0] = temp;
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
}
}
return 0;
}
int main(void) {
FILE *input = fopen("input.txt", "r");
int sum, i;
if (input != NULL) {
if (fscanf(input, "%d", &sum) != 1 || sum <= 0) {
printf("invalid number of pairs\n");
fclose(input);
return 1;
}
char group[sum][2];
for (i = 0; i < sum; i++) {
if (fscanf(input, " %c %c", &group[i][0], &group[i][1]) != 2) {
printf("incorrect input for pair number %d\n", i);
fclose(input);
return 1;
}
}
fclose(input);
if (permutation(group, 1, sum)) {
FILE *output = fopen("output.txt", "a");
if (output == NULL) {
printf("cannot open output file\n");
return 2;
}
for (i = 0; i < sum; i++) {
fprintf(output, "%c %c\n", group[i][0], group[i][1]);
}
fclose(output);
return 0;
} else {
printf("complete path not found\n");
return 1;
}
}
printf("cannot open input file\n");
return 2;
}
I modified other aspects of the code to improve efficiency and reusability:
input is checked for validity.
the recursive function stops and returns 1 when it finds a complete path. This allows the program to continue whether it found the path or not.
output is handled from the main function for consistency.
The above code solves the problem for the specified input with n=50 in less than 0.002 seconds on my laptop. It prints F C C E E F F E E E E E E E E E E B B F F E E A A F F C C A A A A E E F F C C E E E E E E E E E E B B C C E E E E F F E E F F F F E E C C E E E E E E B B F F A A D D A A C C C C E E E E E E B B D D F
EDIT I realized that, since you are looking for a full closed path, you do not need to try different possibilities for the first pair. main can call permutation with 1 instead of 0 and the permutation can be simplified as buffer can never be 0.
Your new code has some problems:
find is defined asa returning int, but you return nothing. You indeed do not test if you have found a complete path, fully relying on the assumption that there is at least one and that you have found it.
You do not test for path closure. You may find a closed path by chance, but you may also produce an unclosed path.
Using 2 loops to find the unused pairs is less efficient than using a temporary array used[sum].
The first pair is always the first, so you can simplify the find function a little.
Here is an improved version:
#include <stdio.h>
int find(char group[][2], int buffer, int sum, int path[], unsigned char used[]) {
int i;
char last = group[path[buffer-1]][1];
if (buffer == sum)
return last == group[0][0];
for (i = 1; i < sum; i++) {
if (!used[i] && last == group[i][0]) {
path[buffer] = i;
used[i] = 1;
if (find(group, buffer + 1, sum, path, used))
return 1;
used[i] = 0;
}
}
return 0;
}
int main() {
FILE *input = fopen("input.txt", "r");
if (input != NULL) {
int sum = 0, i;
fscanf(input, "%d", &sum);
char group[sum][2];
int path[sum];
unsigned char used[sum];
for (i = 0; i < sum; i++)
fscanf(input, " %c %c", &group[i][0], &group[i][1]);
path[0] = 0; // always start at first element
used[0] = 1;
for (i = 1; i < sum; i++)
used[i] = 0;
if (find(group, 1, sum, path, used)) {
FILE *output = fopen("output.txt", "a");
for (i = 0; i < sum; i++)
fprintf(output, "%c %c\n", group[path[i]][0], group[path[i]][1]);
}
} else {
printf("Input file was not found.");
}
return 0;
}
EDIT: I tested this new version with your large input file: it crashes on my laptop. The previous version with the permutation functions works like a charm, producing the complete path in 0.060 seconds. So there is a complete path and something is wrong with this find function.
There are few differences between the algorithms:
permutation uses less stack space: a single automatic array of size n*2 (200k) versus 3 automatic arrays total size n*(sizeof(int) + 3) (700k).
permutation uses fewer variables, so recursion uses less stack space, but both probably use more than 1 MB of stack space to recurse 100000 times.
find does more scans, where permutation swaps group pairs and always snaps the next one directly.
I reimplemented find without recursion and finally got it to produce a complete path. It is a different one and it takes much longer to compute, 3.5 seconds.
For larger input files, you definitely should not use recursion and you should even allocate the arrays from the heap with malloc.
Here is the non recursive code, using heap memory:
#include <stdio.h>
#include <stdlib.h>
int find(const char group[][2], int sum, int path[]) {
path[0] = 0;
if (sum <= 1)
return group[0][1] == group[0][0];
unsigned char *used = calloc((size_t)sum, sizeof(*used));
for (int buffer = 1, i = 1;; i++) {
if (i == sum) {
--buffer;
if (buffer == 0) {
free(used);
return 0;
}
i = path[buffer];
used[i] = 0;
} else
if (!used[i] && group[path[buffer-1]][1] == group[i][0]) {
path[buffer] = i;
if (buffer == sum - 1) {
if (group[i][1] == group[0][0]) {
free(used);
return 1;
}
} else {
buffer++;
used[i] = 1;
i = 0;
}
}
}
}
int main() {
FILE *input = fopen("input.txt", "r");
if (input != NULL) {
int sum = 0, i;
fscanf(input, "%d", &sum);
char (*group)[2] = calloc((size_t)sum, sizeof(*group));
int *path = calloc((size_t)sum, sizeof(*path));
for (i = 0; i < sum; i++)
fscanf(input, " %c %c", &group[i][0], &group[i][1]);
if (find(group, sum, path)) {
FILE *output = fopen("output.txt", "a");
for (i = 0; i < sum; i++)
fprintf(output, "%c %c\n", group[path[i]][0], group[path[i]][1]);
}
} else {
printf("Input file was not found.");
}
return 0;
}

Printing a diamond in C, with mirrored letters

I tried to write a program that prints a diamond with mirrored letters like the following shape:
a
aba
abcba
abcdcba
abcdedcba
abcdefedcba
abcdedcba
abcdcba
abcba
aba
a
here is what I have done already:
#include <stdio.h>
int main()
{
int n, c, k,f=0, space = 1;
char ch='a';
printf("enter the size of diamond\n");
scanf("%d", &n);
space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2*k-1; c++)
if (c <= f)
printf("%c", ch);
ch++;
if (c>f)
ch--;
printf("%c", ch);
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("a");
printf("\n");
}
return 0;
}
But apparently there is some error in this code, can any one help me detect it?
If you replace this
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("a");
with this
for (c = 0 ; c < 2*(n-k)-1; c++) // note the different conditions
printf("%c", 'a' + c); // print char instead of string
you should get each line of the correct length but with increasing chars like
abcde
but it doesn't decrease from the midpoint. I leave that to you!
It would be easier to separate tasks to function. So the function to calculate the character to be printed based on the position and the line length would be:
int getCharAtPos(int pos, int len)
{
if (pos > len / 2)
{
return 'a' + len - pos;
}
else
{
return 'a' + pos - 1;
}
}
And then instead of printf("a") you would use
for (c = 1; c <= 2*k-1; c++)
printf("%c", getCharAtPos(c, 2*k-1));
.....
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("%c", getCharAtPos(c, 2*(n-k)-1));
There is a second solution of the problem.
You can use recursion.
I wrote an example for your task.
//--------------
// Recursive Out one Simbol
void DamSimRec(char chOut ,int scnt)
{
printf("%c", chOut);
if (scnt > 1)
{
DamSimRec(chOut + 1, scnt - 1);
printf("%c", chOut);
}
}// Print Space
//--------------
void SpaceOut(int pSizeSpace)
{
int a_c;
for (a_c = 0; a_c < pSizeSpace; a_c++)
printf(" ");
}
//--------------
// Recursive Print One String Krylov E.A.
void DamRec(int space, int sout)
{
SpaceOut(space);//space;
DamSimRec('a', sout);//Simbols
printf("\n");
if (space > 0)
DamRec(space-1, sout+1);
if (sout > 1)
SpaceOut(space + 1);
DamSimRec('a', sout - 1);
}
printf("\n");
}
main()
{
int aSize;
printf("enter the size of diamond\n");
scanf_s("%d", &aSize);
DamRec(aSize , 1);
}
...So, You can use that, but remember about stack.
int n, c, k, space;
printf("enter the size of diamond\n");
scanf("%d", &n);
char s[n+2];
s[0] = 'a';s[1] = '\0';
for(c = 0; c < n; ++c){
space = n - c -1;
printf("%*s", space, "");
k = printf("%s", s) - 1;
while(--k >=0)
putchar(s[k]);
puts("");
s[c+1] = s[c] + 1;
s[c+2] = '\0';
}
for(c = 1; c < n; ++c){
space = c;
printf("%*s", space, "");
k = printf("%.*s", n - c, s) - 1;
while(--k >=0)
putchar(s[k]);
puts("");
}
#include <stdio.h>
void print(char from, char to){
putchar(from);
if(from < to){
print(from+1, to);
putchar(from);
}
}
int main(void){
int c = 0, n, k = 1;
printf("enter the size of diamond\n");
scanf("%d", &n);
do{
printf("%*s", n - c -1, "");
print('a', 'a' + c);
puts("");
if(c + 1 == n)
k = -k;
c += k;
}while(c>=0);
return 0;
}

Resources