NO

Author Topic: My first steps in learning C  (Read 3065 times)

Offline Adrian Rizeanu

  • Member
  • *
  • Posts: 6
My first steps in learning C
« on: December 07, 2020, 11:47:27 PM »
Hello
I try to implement in C the following: I wish to organize the following number: 1234 in a triangle form, let's say :   3
                                                                                                                                                                                   23
                                                                                                                                                                                  1234
The idea is to break the number in 4 digits , divide every digit in modulo 2 and keep the rest , getting rid of the digit and going to the following one, starting from x^0 , to x^3 ( units, tenth, hundreds , thousands), and finally printing them in the order i choose.
So, my program is:
#include<stdio.h>
int main(void)
{
   unsigned long x;
   unsigned x0,x1,x2,x3;
   printf("please print your number:\n");
   scanf("%lu",&x);
   x0%10;  //keep the units digit
   x/10; // get rid of the units
   x1%10;  //keep the tenth
   x/10;  // get rid of tenth
   x2%10;  //keep the hundreds
   x/10;  //get rid of hundreds
   x3%10;  // keep the hundreds
   x/10; // in x remain the thousands
   printf("   %u\n",x2);  // space , space, x2
   printf(" %u%u\n",x2,x1);  /space x2.x1
   printf("%lu%u%u%u\n, x3,x2,x1,x0);  /no space, x3,x2,x1,x0
   return 0; }

And  the result after input : 1234 , compile is : 3
                                                                        34
                                                                      1233
I can not understand where i am wrong , please help.
Thank you
Adrian
   





Offline algernon_77

  • Member
  • *
  • Posts: 33
Re: My first steps in learning C
« Reply #1 on: December 08, 2020, 06:54:12 AM »
Hello Adrian!

I'm not sure I understand exactly what you want to accomplish (you say modulo 2, but you use modulo 10 in code); However, I changed the code a bit to get to the result I believe you seek:

Code: [Select]
    unsigned long x;
    unsigned x0,x1,x2,x3;

    printf("please print your number:\n");
    scanf("%lu",&x);

    x0 = x % 10;  //units
    x  = x / 10;

    x1 = x % 10;  //tens
    x  = x / 10;

    x2 = x % 10;  //hundreds
    x  = x / 10;
 
    x3 = x % 10;  //thousands

    printf("  %u\n", x1);
    printf(" %u%u\n", x2, x1);
    printf("%lu%u%u%u\n", x3, x2, x1, x0);

Please note the use of the equal sign to actually assign the result to your variables and the use of x1 variable instead of x2 in the first printf.
Good luck!
« Last Edit: December 08, 2020, 01:38:26 PM by algernon_77 »

Offline Adrian Rizeanu

  • Member
  • *
  • Posts: 6
Re: My first steps in learning C
« Reply #2 on: December 08, 2020, 09:36:11 PM »
Hello Algernon_77
Thank you very much for your help!
As soon as i finish my reply to you I will try it on my C compiler ( Pelles of corse )
I feel surely very confused as i am learning from a book that used Borland compiler for explaining and I can not have that for the moment.
May I kindly ask you to let me know where can i find a free tutorial for C ? It is obvious i need to start again my study, confusing %modulo 2 with modulo 10 is not acceptable...
Kindest regards,
Adrian

Offline Adrian Rizeanu

  • Member
  • *
  • Posts: 6
Re: My first steps in learning C
« Reply #3 on: December 08, 2020, 10:51:50 PM »
/*This program is intended to organize the digits of a number in a triangle shape */

#include<stdio.h>
int main(void)
 {
   unsigned long x;
   unsigned x0,x1,x2,x3;

   printf("please print your number:\n");

   scanf("%lu",&x);

   x0=x%10;  //units
   x=x/10;

   x1=x%10;  //tens
   x=x/10;

   x2=x%10;
   x=x/10;

   x2=x%10; //hundreds
   x/10;

   x3=x%10;  //thousands

   printf("   %u\n",x1);
   printf("  %u%u\n",x2,x1);
   printf(" %u%u%u\n",x3,x2,x1);
   printf("%lu%u%u%u\n",x3,x2,x1,x0);

   return 0;}

So, after building the project i get : 3
                                                       13
                                                     113
                                                   1134
Could you please help me understand where is my mistake ? ( my number is : 1234 )
Thank you very much
Adrian
« Last Edit: December 08, 2020, 10:54:26 PM by Adrian Rizeanu »

Offline algernon_77

  • Member
  • *
  • Posts: 33
Re: My first steps in learning C
« Reply #4 on: December 09, 2020, 06:29:27 AM »
Hello!

It does not work because you assign x mod 10 to x2, then divide x by 10, then assign x mod 10 to x2 again, overwriting it:

Code: [Select]
   x2=x%10;
   x=x/10;

   x2=x%10; //hundreds
   x/10;


But I also made a mistake :) in the first printf it should be x3, not x1... sorry for the confusion:

