Thread: Please help me understand how to think in C.

  1. #1
    Registered User Strik3r's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    7

    Please help me understand how to think in C.

    Hi!

    I'm new on C programming and need help.

    First:

    It’s kind of difficult to explain but, how does one think when programming?
    Even though code differs from language to language, the thinking should not different from one to another?
    I'm learning the basics now but the i only understand them to a certain level, which leads to that I can't complete certain tasks.
    For example, I know how to write a simple for-loop, but when they get more advanced it gets more difficult

    Many programming tasks is linked to math in some way
    Do i need to be good at math in order to become a good programmer?

    I've also heard that writing solutions on paper first is good, but how is one going to do that when they don’t even know how to start?

    it's feels like I only understand some parts of code and sometimes forget them, and need to go back in books and check again, and even after I’ve checked, it's still hard to remember the code.

    Finding codes for different kinds of tasks is also hard...
    But the biggest problem is the thinking.


    This feels kind of hard because I am studying C programming in relative to hardware and that’s not exactly basics…


    Second:

    Here is a task of mine which i have no idea at all how to begin
    In this case I am using a vga monitor and Alteras DE2-115 education board.

    You will develop drivers for a VGA monitor
    The following device drivers must be developed:


    Function: print_pix(unsigned int x,unsigned int y,unsigned int rgb);
    Function description: Prints a pixel with the color rgb at the coordinate (x, y).

    Function: print_hline(unsigned int x_start, unsigned int y_start, unsigned int len,
    unsigned int RGB);
    Function description: Prints a horizontal line with the color rgb with the length len
    which starts at the coordinate (x_start, y_start).
    Function: print_vline(unsigned int x_start, unsigned int y_start, unsigned int len,
    unsigned int RGB);
    Function description: Prints a vertical line with the color rgb with the length len
    which starts at the coordinate (x_start, y_start).
    Function: print_char(unsigned int x,unsigned int y,unsigned int rgb,unsigned int
    BG_RGB,char Character);
    Function description: Prints the Character with the color rgb and the background
    color BG_RGB at the coordinate (x, y). NOTE! some characters are not supported by
    this function (only A, B and C (you can add some more if you like)). You can decide
    how the characters will be designed on the screen.
    read_pixel_ram_int(unsigned int x_start,
    unsigned int y_start); return: unsigned pixel_data
    Function description: Read the pixel_data from pixel RAM (3 bits, RGB) from
    address x and y (calculated from x and y).

    examples:
    // Prints the character A, with blue color and green background color
    // with the start coordinate (1, 2)
    print_char(1,2,1,2,’A’);
    // Prints a horizontal blue line with five pixel length at (11, 2)
    print_hline(11,2,5,1);
    // Prints a vertical blue line with six pixel length at (19, 2)
    print_vline(19,2,6,1);
    // Prints a red pixel at (11, 5)
    print_pix (11,5,4);
    These function calls prints the following items.



    Please help me understand how to think in C.-skärmklipp-jpg
    A VGA display signal is produced with the help of five signals. Two signals,
    horizontal sync and vertical sync, which are used for synchronization of the video. Three signals which are used to control the color. The color signals are Red, Green,
    and Blue. They are often collectively referred to as the RGB signals. By changing the
    analog levels of the three RGB signals all other colors can be produced This VGA
    Controller in this thesis supports 8 colors see figure below, no digital to analog
    conversion is made.
    Pix_nr = X + Y*320; //X = 1 – 320 Y = 1-239
    IOWR_ALTERA_AVALON_PIO_DATA(VGA_DATA_W_BASE,farg);
    // three bits that desides the color
    IOWR_ALTERA_AVALON_PIO_DATA(VGA_ADRESS_W_BASE,Pix_ nr);
    // Pix_nr = X + Y*320;
    IOWR_ALTERA_AVALON_PIO_DATA(VGA_CONTROL_W_BASE,1);
    // writing to picture memory
    IOWR_ALTERA_AVALON_PIO_DATA(VGA_CONTROL_W_BASE,0);
    // end writing
    Read from pixel memory
    IOWR_ALTERA_AVALON_PIO_DATA(VGA_ADRESS_W_BASE,Pix_ nr);
    // Pix_nr = X + Y*320;
    IOWR_ALTERA_AVALON_PIO_DATA(VGA_CONTROL_W_BASE,0);
    // end writing
    RGB_Value = IORD_ALTERA_AVALON_PIO_DATA(VGA_DATA_R_BASE);
    // colors

    Please, if u can help me in any way, it would be much appreciated!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    ouch. that looks pretty tough for a first assignment. as far as general advice on how to think in C, here's what I would do, especially dealing with hardware
    write down the detailed steps required to perform each specified function in English (or whatever) as if you were performing the calculations or actions by hand. ignore the programming language. but be precise and be sure you understand what the function is supposed to do.
    write a scaffold C program that implements the functions and a main program to test each one in turn, but leave the innards of the functions empty, don't access the hardware yet. this will get your program structure set up where you can compile it without errors.
    using what you wrote in step one, implement and test each function individually to the actual hardware. In this case you can think of the hardware as simply locations in memory that you read and write to.
    if a function is very complicated, break it into simple steps and test each one against the hardware until you have a good feel for how the hardware works.
    keep it all very incremental. do a little bit at a time.

    that said, if you haven't programmed in C at all, this is a challenging assignment.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    ouch is right.

    Yes, C, or any other language is just a tool to accomplish a solution to a problem.

    Several approaches come to mind when trying to solve a problem (and there are some great books on the subject - George Polya comes to mind). Most of your logic for a solution can be stated in common English:
    Code:
    IF( something is true ) do { this }
    or ELSE do { this }
    WHILE( something is true ) do {this }
    STORE, ASSIGN, READ, PROMPT, etc.
    Just think of it as writing a recipe to accomplish a task, as you would for creating a meal.

    And pen and paper are great tools for understanding a problem. From quickly jotting down simple math, to drawing the problem out or diagramming.

    Doing all this will not only make writing the code easier, but will often expose a simpler solution than you originally had.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Strik3r View Post
    It’s kind of difficult to explain but, how does one think when programming?
    One needs to think like one is giving instructions to a total moron... you gotta tell it every little thing in the right order.

    Compilers and computers are not smart... programmers are.


    Even though code differs from language to language, the thinking should not different from one to another?
    The mindset requred between (for example) C and C++ are as different as night and day. So much so that some C programmers (myself included) find the "Object oriented programming" of C++ almost totally indecipherable. Of course the opposite is also true, C++ programmers often have a very bad time with C's procedural approach.

    I'm learning the basics now but the i only understand them to a certain level, which leads to that I can't complete certain tasks.
    For example, I know how to write a simple for-loop, but when they get more advanced it gets more difficult
    You expected otherwise?

    Do i need to be good at math in order to become a good programmer?
    That depends what you're programming... In my work --dominantly reformatting files, inventory, etc-- complex math is almost never needed. In fact a very large part of programming is text manipulation (which C really sucks at) and repeated operations (loops).

    I've also heard that writing solutions on paper first is good, but how is one going to do that when they don’t even know how to start?
    (Here comes a collective groan from those who've heard this too often...)

    Programming is a 4 step process...

    1) ANALYSE your task until you understand it in minute detail... know exactly what is required of you.
    2) Based on your understanding from #1, work out a step by step PLAN to perform the required tasks.
    3) WRITE your code, according to your plan, working in blocks and testing as you go.
    4) TEST your final program making sure the behaviour is exactly as required.

    You probably won't do much with your computer until you hit step 3...

    The reason for this process is painfully simple; no matter how good you are, nobody can ever solve a problem they don't understand.
    (Hence the frequent reference to software as a "Solution")


    it's feels like I only understand some parts of code and sometimes forget them, and need to go back in books and check again, and even after I’ve checked, it's still hard to remember the code.
    The C standard library is a few hundred functions. The Windows API approaches 30,000 ... don't even try to remember them all. Get used to looking stuff up... I do it all the time, sometimes hundreds of times a day.


    Finding codes for different kinds of tasks is also hard...
    Make sure you have complete documentation for your compiler's library and any APIs you're working with. If you don't have it get it!
    No programmer worth salt would try to work without reference sources right at hand.


    This feels kind of hard because I am studying C programming in relative to hardware and that’s not exactly basics…

    Here is a task of mine which i have no idea at all how to begin
    In this case I am using a vga monitor and Alteras DE2-115 education board.

    You will develop drivers for a VGA monitor
    The following device drivers must be developed:
    Is your teacher INSANE? This is a task for 3rd year, not first semester!
    Last edited by CommonTater; 11-16-2011 at 08:47 PM.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I thought about it for a while and found it pretty much impossible to describe what exactly goes on in my head when I write code.

    I know that, consciously, I am alternating between a very high-level mental view and a very low-level mental view of the problem. I spend some time building out the framework of something, and when I feel like things aren't clear I will spend some time expressing some particular part of the code in complete detail. This usually helps to inform me as to what I should do next. So it goes up and down the scale of abstraction.

    It's important to alternate between the abstract and the specific, because a lot of times a design that seems good at a high level turns out to have serious problems stemming from a pesky implementation detail. Conversely, staying at the detailed level for too long may waste time, because you might be implementing something that turns out not to be necessary, or in fact works against the functioning of some other part of the program.

    It is very hard to put this stuff into words. There are plenty of authors who have done it far better than I ever could. All I can say for sure is that you gain the skill by doing (reading and writing real code), you will never figure it out by ONLY reading books.

    And I agree with the others that your assignment is ridiculously complicated for an introductory class. That sucks. Sorry.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User Strik3r's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    7
    Wow, THANKS for all the responses, I really appreciate it!!! =)

    I believe that I may have explained a few things wrong so ill describe it better this time.
    I’m studying a distance course which started in august which is about embedded systems, working with both hardware and software. This course is one year long and on the first semester (which is coming to an end soon), we are studying VHDL language and hardware related C programming.

    Now, I have barely studied programming, just a bit so I barely know the basics, but I’m far from the only one who’s having a hard time in this course. I have a few books which I’m learning from, but it takes time for the basics to melt in. And time is something I barely have if I’m to keep up with the deadlines

    As when it comes to the task I mentioned earlier I may also have expressed it bad.

    The task which I posted in my first post, is the one I’m currently at and it is the third task NOT the first one, but still a tough one.. Totally we have 5 tasks in C during this semester
    The first task we had was this:
    Designing an embedded system that controls eight LEDs
    Data input Led number
    7…0
    0 none
    1 1
    2 2
    3-8 3-8
    9 Every Led
    10 none




    Still, from this task to the one I posted first, it feels like quite a big jump….

    And since this is hardware related C programming, it should be different from standard C programming or am I wrong? What I mean is that I need to use codes that “speaks” to the hardware such as the codes that I posted for the third task: IOWR_ALTERA_AVALON_PIO_DATA(VGA_ADRESS_W_BASE,Pix_ nr);

    Right now I’m reading a lot of C now, from basics to more advanced code. Not many things get stuck in the head but as far as I have come, I have never come across any kind of chapter or code that looks to be even close to this kind of code. It doesn’t feel like C code at all.

    How is one supposed to learn all this stuff in barely one semester?
    Btw do you guys know more good programming forums like this one?
    VHDL forums seems hard to find..
    //Strik3r

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Strik3r View Post
    And since this is hardware related C programming, it should be different from standard C programming or am I wrong? What I mean is that I need to use codes that “speaks” to the hardware such as the codes that I posted for the third task: IOWR_ALTERA_AVALON_PIO_DATA(VGA_ADRESS_W_BASE,Pix_ nr);

    Right now I’m reading a lot of C now, from basics to more advanced code. Not many things get stuck in the head but as far as I have come, I have never come across any kind of chapter or code that looks to be even close to this kind of code. It doesn’t feel like C code at all.
    Wait till you see the Windows API
    It's not especially all that hard, but it sure is different.


    How is one supposed to learn all this stuff in barely one semester?
    One doesn't. Becomming a proficient C programmer can take *years*.

  8. #8
    Registered User Strik3r's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    7
    Believe it or not, I have actually managed (after a lot of help) to get a working code for my task. And here it is:

    Code:
    #include <stdio.h>
    #include <system.h>
    #include "altera_avalon_pio_regs.h"
    
    
    //Color definitions
    #define black 0
    #define blue 1
    #define green 2
    #define cyan 3
    #define red 4
    #define magenta 5
    #define yellow 6
    #define white 7
    
    
    //print_pix function
    unsigned int x;
    unsigned int y;
    unsigned int rgb;
    
    
    //Print_hline/print_vline
    unsigned int x_start;
    unsigned int y_start;
    unsigned int length;
    
    
    //Print_character function
    unsigned int pix_count = 64;
    unsigned int size = 8;
    unsigned int n = 0;
    unsigned int bg_rgb;
    char character;
    
    
    //Value for pixel-memory
    int rgb_value;
    
    
    //Arrays for the characters A/B/C
    int A[64] = {0,0,0,1,1,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0};
    
    
    int B[64] = {0,0,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0};
    
    
    int C[64] = {0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0};
    
    
    
    
                      //Print's a pixel
                      void print_pixel (alt_u16 x, alt_u16 y, alt_u8 rgb)
                     {
                         // The value (color) that will be written to an address
                         IOWR(VGA_DATA_W_BASE, 0, rgb);
                         // The address that the value will be written on
                         IOWR(VGA_ADRESS_W_BASE, 0, x + y * 320);
                         // Writes to picture-memory
                         IOWR(VGA_CONTROL_W_BASE, 0, 1);
                         // When written, stop writing to picture-memory
                         IOWR(VGA_CONTROL_W_BASE, 0, 0);
    
    
                     }
                      //Print horizontal line
                      void print_hline (alt_u16 x_start, alt_u16 y_start, alt_u16 length, alt_u8 rgb)
                     {
                         for(x = x_start; x <= x_start + length; x++)
                         print_pixel(x+x_start,y_start,rgb);
    
    
                     }
    
    
                      //Prints a vertical line
                      void print_vline (alt_u16 x_start, alt_u16 y_start, alt_u16 length, alt_u8 rgb)
                     {
                         for(y = y_start; y <= y_start + length; y++)
                         print_pixel(x_start,y+y_start,rgb);
    
    
                     }
                      //Prints a character (A/B/C only)
                      void print_chararcter (alt_u16 x, alt_u16 y, alt_u8 rgb, alt_u8 bg_rgb, char character)
                      {
                          //Start while-loop if both true
                          while ((pix_count > 0) && (character == 'C')){
    
    
                                                       while (size > 0){
                                                         //If Character is = 1, then print with the rgb color
                                                           if (C [n] == 1){
    
    
                                                               print_pixel(x, y, rgb);
    
    
                                                                }
                                                         //If Character is = 0, then print with the bg_rgb color
                                                           else{
                                                               print_pixel(x, y, bg_rgb);
    
    
                                                           }
    
    
                                                            pix_count = pix_count - 1;
                                                            size = size - 1;
                                                            n++;
                                                            x++;
                                                           }
                                                       x = x - 8;
                                                       size = 8;
                                                       y++;
                                                     }
                      }
    
    
                       // Read pixel-ram value
                       int read_pixel_ram_int (alt_u16 x_start, alt_u16 y_start)
                      {
                          //Store value in rgb_value
                          int rgb_value;
                          //Pix_nr =x+y*320
                          IOWR(VGA_ADRESS_W_BASE, 0, x + y * 320);
                          //When written, stop writing to picture-memory
                          IOWR(VGA_CONTROL_W_BASE,0,0);
                          //The color that will be read.
                          rgb_value=IORD_ALTERA_AVALON_PIO_DATA(VGA_DATA_R_BASE);
                          //Return the value
                          return rgb_value;
                      }
    int main(){
    
    
    
    
          //Example print_xxxxx (X,Y,LENGTH,COLOR,CHARACTER);
                     print_pixel(23,23,cyan);
                     print_pixel(23,24,green);
                     print_pixel(23,25,red);
                     print_pixel(24,24,magenta);
                     print_hline(50,50,320,yellow);
                     print_hline(10,5,200,blue);
                     print_vline(77,20,239, magenta);
                     print_vline(78,20,100, white);
                     rgb_value = read_pixel_ram_int(23,23);
                     print_chararcter(150,100,4,blue,'C');
                     printf("Pixel value: %d",&rgb_value);
    However there is a part of it which i did not make myself entierly and would be happy if anyone could describe this part of code for me since the person who wrote that part is out of contact for a while...
    The part i need described is "print a character" function (in detail please).

    The main idea i had to write a letter was to use an 8X8 array consisting on "1" and "0", where the "1" is representing the letter and the "0" the background color and the array looked like this:
    Code:
    unsigned char a[8][8] = {
    {0,0,0,1,1,0,0,0},
    {0,0,1,0,0,1,0,0},
    {0,1,0,0,0,0,1,0},
    {0,1,0,0,0,0,1,0},
    {0,1,1,1,1,1,1,0},
    {0,1,0,0,0,0,1,0},
    {0,1,0,0,0,0,1,0},
    {0,1,0,0,0,0,1,0},
    };
    This is supposed to represent the letter "A"
    But then my firend found it easier to remake this for some reason.

    Also i wonder if it is possible to somehow remake this code (except "main") into less code but still working out the same way?? and if yes, then how??

    Also, the letter gets kind of small on the screen, can i make the letter bigger without having to write a bigger array??

    It seems to me that the backgroundcolor has restricted itself to green, or at least I havn't managed to chage it, any ideas?

    //Strik3r

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't understand this
    By .C-Man. in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2011, 07:01 AM
  2. please help me understand this
    By litzkrieg in forum C Programming
    Replies: 8
    Last Post: 02-16-2011, 01:44 PM
  3. Can someone help me understand this plz?
    By Arex Bawrin in forum C Programming
    Replies: 5
    Last Post: 02-18-2010, 06:42 PM
  4. Don't Understand
    By Trckst3 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2008, 11:58 PM
  5. anyone understand this?
    By Da Beav in forum C Programming
    Replies: 6
    Last Post: 09-06-2001, 12:39 PM