pattern

hello . i need help in this program
A
A B
A B C
A B C D
A B C D E
A B C D E F

plz give me answer i tried it . but i only get
A
BC
DEF
If you show the code you have so far, we may be able to identify the problem.
#incluude<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch = 'A' ;
int a , i , j ;
cout<<"enter no of rows"';
cin>>a;
for(i=1;i<=a;++i)
{
for(j=1;j<=i;++j)
{
cout<<ch;
ch=ch+1;
}
cout<<endl;
}
getche();
}
That was very close. You just need to re-set the value of ch each time at the start of the outer loop.
1
2
3
4
5
6
7
8
9
10
11
    for (i=1; i<=a; ++i)
    {
        char ch = 'A';    // This line moved inside the loop.

        for (j=1; j<=i; ++j)
        {
            cout << ch;
            ch++;
        }
        cout<<endl;
    }


Other comments, the include should be #include <iostream> without the .h and main should be declared as int, not void, int main(). (Perhaps you are using an outdated compiler, I'd recommend using a current version).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
	while ( true )
	{
		std::cout << "Enter a non-negative number (0 - exit): ";

		size_t n = 0;
		std::cin >> n;

		if ( !n ) break;

		std::cout << std::endl;
		for ( size_t i = 1; i <= n; i++ )
		{
			for ( unsigned char c = 'A'; c < 'A' + i; c++ ) std::cout << c;
			std::cout << std::endl;
		}
	}
}
can you tell me latest compiler . i am still using turbo c++ with the help of dosbox and thanks for the solution .
Last edited on
You can download MS Visual Studio 2012 Express Edition from the Microsoft site.
Also you can check code online at www.ideone.com choising C++11.
Last edited on
thanks
> ch=ch+1;

There is no guarantee that 'A' + 1 will always evaluate to 'B' (or that it will never result in undefined behaviour).
It's easier to learn the basics early, rater than unlearn things at a later stage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    const int N = sizeof(alphabet) - 1 ;

    int nrows = 0 ;
    std::cout << "number of rows? " ;
    std::cin >> nrows ;
    if( nrows > N ) nrows = N ;

    for( int i = 0 ; i < nrows ; ++i )
    {
        for( int pos = 0 ; pos <= i ; ++pos ) std::cout << alphabet[pos] ;
        std::cout << '\n' ;
    }
}
There is no guarantee that 'A' + 1 will always evaluate to 'B' (or that it will never result in undefined behaviour).


Its certainly defined. 'A' is represented by an integral value, and 'A'+1 simply increases that integral value by 1.

Whether or not 'A'+1=='B' depends on your target system's language/locale... but I'm pretty sure every single target out there has the basic English alphabet ordered numerically.

So you're right that there's no guarantee that 'A'+1=='B'... but kinda?
Last edited on
> Its certainly defined. 'A' is represented by an integral value, and 'A'+1 simply increases that integral value by 1.

Its not 'certainly' defined.
There is no guarantee that the default char is unsigned.
Nor is there a guarantee that int('A') has a numeric value less than std::numeric_limits<int>::max()
There is a guarantee that sizeof(int) >= sizeof(char)
There is also a guarantee that 'A' is a char

Therefore the only way that int('A') would be > int's maximum is if all of the following are true:

1) char is unsigned
2) int is signed
3) sizeof(char) == sizeof(int)
4) Your target system has an extremely insane locale setting where basic english glyphs are assigned insanely high codepoint values (read: as far as I'm aware, such a locale does not exist).


So I guess technically you're right... in the same sense that technically (1<<2) == 4 is not guaranteed.
> Therefore the only way that int('A') would be > int's maximum is

int('A') (or any int for that matter) can never be grater than the maximum value that an int can hold.


> Therefore the only way...
> 1) char is unsigned
> 2) int is signed
> 3) sizeof(char) == sizeof(int)

This case raises no issues of undefined bahaviour for some_char + 1 here. Integral promotions in C++ have always been value-preserving; under those conditions, a char would be promoted to an unsigned int.

This is the case where there could be undefined behaviour for some_char + 1:
1) char is signed
2) sizeof(char) == sizeof(int)


> in the same sense that technically (1<<2) == 4 is not guaranteed.

(1<<2) == 4 is guaranteed since std::numeric_limits<unsigned char>::digits is guaranteed to be >= 8.
The results are implementation-defined for right-shifts of signed values.
int('A') (or any int for that matter) can never be grater than the maximum value that an int can hold.


Right. What I was getting at is that the only way for int_min <= 'A' <= int_max to be false was a combination of all of those conditions.

This case raises no issues of undefined bahaviour for some_char + 1 here. Integral promotions in C++ have always been value-preserving; under those conditions, a char would be promoted to an unsigned int.


