Need help with a simple program

K

kfeder

Im new to C programming i wrote a simple program that displays A - F.
Im having a problem displaying F to A. could someone help me .

include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHARS 6
main()
{
int row;
char ch;

for (row = 0; row <= 6; row++)
{
for (ch = ('A' + row ); ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf("\n");
}
system("pause");

}

output
ABCDEF
BCDEF
CDEF
DEF
EF
F
 
M

Michael Mair

kfeder said:
Im new to C programming i wrote a simple program that displays A - F.
Im having a problem displaying F to A. could someone help me .

I do not understand what kind of output you are trying to achieve.
Please state this clearly.
include <stdio.h>
#include said:
#include <stdlib.h>
#include <string.h>
#define CHARS 6

int main (void)
{
int row;
char ch;

for (row = 0; row <= 6; row++)

Note: You probably want row < 6; more specifically, you want
row < CHARS -- otherwise introducing CHARS makes no sense.
{
for (ch = ('A' + row ); ch < ('A' + CHARS); ch++)
printf("%c", ch);

Use proper indentation or braces to make clear what is
happening.
Note: C does not require 'A', 'B', 'C', 'D', 'E', and 'F' to be
consecutive values (it only requires 'A' < 'B', 'B' < 'C', and
so on). If you want to have them consecutive, use a char array
containing them in order, e.g.
char letters[] = "ABCDEF";
and
for (index = row; index < CHARS; index++) {
ch = letters[index];
printf("%c", ch);
}
printf("\n");
}
system("pause");

Note that while system is a standard C function, its use
is not portable.

return 0;
}

output
ABCDEF
BCDEF
CDEF
DEF
EF
F

What is it you are trying to achieve?
FEDCBA
EDCBA
.....
or
FEDCBA
FEDCB
.....
or
???
 
W

Walter Roberson

Im new to C programming i wrote a simple program that displays A - F.
Im having a problem displaying F to A. could someone help me .

You've shown us the output that is in error, but you have not
really indicated what you want the output to look like.
include <stdio.h>

The leading '#' disappeared somewhere on that line.
#include <stdlib.h>
#include <string.h>

You do not use anything from string.h in the program you show.
#define CHARS 6
main()

int main(void)
{
int row;
char ch;

for (row = 0; row <= 6; row++)

You have 6 characters to deal with, which are going to be at
offsets 0, 1, 2, 3, 4, and 5, but your termination condition is
such that you are also going to execute the loop for the row
offset 6. It is not clear to us whether that is a mistake or
a deliberate outputting of the empty line?
{
for (ch = ('A' + row ); ch < ('A' + CHARS); ch++)
printf("%c", ch);

It would be clearer if you indented that printf()

Your code here assumes that the characters 'A' through 'F' are
contiguous. All that C promises is that '0' through '9' are
contiguous. It would be legal in C for the underlying character
set to run something like AQ*v/ba in which case your first
line of output would be AQ*v/b instead of the ABCDEF that you hoped for.

There really is no good hack that you can use to get around this,
so the easiest way to deal with it is to precreate a string that
has the characters in the order you want them:

char hexes[] = "ABCDEF";

Then your logic becomes something like

for (row=0; row < strlen(hexes); row++) {
for( offset=row; offset < strlen(hexes); offset++ ) putchar(hexes[offset]);
putchar('\n');
}


As for the reverse direction, I will give the hint:

putchar(hexes[strlen(hexes)-offset-1])

printf("\n");
}
system("pause");

You should return a value from main() or use exit(), so that the
operating system does not get confused about whether the program
worked or failed.
 
W

Walter Roberson

Michael Mair said:
Note: C does not require 'A', 'B', 'C', 'D', 'E', and 'F' to be
consecutive values (it only requires 'A' < 'B', 'B' < 'C', and
so on).

Where do you find the requirement that the execution character
set is monotonically increasing? What does your reference say
about the relative values of 'A' and 'a' ? How does the
clause you are thinking of interact with signed vs unsigned
characters ?


C89's description of the execution character set lists the
characters that must be present, and indicates that '0' through
'9' must be consequative, but I don't find anything in it that
would require 'A' < 'B'. What is your source?
 
M

Michael Mair

kfeder said:
the output im looking for is
F
FE
FED
FEDC
FEDCB
FEDCBA

Please quote sufficient context -- every article may have to
stand for itself.

Back to your question:
There are several ways to do this; basically, you just
have to count backwards.

#include <stdio.h>

#define PRINT_CHARS 6

int main (void)
{
int i, j;
char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (i = PRINT_CHARS - 1; i >= 0; i--) {
for (j = PRINT_CHARS - 1; j >= i; j--) {
putchar((unsigned char)letters[j]);
}
putchar('\n');
}

#ifdef USE_PAUSE
system("pause");
#endif

return 0;
}

Cheers
Michael
 
M

Michael Mair

Walter said:
Where do you find the requirement that the execution character
set is monotonically increasing? What does your reference say
about the relative values of 'A' and 'a' ? How does the
clause you are thinking of interact with signed vs unsigned
characters ?

C89's description of the execution character set lists the
characters that must be present, and indicates that '0' through
'9' must be consequative, but I don't find anything in it that
would require 'A' < 'B'. What is your source?

I don't know what I was smoking at the time or whether it were
withdrawal symptoms... You are right: I do not find anything
in the (C99) standard justifying what I wrote about the relation
between letters of the Latin alphabet.

Cheers
Michael
 
D

David Resnick

Walter said:
Where do you find the requirement that the execution character
set is monotonically increasing? What does your reference say
about the relative values of 'A' and 'a' ? How does the
clause you are thinking of interact with signed vs unsigned
characters ?


C89's description of the execution character set lists the
characters that must be present, and indicates that '0' through
'9' must be consequative, but I don't find anything in it that
would require 'A' < 'B'. What is your source?

I think there is no such requirement.

In the "C" locale, I think users of machines other
than the Deathstation would be few and far between if
strcmp("a","b"), for example, returned positive.

The standard does't seem to disallow that, however. Under the
"portability issues" section of n869 in section J.3.4 it includes:

-- The values of the members of the execution character
set (5.2.1).

And the section you cited requires nothing other than the digit order
you cited. It says what the basic set must include, and says that
a collating sequence (unspecified) must be defined...

-David
 
R

Richard Heathfield

Walter Roberson said:
Then your logic becomes something like

for (row=0; row < strlen(hexes); row++) {
for( offset=row; offset < strlen(hexes); offset++ )
putchar(hexes[offset]); putchar('\n');

I know he's new, but don't you think it'd be a great idea *not* to teach him
to do O(N^4) loops?
}


As for the reverse direction, I will give the hint:

putchar(hexes[strlen(hexes)-offset-1])

Or indeed O(N^5).
 
D

Dave Thompson

Walter Roberson wrote:
Aside: consecutive.
I think there is no such requirement.

In the "C" locale, I think users of machines other
than the Deathstation would be few and far between if
strcmp("a","b"), for example, returned positive.

The standard does't seem to disallow that, however. Under the
"portability issues" section of n869 in section J.3.4 it includes:

-- The values of the members of the execution character
set (5.2.1).

And the section you cited requires nothing other than the digit order
you cited. It says what the basic set must include, and says that
a collating sequence (unspecified) must be defined...
There is one more requirement in 6.2.5p3: any char in the basic
execution charset (IIRC corrected by TC1 to except null) can be stored
in char and be positive; thus if plain char 'is' (acts like) signed,
none of the basic charset can be in the top half. (For ASCII family
machines this is no problem since all the basic chars are in 7-bit
ASCII and all C char flavors must be at least 8 bits.)

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top