Code: [Select]
int main(void)
{
    // you can make them all unsigned, it's the same size as unsigned long (32 bit)
    unsigned x,x0,x1,x2,x3;

    printf("please print your number:\n");
    scanf("%lu",&x);
 
    x0 = x % 10;  //units
    x  = x / 10;

    x1 = x % 10;  //tens
    x  = x / 10;

    x2 = x % 10;  //hundreds
    x  = x / 10;
 
    x3 = x % 10;  //thousands

    printf("   %u\n", x3);
    printf("  %u%u\n", x2, x1);
    printf(" %u%u%u\n",x3,x2,x1);
    printf("%u%u%u%u\n", x3, x2, x1, x0);

    return 0;
}

As for books, "The C programming language" by Kernighan and Ritchie would be nice, but there's also a lot of tutorials online.
« Last Edit: December 09, 2020, 07:01:18 AM by algernon_77 »

Offline John Z

  • Member
  • *
  • Posts: 790
Re: My first steps in learning C
« Reply #5 on: December 09, 2020, 01:46:34 PM »
Hi Adrian,

There are many, many on-line books and tutorials.  Here is one that has a downloadable pdf.  FREE -
https://riptutorial.com/ebook/c

which is under:
https://riptutorial.com/

John Z

Offline John Z

  • Member
  • *
  • Posts: 790
Re: My first steps in learning C
« Reply #6 on: December 09, 2020, 02:41:35 PM »
Hi Adrian,

So I supposed the task was to learn about Mod 10?  But if just want the triangle here is another method.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{ int loup;
   int x;
   char temp[25];

   printf("please print your number:\n");
   scanf("%lu",&x);

   sprintf(temp,"%d",x);
   for( loup=strlen(temp); loup>0; loup--)
{ printf("%15s\n",temp+loup-1); }

   return(0);

}

Since you are learning you can play with this and figure out its workings.
This works for a wide range of input numbers.  You might see that for numbers over four digits your original code will fail to give you the results you want, and for number less than 1000 you will see a tree with zero's.
Also try entering -1234  :)
C is well worth learning.

John Z

Offline Adrian Rizeanu

  • Member
  • *
  • Posts: 6
Re: My first steps in learning C
« Reply #7 on: December 09, 2020, 11:18:56 PM »
Hi algernon_77 , Hi John ,
Thank you both for helping me to advance in my study !
It is late now and i am not capable to go through all your , so nice , messages, my day was full of tasks... and at 69 years old energy is not what it used to be.
Tomorrow i will give full attention and , with your permission , i will bother you again.
I look forward to test your ideas and learn from them as much as i can.
Have a great night !
Your sincerely
Adrian

Offline John Z

  • Member
  • *
  • Posts: 790
Re: My first steps in learning C
« Reply #8 on: December 10, 2020, 01:56:20 PM »
Hi Adrian,