Yes, but casting an unsigned int to a signed int where the value is > int_max is implementation dependent.

Which is why I listed #4 among my list of requirements for this to be an undefined operation. 'A' would have to be > int_max




That said.... what are you getting at here? You were the one who said he was doing something undefined. I proceed to show how it is undefined... and you turn around and say "no, that's clearly defined".

So what exactly about his code is undefined?


(1<<2) == 4 is guaranteed since std::numeric_limits<unsigned char>::digits is guaranteed to be >= 8


The implementation of how numerical values are represented in binary is not dictated by C++.

(1<<2) takes the binary representation of the literal 1 and shifts it left 2 places. That will only == 4 if the resulting binary number matches the binary representation of the literal 4.

It is true for 2s compliment and 1s compliment integers... but if the target machine is using some kind of insane representation it may not be true. Of course the likelyhood of that is extraordinarily low... just as the likelyhood of 'A'-'Z' not being sequential is extraordinarily low... which was my point.
Last edited on
> What I was getting at is that the only way for int_min <= 'A' <= int_max to be false was a combination of all of those conditions.

You are wrong about the 'only way'.
For int_min <= 'A' <= int_max, that char is unsigned is neither a necessary or a sufficient condition.
int_min <= 'A' <= int_max is guaranteed if and only if the default char is signed.


> You were the one who said he was doing something undefined.

I did not say that 'he was doing something undefined'.
I said that 'there is no guarantee that it will never result in undefined behaviour'.


> I proceed to show how it is undefined...

What you showed and what you believed to be undefined bahaviour is in fact not undefined behaviour.

If the default char is unsigned, and sizeof(char) == sizeof(int), a char is promoted to an unsigned int;
some_char + 1 does not result in undefined behaviour.

On the contrary, if the default char is signed, and sizeof(char) == sizeof(int), a char is promoted to an int;
some_char + 1 can result in undefined behaviour.


> The implementation of how numerical values are represented in binary is not dictated by C++.

The C++ standard imposes strict requirements on how integral types are to be represented.

The representations of integral types shall define values by use of a pure binary numeration system. (Footnote: A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position.)

[ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. — end example ] - IS



> (1<<2) takes the binary representation of the literal 1 and shifts it left 2 places.
> That will only == 4 if the resulting binary number matches the binary representation of the literal 4.
> It is true for 2s compliment and 1s compliment integers...
>but if the target machine is using some kind of insane representation it may not be true.

This is always true (in C++).

The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. Otherwise, if E1 has a signed type and non-negative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. - IS


In this specific case, (1<<2), 1*22 is representable in the result type, and that is guaranteed to be the resulting value.
You are wrong about the 'only way'.
For int_min <= 'A' <= int_max, that char is unsigned is neither a necessary or a sufficient condition.
int_min <= 'A' <= int_max is guaranteed if and only if the default char is signed.


What you showed and what you believed to be undefined bahaviour is in fact not undefined behaviour.

I'm not following. Here's my thought process and maybe you can point out where my mistake is:

1) char_min <= 'A' <= char_max must be true because 'A' is a char.

2) int is guaranteed to be as big or bigger than a char. This means that: signed_int_min <= signed_char_min and signed_int_max >= signed_char_max

3) Therefore, the only way for char_max > int_max to be true is if int is signed, and char is unsigned.

4) But that alone is not the only restriction, as any unsigned char value below int_max can be safely cast to an int. Therefore 'A' > int_max must also be true.


Therefore my original conclusion:
- char is unsigned (per #3)
- int is signed (per #3)
- sizeof(char) == sizeof(int) (per #2)
- Target system locale has to be weird (per #4)


EDIT: I just realized none of that matters because I think we were talking about 2 different things.

On the contrary, if the default char is signed, and sizeof(char) == sizeof(int), a char is promoted to an int;
some_char + 1 can result in undefined behaviour.


Yes but no locale in the world has 'A' == char_max, that I know of anyway. 'A'-'Z' are stored sequentially pretty much universally (barring reaaally old systems whose software had custom tables for their text)


The C++ standard imposes strict requirements on how integral types are to be represented.


Ah! I didn't realize that. I stand corrected. Thank you.
Last edited on
how about we use floats instead of ints then we will really have undefined behavior.
1
2
3
char ch( 'A' );
ch += 1.0;
//ch += 1f; 
@giblit

how about we use floats instead of ints then we will really have undefined behavior


I have not understood why we will use floats instead of integers for this simple program.
According to the C++ Standard
For each basic execution character set, the values of the members shall be non-negative and distinct from one another.
So even if you are using the type char instead of unsigned char all members from 'A' to 'Z' are non-negative values.
Last edited on
Topic archived. No new replies allowed.