Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #85194 > unrolled thread

Two versions of K&R exercise 3-5: one doesn't print the string

Started byAlla _ <modelling.data@gmail.com>
First post2016-03-28 11:49 -0700
Last post2016-04-12 11:26 -0700
Articles 16 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  Two versions of K&R exercise 3-5: one doesn't print the string Alla _ <modelling.data@gmail.com> - 2016-03-28 11:49 -0700
    Re: Two versions of K&R exercise 3-5: one doesn't print the string Paul N <gw7rib@aol.com> - 2016-03-28 13:42 -0700
      Re: Two versions of K&R exercise 3-5: one doesn't print the string Alla _ <modelling.data@gmail.com> - 2016-03-30 04:01 -0700
    Re: Two versions of K&R exercise 3-5: one doesn't print the string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-28 22:29 +0100
    Re: Two versions of K&R exercise 3-5: one doesn't print the string Barry Schwarz <schwarzb@dqel.com> - 2016-03-28 22:05 -0700
      Re: Two versions of K&R exercise 3-5: one doesn't print the string Jerry Stuckle <jstucklex@attglobal.net> - 2016-03-29 08:49 -0400
      Re: Two versions of K&R exercise 3-5: one doesn't print the string Keith Thompson <kst-u@mib.org> - 2016-03-29 09:00 -0700
        Re: Two versions of K&R exercise 3-5: one doesn't print the string Barry Schwarz <schwarzb@dqel.com> - 2016-03-29 10:42 -0700
          Re: Two versions of K&R exercise 3-5: one doesn't print the string Keith Thompson <kst-u@mib.org> - 2016-03-29 12:12 -0700
    Re: Two versions of K&R exercise 3-5: one doesn't print the string programming109@gmail.com - 2016-04-11 08:29 -0700
      Re: Two versions of K&R exercise 3-5: one doesn't print the string programming109@gmail.com - 2016-04-11 08:35 -0700
      Re: Two versions of K&R exercise 3-5: one doesn't print the string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-11 19:59 +0100
        Re: Two versions of K&R exercise 3-5: one doesn't print the string programming109@gmail.com - 2016-04-11 22:41 -0700
          Re: Two versions of K&R exercise 3-5: one doesn't print the string luser droog <luser.droog@gmail.com> - 2016-04-11 23:18 -0700
          Re: Two versions of K&R exercise 3-5: one doesn't print the string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-12 11:02 +0100
      Re: Two versions of K&R exercise 3-5: one doesn't print the string Keith Thompson <kst-u@mib.org> - 2016-04-12 11:26 -0700

#85194 — Two versions of K&R exercise 3-5: one doesn't print the string

FromAlla _ <modelling.data@gmail.com>
Date2016-03-28 11:49 -0700
SubjectTwo versions of K&R exercise 3-5: one doesn't print the string
Message-ID<4b1ed1df-f855-421f-8a82-82dcab0656e2@googlegroups.com>
Hello!

I have written two versions of the exercise 3-5, but the version,
which uses an array for hexadecimal values, doesn't fill the array
(it fills it until the itob function runs, but fails to print 
the array from the main function).

Here is the text of the exercise:
Write the function itob (n, s ,b) that converts the integer n
into a base b character representation in the string s. In 
particular, itob (n, s, 16) formats n as a hexadecimal integer
in s.

Here is how do-while loop in the version with hexadecimal 
array should work. For example, input n = 23, base = 16.
temp = 23 % 16 = 7
string[0] = hex_array[temp] = 7
i++, so i = 1
new_n = 23 / 16 = 1

temp = 1 % 16 = 1
string[0] = hex_array[temp] = 1
i++, so i = 2
new_n = 1 / 16 = 0
and the loop stops; reverses the array. 

The version with the array (doesn't do the job):

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

int atoi_function(char *string);
void itob_function(int n, char *string, int base, char *hex_array);
void reverse(char *string);

int main(int argc, char **argv)
{
    char string[1024];
    
    if (argc < 3)
    {
        printf("Usage: ./program, number, base\n");
        return -1;
    }
    char base_16[] = {0,1,2,3,4,5,6,7,8,9,'A',
                         'B','C','D','E','F'};
    int n = atoi_function(argv[1]);
    int base = atoi_function(argv[2]);
    itob_function(n, string, base, base_16);
        
    printf("\"%d\" => \"%s\" base \"%d\"\n", n, string, base);
    
    return 0;
}

// convert string to integer
int atoi_function(char *string)
{
    int i, n, sign;
    //skip white space
    for (i = 0; isspace(string[i]); i++);
    
    sign = (string[i] == '-') ? -1 : 1;
    //skip sign
    if (string[i] == '+' || string[i] == '-')
        i++;
    for (n = 0; isdigit(string[i]); i++)
        n = 10 * n + (string[i] - '0');
    return sign * n;
}

// convert number to a character string
void itob_function(int n, char *string, int base, char *hex_array)
{
    
    int new_n = n;
    int i, sign, temp;
    
    //record sign, make it positive
    if((sign = new_n) < 0)
    {
        if (n == INT_MIN)
            new_n += 1;
        new_n = -new_n;
        // test the previous step
        printf("%d\n", new_n);
    }
    i = 0;
    //generate digits in reverse order
    do
    {
        if (base == 16)
        {
            temp = new_n % base;
            // test the previous step
            printf("%d\n", temp);
            string[i] = hex_array[temp];
            // test the previous step
            printf("%d\n", string[i]);
        }
        else
            string[i] = new_n % base + '0';
        i += 1;
    } while ((new_n /= base) > 0);
    
    if (n == INT_MIN)
        string[0] += 1;
    
    if (sign < 0)
        string[i++] = '-';
    string[i] = '\0';
    reverse(string);
}
// reverse string
void reverse(char *string)
{
    int c;
    for (int i = 0, j = strlen(string) - 1;
         i < j; i++, j--)
    {
        c = string[i];
        string[i] = string[j];
        string[j] = c;
    }
}

The version without the array (does the job):
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

int atoi_function(char *string);
void itob_function(int n, char *string, int base);
void reverse(char *string);

int main(int argc, char **argv)
{
    char string[1024];
    
    if (argc < 3)
    {
        printf("Usage: ./program, number, base\n");
        return -1;
    }
    int n = atoi_function(argv[1]);
    int base = atoi_function(argv[2]);
    itob_function(n, string, base);
        
    printf("\"%d\" => \"%s\" base \"%d\"\n", n, string, base);
    
    return 0;
}

// convert string to integer
int atoi_function(char *string)
{
    int i, n, sign;
    //skip white space
    for (i = 0; isspace(string[i]); i++);
    
    sign = (string[i] == '-') ? -1 : 1;
    //skip sign
    if (string[i] == '+' || string[i] == '-')
        i++;
    for (n = 0; isdigit(string[i]); i++)
        n = 10 * n + (string[i] - '0');
    return sign * n;
}

// convert number to a character string
void itob_function(int n, char *string, int base)
{
    
    int new_n = n;
    int i, sign;
    
    //record sign, make it positive
    if((sign = new_n) < 0)
    {
        if (n == INT_MIN)
            new_n += 1;
        new_n = -new_n;
        // test the previous step
        printf("%d\n", new_n);
    }
    i = 0;
    //generate digits in reverse order
    do
    {
        if (base == 16)
        {
            int temp = new_n % base;
            string[i] = (temp < 10) ? temp + '0' :
                                       temp + 'A';
            // test the previous step
            printf("%d\n", string[i]);
        }
        else
            string[i] = new_n % base + '0';
        i += 1;
    } while ((new_n /= base) > 0);
    
    if (n == INT_MIN)
        string[0] += 1;
    
    if (sign < 0)
        string[i++] = '-';
    string[i] = '\0';
    reverse(string);
}
// reverse string
void reverse(char *string)
{
    int c;
    for (int i = 0, j = strlen(string) - 1;
         i < j; i++, j--)
    {
        c = string[i];
        string[i] = string[j];
        string[j] = c;
    }
}

Thank you!

[toc] | [next] | [standalone]


#85201

FromPaul N <gw7rib@aol.com>
Date2016-03-28 13:42 -0700
Message-ID<9d277355-8834-40d5-9a1a-1a4ab2ea2074@googlegroups.com>
In reply to#85194
On Monday, March 28, 2016 at 7:49:25 PM UTC+1, Alla _ wrote:

> I have written two versions of the exercise 3-5, but the version,
> which uses an array for hexadecimal values, doesn't fill the array
> (it fills it until the itob function runs, but fails to print 
> the array from the main function).

[Big snip]

>     char base_16[] = {0,1,2,3,4,5,6,7,8,9,'A',
>                          'B','C','D','E','F'};

This is your problem. The array is supposed to contain the characters that you
want to print, so it should be { '0', '1', etc

'0' is the value used to print a 0. It's typically 48. The values of 0-31 are used for odd things, and indeed 0 is used to show the end of a string.

[More snip]

>     do
>     {
>         if (base == 16)
>         {
>             temp = new_n % base;
>             // test the previous step
>             printf("%d\n", temp);
>             string[i] = hex_array[temp];
>             // test the previous step
>             printf("%d\n", string[i]);
>         }
>         else
>             string[i] = new_n % base + '0';

This is a bit odd, as it will work for base 16 but not for bases between 11 and 15 inclusive. You could try <= 16 instead, which will work for these bases.
But of course your other option of

[snip]

>             string[i] = (temp < 10) ? temp + '0' :
>                                        temp + 'A';

will, if you're using the ASCII character set, work for anything up to base 36. As long as you put a " - 10" in the right place...

Hope that helps.
Paul.

[toc] | [prev] | [next] | [standalone]


#85310

FromAlla _ <modelling.data@gmail.com>
Date2016-03-30 04:01 -0700
Message-ID<1a9160f4-c82e-4958-9bb6-4971489fc274@googlegroups.com>
In reply to#85201
On Tuesday, March 29, 2016 at 12:42:32 AM UTC+4, Paul N wrote:
> On Monday, March 28, 2016 at 7:49:25 PM UTC+1, Alla _ wrote:
> 
> > I have written two versions of the exercise 3-5, but the version,
> > which uses an array for hexadecimal values, doesn't fill the array
> > (it fills it until the itob function runs, but fails to print 
> > the array from the main function).
> 
> [Big snip]
> 
> >     char base_16[] = {0,1,2,3,4,5,6,7,8,9,'A',
> >                          'B','C','D','E','F'};
> 
> This is your problem. The array is supposed to contain the characters that you
> want to print, so it should be { '0', '1', etc
> 
> '0' is the value used to print a 0. It's typically 48. The values of 0-31 are used for odd things, and indeed 0 is used to show the end of a string.
> 
> [More snip]
> 
> >     do
> >     {
> >         if (base == 16)
> >         {
> >             temp = new_n % base;
> >             // test the previous step
> >             printf("%d\n", temp);
> >             string[i] = hex_array[temp];
> >             // test the previous step
> >             printf("%d\n", string[i]);
> >         }
> >         else
> >             string[i] = new_n % base + '0';
> 
> This is a bit odd, as it will work for base 16 but not for bases between 11 and 15 inclusive. You could try <= 16 instead, which will work for these bases.
> But of course your other option of
> 
> [snip]
> 
> >             string[i] = (temp < 10) ? temp + '0' :
> >                                        temp + 'A';
> 
> will, if you're using the ASCII character set, work for anything up to base 36. As long as you put a " - 10" in the right place...
> 
> Hope that helps.
> Paul.

Thank you! Shame on me - indeed, I have missed single quotation marks
around digits. 
As to your last comment, I didn't intend to account for unconventional
bases, such as base 13, or 14, or 15.

[toc] | [prev] | [next] | [standalone]


#85202

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-03-28 22:29 +0100
Message-ID<87h9fqz3yb.fsf@bsb.me.uk>
In reply to#85194
Alla _ <modelling.data@gmail.com> writes:

> I have written two versions of the exercise 3-5, but the version,
> which uses an array for hexadecimal values, doesn't fill the array
> (it fills it until the itob function runs, but fails to print 
> the array from the main function).

That's very confusing!  Anyway, the code is clearer so that's not really
important.


>
> Here is the text of the exercise:
> Write the function itob (n, s ,b) that converts the integer n
> into a base b character representation in the string s. In 
> particular, itob (n, s, 16) formats n as a hexadecimal integer
> in s.
>
> Here is how do-while loop in the version with hexadecimal 
> array should work. For example, input n = 23, base = 16.
> temp = 23 % 16 = 7
> string[0] = hex_array[temp] = 7
> i++, so i = 1
> new_n = 23 / 16 = 1

You pseudo code is not clear so I am not surprised that you got the code
wrong.  What does string[0] = hex_array[temp] = 7 mean for example?
When 23 % 16 == 7 (note I wrote == because 23 % 16 = 7 is at best
ambiguous) then string[i] should be set to '7' not 7.

<snip>
>     char base_16[] = {0,1,2,3,4,5,6,7,8,9,'A',
>                          'B','C','D','E','F'};

When the remainder is 7, you need to put 55 in the target string, not 7
(55 can be written '7' of course).

But if you use an array like this it's much easier to write it as
"0123456789ABCDEF" and it belongs in the itob function, not out here.

<snip>
> The version without the array (does the job):

I don't think so.  There are a few issues with it (I won't say what
right away because you need to develop the skill of testing your code).

<snip>
> // reverse string

It reverses almost all strings.  There's one string it gets wrong which,
as it happens, does not occur in your program so it's fine for this
job.  However, it's a bug waiting to happen if you use this code in some
other program.

> void reverse(char *string)
> {
>     int c;
>     for (int i = 0, j = strlen(string) - 1;
>          i < j; i++, j--)
>     {
>         c = string[i];
>         string[i] = string[j];
>         string[j] = c;
>     }
> }

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#85220

FromBarry Schwarz <schwarzb@dqel.com>
Date2016-03-28 22:05 -0700
Message-ID<832kfbh2q3mn6n0d5vsobentt7gd03d7k8@4ax.com>
In reply to#85194
On Mon, 28 Mar 2016 11:49:16 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>I have written two versions of the exercise 3-5, but the version,
>which uses an array for hexadecimal values, doesn't fill the array
>(it fills it until the itob function runs, but fails to print 
>the array from the main function).

<snip>

>    if (argc < 3)
>    {
>        printf("Usage: ./program, number, base\n");

When using printf to print string like this (either literal, array, or
pointer), there is a benefit to coding it as
     printf("%s", your string parameter here);
This will eliminate any problems if your string should happen to
contain any '%'.  As you have written it, the '%' would be interpreted
as the start of a format conversion.

>        return -1;
>    }
>    char base_16[] = {0,1,2,3,4,5,6,7,8,9,'A',
>                         'B','C','D','E','F'};

I wonder why you thought the function should not handle bases larger
than 16.

<snip>

>// convert number to a character string
>void itob_function(int n, char *string, int base, char *hex_array)
>{
>    
>    int new_n = n;
>    int i, sign, temp;
>    
>    //record sign, make it positive
>    if((sign = new_n) < 0)
>    {
>        if (n == INT_MIN)
>            new_n += 1;
>        new_n = -new_n;
>        // test the previous step
>        printf("%d\n", new_n);
>    }
>    i = 0;
>    //generate digits in reverse order
>    do
>    {
>        if (base == 16)
>        {
>            temp = new_n % base;
>            // test the previous step
>            printf("%d\n", temp);
>            string[i] = hex_array[temp];
>            // test the previous step
>            printf("%d\n", string[i]);
>        }
>        else
>            string[i] = new_n % base + '0';
>        i += 1;
>    } while ((new_n /= base) > 0);
>    
>    if (n == INT_MIN)
>        string[0] += 1;

This trick is guaranteed to fail for bases 2, 8, 16.  I pretty sure it
will also fail for base 4 and 32.

>    
>    if (sign < 0)
>        string[i++] = '-';
>    string[i] = '\0';
>    reverse(string);
>}

<snip>

>The version without the array (does the job):

<snip>

>// convert number to a character string
>void itob_function(int n, char *string, int base)
>{
>    
>    int new_n = n;
>    int i, sign;
>    
>    //record sign, make it positive
>    if((sign = new_n) < 0)
>    {
>        if (n == INT_MIN)
>            new_n += 1;
>        new_n = -new_n;
>        // test the previous step
>        printf("%d\n", new_n);
>    }
>    i = 0;
>    //generate digits in reverse order
>    do
>    {
>        if (base == 16)
>        {
>            int temp = new_n % base;
>            string[i] = (temp < 10) ? temp + '0' :
>                                       temp + 'A';

Others have noted this error.

>            // test the previous step
>            printf("%d\n", string[i]);
>        }
>        else
>            string[i] = new_n % base + '0';
>        i += 1;
>    } while ((new_n /= base) > 0);
>    
>    if (n == INT_MIN)
>        string[0] += 1;

Still wrong as noted above.
    
>    if (sign < 0)
>        string[i++] = '-';
>    string[i] = '\0';
>    reverse(string);
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#85230

FromJerry Stuckle <jstucklex@attglobal.net>
Date2016-03-29 08:49 -0400
Message-ID<nddtdp$paj$1@jstuckle.eternal-september.org>
In reply to#85220
On 3/29/2016 1:05 AM, Barry Schwarz wrote:
> On Mon, 28 Mar 2016 11:49:16 -0700 (PDT), Alla _
> <modelling.data@gmail.com> wrote:
> 
>> I have written two versions of the exercise 3-5, but the version,
>> which uses an array for hexadecimal values, doesn't fill the array
>> (it fills it until the itob function runs, but fails to print 
>> the array from the main function).
> 
> <snip>
> 
>>    if (argc < 3)
>>    {
>>        printf("Usage: ./program, number, base\n");
> 
> When using printf to print string like this (either literal, array, or
> pointer), there is a benefit to coding it as
>      printf("%s", your string parameter here);
> This will eliminate any problems if your string should happen to
> contain any '%'.  As you have written it, the '%' would be interpreted
> as the start of a format conversion.
> 

I disagree.  When the string is fixed and known, as it is here, I see no
problem with the way he's coded it.  It's clearer (and a bit faster,
although that shouldn't matter) than your way.

Even if it does contain a '%', it's easy to change to '%%'.

Now, if the string is unknown, then I would agree with you.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#85239

FromKeith Thompson <kst-u@mib.org>
Date2016-03-29 09:00 -0700
Message-ID<lntwjp1dgm.fsf@kst-u.example.com>
In reply to#85220
Barry Schwarz <schwarzb@dqel.com> writes:
> On Mon, 28 Mar 2016 11:49:16 -0700 (PDT), Alla _
> <modelling.data@gmail.com> wrote:
[...]
>>    if (argc < 3)
>>    {
>>        printf("Usage: ./program, number, base\n");
>
> When using printf to print string like this (either literal, array, or
> pointer), there is a benefit to coding it as
>      printf("%s", your string parameter here);
> This will eliminate any problems if your string should happen to
> contain any '%'.  As you have written it, the '%' would be interpreted
> as the start of a format conversion.

I'd probably put the newline in the format string:

    printf("%s\n", "Usage: ./program, number, base");

Another option (I prefer this one):

    puts("Usage: ./program, number, base");

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#85249

FromBarry Schwarz <schwarzb@dqel.com>
Date2016-03-29 10:42 -0700
Message-ID<8iflfbpjcdar85d5opnt997elf4flnul6b@4ax.com>
In reply to#85239
On Tue, 29 Mar 2016 09:00:25 -0700, Keith Thompson <kst-u@mib.org>
wrote:

>Barry Schwarz <schwarzb@dqel.com> writes:
>> On Mon, 28 Mar 2016 11:49:16 -0700 (PDT), Alla _
>> <modelling.data@gmail.com> wrote:
>[...]
>>>    if (argc < 3)
>>>    {
>>>        printf("Usage: ./program, number, base\n");
>>
>> When using printf to print string like this (either literal, array, or
>> pointer), there is a benefit to coding it as
>>      printf("%s", your string parameter here);
>> This will eliminate any problems if your string should happen to
>> contain any '%'.  As you have written it, the '%' would be interpreted
>> as the start of a format conversion.
>
>I'd probably put the newline in the format string:
>
>    printf("%s\n", "Usage: ./program, number, base");
>
>Another option (I prefer this one):
>
>    puts("Usage: ./program, number, base");

puts() forces a newline.  OK for the example but not so much for a
prompt to enter a value.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#85258

FromKeith Thompson <kst-u@mib.org>
Date2016-03-29 12:12 -0700
Message-ID<lnh9fp14ke.fsf@kst-u.example.com>
In reply to#85249
Barry Schwarz <schwarzb@dqel.com> writes:
> On Tue, 29 Mar 2016 09:00:25 -0700, Keith Thompson <kst-u@mib.org>
> wrote:
>>Barry Schwarz <schwarzb@dqel.com> writes:
>>> On Mon, 28 Mar 2016 11:49:16 -0700 (PDT), Alla _
>>> <modelling.data@gmail.com> wrote:
>>[...]
>>>>    if (argc < 3)
>>>>    {
>>>>        printf("Usage: ./program, number, base\n");
>>>
>>> When using printf to print string like this (either literal, array, or
>>> pointer), there is a benefit to coding it as
>>>      printf("%s", your string parameter here);
>>> This will eliminate any problems if your string should happen to
>>> contain any '%'.  As you have written it, the '%' would be interpreted
>>> as the start of a format conversion.
>>
>>I'd probably put the newline in the format string:
>>
>>    printf("%s\n", "Usage: ./program, number, base");
>>
>>Another option (I prefer this one):
>>
>>    puts("Usage: ./program, number, base");
>
> puts() forces a newline.  OK for the example but not so much for a
> prompt to enter a value.

The argument to the printf() had a newline in it.  That's the only
reason I suggested replacing it with puts().  If you want to print a
string literal without a newline, you'd have to use
    fputs("some string", stdout);
which I still slightly prefer to
    printf("%s", "some string");
but it's a closer call.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#86288

Fromprogramming109@gmail.com
Date2016-04-11 08:29 -0700
Message-ID<e4290a79-84b6-49ae-8aad-cd1194bdb49b@googlegroups.com>
In reply to#85194
didn't wanna open a new thread (or maybe I should have? dono the rules and preferences around here), so I will ask here.

like I said in another thread, with this book's sometimes vague and so called 'high class' language, its sometimes harder for me to figure out what the problem is and what they need me to do, more then actually solving the problem. so with this exercise, I'm not exactly sure what they want.

if they want me to convert a dec integer to a hex and than print it to the string like an hex, I think the formula is pretty straight forward as I learned from this book itself somewhere, just replace the s[i++] = n % 10 + '0' to an n%16 + (gets a bit tricky here, if its bigger then 9 have to add here 64 or 65 [gonna have to try if the ?:. operator works here) and you are good. but I'm not sure thats what they want me to do.

[toc] | [prev] | [next] | [standalone]


#86289

Fromprogramming109@gmail.com
Date2016-04-11 08:35 -0700
Message-ID<686e2a78-0058-4e2b-962e-a4c5833730ac@googlegroups.com>
In reply to#86288
On Monday, April 11, 2016 at 11:29:19 AM UTC-4, program...@gmail.com wrote:
> didn't wanna open a new thread (or maybe I should have? dono the rules and preferences around here), so I will ask here.
> 
> like I said in another thread, with this book's sometimes vague and so called 'high class' language, its sometimes harder for me to figure out what the problem is and what they need me to do, more then actually solving the problem. so with this exercise, I'm not exactly sure what they want.
> 
> if they want me to convert a dec integer to a hex and than print it to the string like an hex, I think the formula is pretty straight forward as I learned from this book itself somewhere, just replace the s[i++] = n % 10 + '0' to an n%16 + (gets a bit tricky here, if its bigger then 9 have to add here 64 or 65 [gonna have to try if the ?:. operator works here) and you are good. but I'm not sure thats what they want me to do.

I meant 64 or 96 (the equivalent to 0 before digits) depends if you wanna print it as uppercase or lowercase

[toc] | [prev] | [next] | [standalone]


#86298

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-04-11 19:59 +0100
Message-ID<87d1pw2cpv.fsf@bsb.me.uk>
In reply to#86288
programming109@gmail.com writes:

> didn't wanna open a new thread (or maybe I should have? dono the rules
> and preferences around here), so I will ask here.

It's usually better to start anew thread unless you are commenting on
something particular.  Cutting all the quoted text in a reply us a good
sign that a new thread is probably called for.

> like I said in another thread, with this book's sometimes vague and so
> called 'high class' language, its sometimes harder for me to figure
> out what the problem is and what they need me to do, more then
> actually solving the problem. so with this exercise, I'm not exactly
> sure what they want.

I thought you asked that question already?  Maybe it was not you.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#86316

Fromprogramming109@gmail.com
Date2016-04-11 22:41 -0700
Message-ID<a384f662-0f35-42c9-a75b-17336353983e@googlegroups.com>
In reply to#86298
On Monday, April 11, 2016 at 2:59:49 PM UTC-4, Ben Bacarisse wrote:
> programming109@gmail.com writes:
> 
> > didn't wanna open a new thread (or maybe I should have? dono the rules
> > and preferences around here), so I will ask here.
> 
> It's usually better to start anew thread unless you are commenting on
> something particular.  Cutting all the quoted text in a reply us a good
> sign that a new thread is probably called for.
> 
> > like I said in another thread, with this book's sometimes vague and so
> > called 'high class' language, its sometimes harder for me to figure
> > out what the problem is and what they need me to do, more then
> > actually solving the problem. so with this exercise, I'm not exactly
> > sure what they want.
> 
> I thought you asked that question already?  Maybe it was not you.
> 
> <snip>
> -- 
> Ben.

thanks for your reply. as I mentioned, I wrote this already in another thread - about another k&r exercise - that sometimes I just couldn't figure out what they want me to do. so all I can do is ask ppl. (its interesting, because even though the whole book is written in such a professor like language, when it comes to the exercises it gets even worse. almost like they are doing it on purpose :)

[toc] | [prev] | [next] | [standalone]


#86317

Fromluser droog <luser.droog@gmail.com>
Date2016-04-11 23:18 -0700
Message-ID<ca18f379-0ff7-435a-bfa8-a5903c79f81c@googlegroups.com>
In reply to#86316
On Tuesday, April 12, 2016 at 12:41:28 AM UTC-5, program...@gmail.com wrote:
sometimes I just couldn't figure out what they want me to do. so all I can do is ask ppl. (its interesting, because even though the whole book is written in such a professor like language, when it comes to the exercises it gets even worse. almost like they are doing it on purpose :)

I think that this is not isolated to k&r2 exercises,
many times in programming and in life understanding
"what is being asked of me?" can be a big task,
and once you get it, the actual undertaking is much
easier.

[toc] | [prev] | [next] | [standalone]


#86322

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-04-12 11:02 +0100
Message-ID<87k2k3ywjl.fsf@bsb.me.uk>
In reply to#86316
programming109@gmail.com writes:

> On Monday, April 11, 2016 at 2:59:49 PM UTC-4, Ben Bacarisse wrote:
>> programming109@gmail.com writes:
<snip>
>> > like I said in another thread, with this book's sometimes vague and so
>> > called 'high class' language, its sometimes harder for me to figure
>> > out what the problem is and what they need me to do, more then
>> > actually solving the problem. so with this exercise, I'm not exactly
>> > sure what they want.
>> 
>> I thought you asked that question already?  Maybe it was not you.
>> 
>> <snip>
>> -- 
>> Ben.

(It's best to snip signatures)

> thanks for your reply. as I mentioned, I wrote this already in another
> thread - about another k&r exercise - that sometimes I just couldn't
> figure out what they want me to do. so all I can do is ask ppl. (its
> interesting, because even though the whole book is written in such a
> professor like language, when it comes to the exercises it gets even
> worse. almost like they are doing it on purpose :)

I'm sure they *are* doing it on purpose!  There's two reasons for this:
one is that the book is not about programming -- it's about C.  It was
intended for people who know how to program, but who wanted to know this
new fancy language called C.  The other reason is that they don't care
about the details they have left unspecified.  You are supposed to come
up with something that fills in those parts to *your* satisfaction, not
theirs.

This is not that same as being confused by some of the text.  If you
don't understand what is being asked (rather than being worried about
the things left unsaid) then you just have to ask someone, and here is
as good a place as any for that.  It helps if you say what's confusing
you as well as you can.

it's also perfectly reasonable to ask about the things that are unsaid,
but you should include what you decided to do.  For example, when I just
tried this exercises I decided (at first) simply to ignore negative or
zero bases and it's reasonable to ask if other people would do the same.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#86339

FromKeith Thompson <kst-u@mib.org>
Date2016-04-12 11:26 -0700
Message-ID<ln8u0iof9g.fsf@kst-u.example.com>
In reply to#86288
programming109@gmail.com writes:
> didn't wanna open a new thread (or maybe I should have? dono the rules
> and preferences around here), so I will ask here.

[snip]

When posting here, please keep your lines down to about 72 columns.  In
many contexts, it makes sense to insert a line break only at the end of
a paragraph, but on Usenet your text will be much easier to read with
shorter lines.

The Google Groups web interface doesn't make it particularly convenient
to do this, but those of us who use actual newsreaders will appreciate
the effort.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web