HPG
asked on
for loop in 'C'
I'm attempting to figure out how to do this pattern. I'm learning C through a book but it has no answers to its questions. Here the pattern
A
ABA
ABCBA
ABCDCDA
ABCDEDCBA
so far i have done this
int row, space, ascen, desc;
char input, ch;
int str;
printf("Please enter a letter");
scanf("%c", &input);
str=sizeof(input)/2;
for(row='A';row<=input;row ++)
{
for(space=str;space<=input ;space++)
for(ascen=row;ascen<=input ;ascen++)
for(desc=input;desc>row;de sc--)
printf("%c", ch);
}
Obviously this does not work, any pointers at all please!
Thank You
A
ABA
ABCBA
ABCDCDA
ABCDEDCBA
so far i have done this
int row, space, ascen, desc;
char input, ch;
int str;
printf("Please enter a letter");
scanf("%c", &input);
str=sizeof(input)/2;
for(row='A';row<=input;row
{
for(space=str;space<=input
for(ascen=row;ascen<=input
for(desc=input;desc>row;de
printf("%c", ch);
}
Obviously this does not work, any pointers at all please!
Thank You
hmm i just thought of it, last quick part is not quite it, i forgot the spaces but when position = row, then you go back in the array till you get back to your initial letter or something like that
#include <stdio.h>
int i,j,k,l;
void main()
{
for (i=65;i<91;++i)
{
l = 65;
for (j = 65; j < i+1; j++)
{
printf("%c", l++);
}
l=l-2;
for (k = l; k > 64; k--)
{
printf("%c",l--);
}
printf("\n");
}
}
int i,j,k,l;
void main()
{
for (i=65;i<91;++i)
{
l = 65;
for (j = 65; j < i+1; j++)
{
printf("%c", l++);
}
l=l-2;
for (k = l; k > 64; k--)
{
printf("%c",l--);
}
printf("\n");
}
}
Sorry, didn't see the "pyramid spacing" requirement - here it is fixed...
#include <stdio.h>
int i,j,k,l,m;
void main()
{
for (i=65;i<91;++i)
{
for (m = 1; m < 25-(i-65); m++) printf(" ");
l = 65;
for (j = 65; j < i+1; j++)
{
printf("%c", l++);
}
l=l-2;
for (k = l; k > 64; k--)
{
printf("%c",l--);
}
printf("\n");
}
}
#include <stdio.h>
int i,j,k,l,m;
void main()
{
for (i=65;i<91;++i)
{
for (m = 1; m < 25-(i-65); m++) printf(" ");
l = 65;
for (j = 65; j < i+1; j++)
{
printf("%c", l++);
}
l=l-2;
for (k = l; k > 64; k--)
{
printf("%c",l--);
}
printf("\n");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your answers. I chose gi62 as the answer because it works with my compiler. Thanks again. I don't think I would have figured out the answer for a few days yet!
char alphabet[26];
char letter;
char space = " ";
for(int i = 1; i <= 26; i++)
{
alphabet[1] = 'A';
// keep writing till..
alphabet[26] = 'Z';
}
printf("Please enter a letter: ");
scanf("%c", &letter);
/*look for the letter in the alphabet array and save its position*/
while(!(alphabet[i] == letter))
{
if( alphabet[i] == letter)
position = i; /* save position */
else
i++; /* go to next letter */
}
sorry, don't have time to write the rest
but as rows increase, you add 2 columns to each subsequent rows
and you start in the array alphabet at position i and then if you're at row 3 for example very quickly, for(row = 3; row < 26; row++)
for(column = 0; column <= row + 2; column ++)
{
printf alphabet[position]
position++
}
hope it helped