how to print the below triangle without using arrays
a
a b
a e i
a b c d
a e i o u
use a loop.
>use a loop.
Obviously. Now do it in an elegant way. ;)
But sure that is your output...............
use multiple loops for this example, use two for , while or do while statements.
Best is for loop, as there is possible initial condition, predetermined no of possible iterations required, just check the pattern for the character yourself and make the necessary increment value.
-Manoj
Hi did you have any predefine text, which you want to print in following way
a
ab
abc
abcd
for(r=0;r<no_row;r++)//for row
for(c=0;c<r+1;c++)//for column
There are two different alternate sequences in your output.
For the one with "ab", "abcd"....... sequence you can simply use a single character (ch = 'a') in loop like this :
for (i = 0; i < no_of_times_you_want; i++)
{
for (j = 0; j < i*2; j++)
{
printf("%c", ch+j);
}
printf("\n");
}
For the other sequence, I suppose it is vowels, but that is supposed to be the end of it (a e i o u). Moreover, it doesn't really follow any sequence and so can't be printed with character.
Check out the sequence correctly and print accordingly.
Not only are there two diferent sequences, but they need to be printed alternately. Printing all the letters abcd together will not solve the problem.
I suppose there are more than one way to solve the problem, the solution I wrote requires three loops, the mod operator, and the switch/case statements. I'm not going to post my solution until after the OP has solve it himself. But it is not quite as simple as first assumed.
Actually this is a question from computer management Question paper,
But i feel the use of array is inevitable
Nope. An array is not needed.
Actually this is a question from computer management Question paper,
But i feel the use of array is inevitable
Yes, it is clear that if that computer management Question paper, may be testing you for the looping skills or may be for something else God knows.
But if they had made it clear not to use array, then only way to achieve result is by iteration ( simply by finding any pattern here.):sad:
My analysis for this problem ( infact pattern ) is as under:-
a 1
ab 1 2
aei 1 5 9
abcd ----> 1 2 3 4
aeiou 1 5 9 15 21
This is their alphabetical sequence in English and starting point for me to get my incremental value for FOr Loop.
study 1:
There may be two but for me there are three sequences. This will make the logic better if done properly, but will
make code design a little challenging a little bit.
Study 2:
My analyse for sequence 1: it is a character in the first line. it is seperate sequence because every other
sequence in this pattern has two charcters added to it.
My analyse for sequence 2: ab and abcd here increment of one is added.
My analyse for sequence 3: aei and aeiou here increment is 4 for first iteration and 6 for second iteration.
Study 3:sweat::
Way 1: My suggestion for make is : Make the first outer loop for two iterations and have three inner loop. Make first loop
die prematurely on completion of first iteration through some condition checking. For second iteration, increment is 1.
for third iteration, use a variable to store with initial 4 and increment that with 2 in each iteration.
Way 2: My suggestion 2 for this loop is : Make first iteration outside, ya! just print a character. rest is same as above.
Try to solve this if you get my point. I will try to solve the logical part in your program.
But, I am a little busy right now and may help you only in 12- 14 hours.:sad:
See you in 12 - 24 hours.
Attachment : coding.txt for you to get charcter sequence.
-Manoj
a 1
ab 1 2
aei 1 5 9
abcd ----> 1 2 3 4
aeiou 1 5 9 15 21
This is the only logic i could derive.
Pls tell if anything better could be done
void main()
{
char a = 'a';
int i,j,l=0;
clrscr();
for(i=1;i<=6;i++,printf("\n"))
{
for(j=0;j<i;j++)
{
if(i%2==0)
printf("%c ",a+j);
else
{
printf("%c ",a+=l);
if(i>2 && j>=2)
l=6;
else
l=4;
}
}
a = 'a';
l=0;
}
}
My analyse for sequence 1: it is a character in the first line. it is seperate sequence because every other
sequence in this pattern has two charcters added to it.
Wrong !
Both the sequences are adding two more characters in every cycle.
a -> aei -> aeiou
ab -> abcd ->
The only difference among the two is the starting point (first sequence starts with one letter and the second two).
That it is print it alternatively where the final output has an increment of one character in each step.
i will try .i will say within 24 hours.
according to me the logic is as follows..
using 1 loop for printing
ab
abcd
and using another loop for printing
a
aei
aeiou
this can be achieved through boolean value conditions in java
and
an other idea is to use ascii value of characters in the for loop..
ya now i got it..
to print a e i:
ascii value of 'a'=97(say)b=98c=99d=100
ascii value of 'e'=97+4=101
ascii value of 'i'=(97+4)+4=105 o=105+6=111 u=(105+6)+6=117
97
97 98 dif:1
97 101 105 dif:3
97 98 99 100 dif:1
97 101 105 111 117 dif:3 from 97 to 105 and diff is 6 from 105 to 117
this is the logic apply them in for loop .
the numbers above should be declared as int .but when printing(inside 'for') convert it to char by
printf("%c",num);
try this pa if works,reply me
Well sakthi i would say that the efficiency was what i was looking for
there are numerous ways. the one which u said would uses 3 nested loops
which would again bring a linear growth in algorithm (n^6 times). the previous i posted was also linear but only 2n iterations. if there is a shorter version and efficient i would be interested.
oh sorry
now only i executed your program..
i got answer..
i thought tat u asked for answer...
If uwe use a array for sequence (a e i o u) I think code are more easy........
#include<iostream.h>
int main()
{
char vow_array[5]={'a','e','i','o','u'};
for(int i=1;i<=6;i++)
{
char ch='a';
for(int j=0;j<i;j++)
{
if((i%2)==0)
cout<<ch++<<" ";
else
cout<<vow_array[j]<<" ";
}
cout<<endl;
}
system("PAUSE");
}
>If uwe use a array for sequence (a e i o u) I think code are more easy........
If you had bothered to read the original question, arrays are explicitly prohibited.
I'ts been almost two weeks now since the original post, so here is my solution if anyone is interested
Note that every other line that is printed is either a consonants (abcdef ...) or a vowel (aeiou).
#include <stdio.h>
int main()
{
int seq = 0;
int i;
for(i = 1; i < 6; i++, seq++)
{
if( (seq % 2) != 0)
{
// print series of consonants
char c = 'a';
for(int j = 0; j < i; j++)
{
putchar(c);
++c; // increment the letter
}
putchar('\n');
}
else
{
// print a series of vowels. Ignore the fact that y can sometimes
// be a vowel.
for(int j = 0; j < i; j++)
{
char c = 'z';
switch(j)
{
case 0: c = 'a'; break;
case 1: c = 'e'; break;
case 2: c = 'i'; break;
case 3: c = 'o'; break;
case 4: c = 'u'; break;
}
putchar(c);
}
putchar('\n');
}
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.