Well just to play around I added a few more methods and some comments in the code  :)
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main (void) {

 int loup;
 int z;
 char temp[25];
 char a[25];
 memset(a,0,25);


  printf("please enter your number:\n");
  scanf("%lu",&z);
//--------------------------------------------------------------
  printf("Output #1 reverse order\n");
  sprintf(temp,"%d",z);                   // put number into string
  for( loup=strlen(temp); loup>0; loup--) // loop right to left
{ printf("%15s\n",temp+loup-1);}      // print it

//--------------------------------------------------------------
 printf("\nOutput #2 in desired order\n");
 sprintf(temp,"%d",z);                   // put number into string
 sprintf(a,"%s","");                     // clear temp string 
 for( loup=0; loup<strlen(temp); loup++) // loop left to right through the digits
{ strncpy(a,temp,loup+1);            // copy loup count chars from temp to a
  printf("%15s\n",a);                // print the characters
          memset(a,0,25);                    // clear for next group
}                   

//---------------------------------------------------------------                                       
 printf("\nOutput #3 in desired order\n");  // needs math.h
 int n = (int)log10(abs(z));                // how many digits 0 based
 for (loup = n; loup > -1 ; loup--)         // get each digit group
{ printf("%15d\n", (int)(z/(pow(10,loup)))); } // print it


//---------------------------------------------------------------
   // original
    // you can make them all unsigned, it's the same size as unsigned long (32 bit)
    unsigned x,x0,x1,x2,x3;
    x=(unsigned int)z; // to maintain original intent on unsignedness
//    printf("please enter your number:\n");
//    scanf("%lu",&x);
 
    x0 = x % 10;  //units
    x  = x / 10;

    x1 = x % 10;  //tens
    x  = x / 10;

    x2 = x % 10;  //hundreds
    x  = x / 10;
 
    x3 = x % 10;  //thousands
    printf("\nOriginal Output #4\n");
    printf("   %u\n", x3);
    printf("  %u%u\n", x2, x1);
    printf(" %u%u%u\n",x3,x2,x1);
    printf("%u%u%u%u\n", x3, x2, x1, x0);


   return(0);

}

Try different length input numbers and minus numbers too.

Enjoy,
John Z
« Last Edit: December 10, 2020, 02:09:01 PM by John Z »

Offline Adrian Rizeanu

  • Member
  • *
  • Posts: 6
Re: My first steps in learning C
« Reply #9 on: December 10, 2020, 10:50:59 PM »
Thank you John!
I downloaded the book, thank you so very much, just wonderfull !
I am learning C at my age as Now  I find a little time to improove my work with mucrocomputers ( PICs, generally ), I work a lot in assembler as i do small projects , but working in C will help me a lot.
Kindest regards, and I hope you will have a quiet , healthy and prosperous Christmas and a very Happy New Year !

Offline John Z

  • Member
  • *
  • Posts: 790
Re: My first steps in learning C
« Reply #10 on: December 11, 2020, 11:36:46 AM »
Very welcome Adrian.

Small world. I've also done a number of PIC projects, used both assembly and PicBasic Pro by microengineering labs. Also 6502,6800,Z80, x86 of course.  Parallax stamps in a few projects too.

Merry Christmas and Happy New Year to you and yours as well.

Stay COVID safe,
John

P.S. as long as your slide rule still works you are not old!

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: My first steps in learning C
« Reply #11 on: December 25, 2020, 07:29:58 AM »
Hello Algernon_77
Thank you very much for your help!
As soon as i finish my reply to you I will try it on my C compiler ( Pelles of corse )
I feel surely very confused as i am learning from a book that used Borland compiler for explaining and I can not have that for the moment.
If you are using Borland C or Pelle's C, as far as such simple assignments and operations go, there is no difference. C is C. Only the handling of an IDE or compiler parameter might differ but not such trivial things...

Ralf

Offline Adrian Rizeanu

  • Member
  • *
  • Posts: 6
Re: My first steps in learning C
« Reply #12 on: December 28, 2020, 10:08:35 PM »
Hello,
I begin to understand , thank you!
Have a great 2021 !