Strings in C

Discussion in 'C' started by HelpF2, Feb 10, 2016.

  1. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Hi.

    I need to check how many numbers in strings of 16 numbers is less than for an example 6.

    I only need a main rutine..
    Below is my part of code but doesnt work

    Code:
    byte a[16]={3,22,31,9,11,6,7,8,23,17,10,30,73,40,55,93};
     byte b[16]={11,14,7,13,17,19,97,2,12,128,1,124,123,122,33,21};
      byte c[16]={9,15,4,13,2,49,57,3,74,47,39,59,112,100,91,6};
       byte d4[16]={33,58,68,66,21,19,97,3,1,2,5,12,27,9,15,11};
    
    
    for (i=0;i<16;i++){
                               if(a[i]<6) count1++;
                                    display=count1 ;
    
                               if(b[i]<6) count2++;
                                display=count2;
                               if(c[i]<6) count3++;
                              display=count3;
                               if(d[i]<6) count4++;
                               display=count4;  }
    
    
    

    display is variable which is memory mapped (led display)

    Maybe I need 4 FOR loop or?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, you should be able to do it in one for loop. How exactly doesn't it work? Does it compile OK? If not, what errors do you get? Do you get any warnings? If it compiles and links OK, what happens at runtime?

    Are your countN variables initialised to zero before the for loop?

    Does it matter that you copy all four counts into the same display variable? I don't know how your hardware works of course, so it may be that the hardware interprets "display=1; display=2;" to mean "display 12 on the LEDs". Or do you need to do something like display[0]=1; display[1]=2;"?
     
  3. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0

    For that code compiler is OK. Without errors.
    But he check only one string not all. And he didnt count well
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Which string, what count did you get, and what count were you expecting?
    (Remember when responding we can't read your mind)
     
  5. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I got only first string.. The rest couldnt read.
    I expect to count in each string how many numbers have which are less than six.
    Evrery counting i must set on display (which is memory mapped) cyclically....
     
  6. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Maybe I can do that with 4 loop of FOR -one FOR loop for one string?
    So, I asked can I do that and how?
     
  7. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I need only code in program language C, my hardware supports programming in C.
    I have simulator which have display (led diodes)
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Can you write a simpler program, one that just displays "4321" on the screen, then post the whole code?

    You're not going to be able to do this with four for loops if you can't do it with one.
     
  9. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    This is for ONE STRING

    Code:
    #include <reg51.h>
    
    typedef unsigned char byte;
    
    byte string1[16]={3,22,31,9,11,6,7,8,23,17,10,30,73,40,55,93};
    
    byte i=0;
    byte counter;
    byte a;
    byte count1=0;
    
    byte xdata display _at_ 0x8000;
    
    void Inicialization(void) {
     EA=1;
     ET0=1;
     TMOD=0x01;
     TH0=0x00;
     TL0=0x00;
     counter=0;
     i=0;
     TR0=1;
    }
    
    void timer0 (void) interrupt 1 using 2 {
     TR0=0;
     TH0=0x3C;
     TL0=0xB0;
     counter++;
    if (counter==20) { 
    
    a= string1[i];
    
    for (i=0;i<16;i++) {
                                if(a<6) count1++; 
                                           display=count1;
    }
    counter=0;
    
    }
     
     TR0=1;
    }
    
    void main(void) {
     Inicialization();
     while(1){}
      
     }
    
                                  
    
    
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re-read my question please. That is not what I asked you for and does not answer my question.
     
  11. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    For 4321 code is:



    Code:
    
    #include <reg51.h>
    
    typedef unsigned char byte;
    
    byte string1[16]={4,3,2,1};
    
    byte i=0;
    byte counter;
    byte a;
    byte count1=0;
    
    byte xdata display _at_ 0x8000;
    
    void Inicialization(void) {
     EA=1;
     ET0=1;
     TMOD=0x01;
     TH0=0x00;
     TL0=0x00;
     counter=0;
     i=0;
     TR0=1;
    }
    
    void timer0 (void) interrupt 1 using 2 { 
     TR0=0;
     TH0=0x3C;
     TL0=0xB0;
     counter++;
    if (counter==20) { 
    
    a= string1[i];
    
    for (i=0; i<3; i++)     { 
    display= a;
    }
    }
    counter=0;
    
    }
     
     TR0=1;
    }
    
    void main(void) {
     Inicialization();
     while(1){}
      
     }
    
    
    
    
     
  12. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    This is better code for 4,3,2,1

    Code:
    #include <reg51.h>
    
    typedef unsigned char byte;
    
    byte string1[4]={4,3,2,1};
    
    byte i=0;
    byte counter;
    byte a;
    byte count1=0;
    
    byte xdata display _at_ 0x8000;
    
    void Inicialization(void) {
     EA=1;
     ET0=1;
     TMOD=0x01;
     TH0=0x00;
     TL0=0x00;
     counter=0;
     i=0;
     TR0=1;
    }
    
    void timer0 (void) interrupt 1 using 2 {
     
     TH0=0x00;
     TL0=0x00;
     counter++;
    if (counter==200) { 	  counter=0;
    
    
    
    			  if (i<4){                         
                                           display=string1[i];
    										 i++;   }
    			if (i==4){TR0=0;	 }
    							  
    
    }
      TR0=0;
     
    }
    
    void main(void) {
     Inicialization();
     while(1){
     			TR0=1;
     }
      
     }
    
    
    
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Seems unnecessarily overcomplicated. Why can't you just do:
    Code:
    display=4;
    display=3;
    display=2;
    display=1;
    
    ?

    From Googling reg51.h I'm guessing you're using an 8051 microcontroller, would that be correct?
    Could you describe the display to me please? Pretend I can't see it at all and that I'm trying to recreate it from a bunch of components.
    How many digits can it display at once? Is it a collection of 7-segment displays, or dot matrix, or an LCD screen, or something else?

    Also could you read this carefully and answer it in full please: Which string, what count did you get, and what count were you expecting? ("I got the first string" doesn't come close to answering it.)

    Remember there is still a lot I don't know about your particular development platform.

    Hopefully if you give me all the answers to these questions I'll be able to point out why it doesn't work. And I'll also need your code in full please, not just the string definitions and for loop.
     
  14. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I working with matric dislpay. I use only 8 diodes in row . When each diode is on that represent number in bits...I dont need 7-segment displays and LCD..
    SO , I cant put on display what you want. I can put counting in bits 4,3,2,1 on 8 diodes.

    I wrote you all code .
    I asked you how can I check all strings with FOR loop on display,nothing else ?
    So, I need to write on display which HOW MANY NUMBERS HAVE EACH STRING which are less than.
    If you know, please tell me. That is programming in C,nothing special.
    Also, if you dont know how that work in C please tell me. Because, I dont have too much time.

    You do not need to worry what is display.
    I need only part of code which checking 4 strings and puting on display (variable display is memory mapped at adress,that is clear)
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    After 25+ years of professional programming in C and C++ on multiple platforms including some hobbyist PIC programming I know more than enough C to answer your question.

    What I don't know is outlined in the questions I have asked you. You are not using the only programming platform in existence.

    You wanna my help you ansa my questions. You no wanna ansa then you no getta da help. I can't solve this at the moment due to too much missing info.
     
  16. HelpF2

    HelpF2 New Member

    Joined:
    Feb 10, 2016
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    O.K., O.K.
    Everything is clear..

    Bye
     
  17. senthil_a4nlabs

    senthil_a4nlabs New Member

    Joined:
    Aug 27, 2016
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    string is one of inut and output element which contain both integer and string elements inside..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice