Dismiss Notice
You are browsing this site as a guest. It takes 2 minutes to CREATE AN ACCOUNT and less than 1 minute to LOGIN

Wakuu msaada wa C programming please

Discussion in 'Tech, Gadgets & Science Forum' started by Papizo, Jan 26, 2011.

  1. P

    Papizo JF-Expert Member

    #1
    Jan 26, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160
    Wakuu naombeni msaada wenu kugundua nini kimekosekana kwenye hii program yangu, najaribu kuicompile kutumia gcc filename -o filename but lakini naona inakubali lakini hainipi result ninazo taka mimi sasa sijui ni kwa nini au imekosekana nini.......hebu check hapa chini program yenyewe

    //************************************************** *******
    //point5.c - Demo of use of pointers. Pointers and functions.
    //The function call passes arguments that are addresses of two variables x and y.
    //The function, using the addresses of x and y modifies their content.
    //The main function then displays the content of x and y.
    //************************************************** ***********/

    #include <stdio.h>

    void addconst(int *px, int *py); // very clear function prototype

    int main(void)
    {
    int x = 2, y = 4; //integer variables

    printf("\n\npoint5.c Demo of pointers\r\n");
    printf("=========================\r\n");

    printf("Before function call :x = %d :y = %d\n\n", x , y);
    printf("The function adds 3 to x and 6 to y indirectly by using the\n");
    printf("addresses of the variables.\n\n");

    //addconst( &x, &y ); // Addresses passed to function




    printf("After function executed :x = d :y = d",x,y);

    return 0;
    }// end of main function


    //addconst
    //function is passed the addresses of two variables x and y
    //and then uses these addresses to alter the content of the variables
    void addconst( int *px , int *py)
    {
    *px = *px + 3;
    *py = *py + 6;

    }//end of function


    Hiyo ndio program yenyewe sasa hapa nime compile kwa gcc point5.c -o point then haionyeshi error alafu naweka point5 kunionyesha result,lakini majibu ninayopata hapa sio ambayo mimi ninayotaka inanipa result tofauti ambayo ni

    point5.c Demo of pointers
    =========================
    Before function call :x = 2 :y = 4

    The function adds 3 to x and 6 to y indirectly by using the
    addresses of the variables.

    After function executed :x = d :y = d


    so hapo nilipoweka red ilibidi jibu lake liwe kama x= 5 na y=10 lakini sasa inanipa result ya x=d and y=d so not really sure where nimeenda wrong kwenye hiyo program!

    Kama mnaweza kuumiza kichwa naomba kufundishwa na kukosolewa au kama kuna kitu inabidi ni add ili niweze kupata hizo result nitashukuru sana...
     
  2. redSilverDog

    redSilverDog JF-Expert Member

    #2
    Jan 26, 2011
    Joined: Sep 18, 2010
    Messages: 486
    Likes Received: 3
    Trophy Points: 0

    nadhani hio "d" kama ni string formating basi si ingekuwa na ki2 b4! kama variable haijawa declared!

    Na sina hakika kama function itakuwa executed coz iko kwanye comments "//"!

    sina compiler, so im sorry I cudnt try it!
     
  3. Mtazamaji

    Mtazamaji JF-Expert Member

    #3
    Jan 26, 2011
    Joined: Feb 29, 2008
    Messages: 5,972
    Likes Received: 27
    Trophy Points: 0
    Unaweza kunimbia hio line ina maana gani
    void addconst(int *px, int *py) -- hivyo vinyota vina maana gani

    Na kwa kwa nini hii function ya void addconst(int *px, int *py) umeiweka na kui declare kabla ya main funcntion na sio ndani au baada ya main()

    Kama sikosei kwenye C wanasema function zote ni watoto wa main()

    NB.
    Sio kwamba ninakukuosoa ila napenda kujua .kwa kweli ni mchovu wa Cna ni siku nyingi sana napenda kuchemsha kichwa tu

    Hiyo * ina maanisha nini sorry kukurudisha nyuma napenda kujikumbusha
     
  4. Mchizi

    Mchizi JF-Expert Member

    #4
    Jan 26, 2011
    Joined: Apr 23, 2009
    Messages: 304
    Likes Received: 97
    Trophy Points: 45
    hivyo vinyota ni pointer to variable, na hiyo line maana yake ni prototype function, in c programming function should be declared first the same as global variables kabla hujazitumia toofauti na language kama java ambayo huitaji ku declare methods mwanzoni. Hapo naona unapata wrong results sababu hiyo function call kwenye main function ume icomment kwa hiyo hakuna inachofanya,kwa hiyo kilichotokea imeprint line chini baada ya hiyo comment
    printf("After function executed :x = d :y = d",x,y);
     
  5. Mtazamaji

    Mtazamaji JF-Expert Member

    #5
    Jan 26, 2011
    Joined: Feb 29, 2008
    Messages: 5,972
    Likes Received: 27
    Trophy Points: 0
    Ok thank you very much je kwa hiyo ni sawa alivyofanya kuideclare kabla ya main()
     
  6. P

    Papizo JF-Expert Member

    #6
    Jan 26, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160

    Thanks mkuu kwa msaada na nashukuru sana, Nadhani nimejaribu kucheza na hii program toka mchana lakini still sijui tatizo lipo sehemu gani,nimejaribu ku compile hiyo kitu lakini inanipa kila kitu powa ila result tofauti,unadhani hiyo ndio naweza kuwa nimekosea hapo??

    N.B ni kweli kabisa hiyo d haina kazi kabisa ilibidi iwe hivi %d ndio program iweze kufanya kazi........Thanks again
     
  7. P

    Papizo JF-Expert Member

    #7
    Jan 26, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160

    Mkuu kwa kweli hata mimi mwenyewe najaribu na wala usijali kabisa na nakubali kabisa kukosolewa kwa mtu yoyote yule na bila kuona aibu maana mimi mwenyewe nahitaji kufahamu kwenye hizi nyanda....Nadhani mkuu MCHIZI amejibu kila kitu au hizo star(*) kwa jina lingine tunaweza kuziita pointer data type..Anything tuulizane tu nadhani hata mimi nahitaji kufundishwa zaidi ya hapa!!
     
  8. P

    Papizo JF-Expert Member

    #8
    Jan 26, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160


    Mkuu nadhani hilo nalo swali zuri,yes lazima main () ndio iwe njia ya kila kitu lakini kwa hiyo program naona hata nikiweka hivyo inawork fine so sidhani kama hiyo nayo ni problem, nimeicheck ila naona problem haipo hapo!!
     
  9. redSilverDog

    redSilverDog JF-Expert Member

    #9
    Jan 26, 2011
    Joined: Sep 18, 2010
    Messages: 486
    Likes Received: 3
    Trophy Points: 0
    Daaah! umenifanya nikatafute macompiler, so here it works!.. Haya ndo maswali ya kuuliza.. :ranger:

    Code:
    
    //************************************************** *******
    //point5.c - Demo of use of pointers. Pointers and functions.
    //The function call passes arguments that are addresses of two variables x and y.
    //The function, using the addresses of x and y modifies their content.
    //The main function then displays the content of x and y.
    //************************************************** ***********/
    
    #include <stdio.h>
    
    void addconst(int *px, int *py); // very clear function prototype
    
    int main(void)
    {
    int x = 2, y = 4; //integer variables
    
    printf("\n\npoint5.c Demo of pointers\r\n");
    printf("=========================\r\n");
    
    printf("Before function call :x = %d :y = %d\n\n", x , y);
    printf("The function adds 3 to x and 6 to y indirectly by using the\n");
    printf("addresses of the variables.\n\n");
    
    addconst( &x, &y ); // Addresses passed to function
    
    
    
    
    printf("After function executed :x = %d :y = %d",x,y);
    
    return 0;
    }// end of main function
    
    
    //addconst
    //function is passed the addresses of two variables x and y
    //and then uses these addresses to alter the content of the variables
    void addconst( int *px , int *py)
    {
    *px = *px + 3;
    *py = *py + 6;
    
    }//end of function
    
    sijawahi fanya C, this was my first time!.. lakini haina tafauti na C++ niliofanya..
     
  10. MNDEE

    MNDEE JF-Expert Member

    #10
    Jan 26, 2011
    Joined: Jul 10, 2009
    Messages: 494
    Likes Received: 6
    Trophy Points: 0
    Wakuu mimi sio mtu wa C, lakini natatizwa na utumiaji wa one variable d (not a multvariable attribute) na somehow tutegemee irudishe two different values :x = d :y = d ?!

    umedeclare int x =2, na y = 4
    then x = x + 3 // 5
    y = y + 6 //10

    ni vipi utapata different results wakati :x = d :y = d


    - mbona addconstant() method umeicomment out?
    - hapa naona unadisplay tu x and y the rest in "message" printf("Before function call :x = %d :y = %d\n\n", x , y);

    Iangalie tena program yako, error itakuwepo kama kuna syntax issue otherwise cha msingi je unapata output uliyotegemea.
     
  11. Zing

    Zing JF-Expert Member

    #11
    Jan 26, 2011
    Joined: Jun 24, 2009
    Messages: 1,780
    Likes Received: 15
    Trophy Points: 0
    haya ndo mambo ngoja niistall turbo borland C++ nami nijifunze

    mkubwa japo nami sio mtaalma wa prg cha muhimu sio program itoejibu sahihi cha muhimu program logic iwe sahihi.
    kwahiyo usisitize prog ikupe jibu 10 tu.

    tupe somo program logic yake ilikuwa na kosa wapi?
     
  12. redSilverDog

    redSilverDog JF-Expert Member

    #12
    Jan 26, 2011
    Joined: Sep 18, 2010
    Messages: 486
    Likes Received: 3
    Trophy Points: 0
    tupe somo program logic yake ilikuwa na kosa wapi?[/QUOTE]

    kwanza ukiangalia wakati anaicall ile function, alieka comment so, ilikuwa haitwi ile "addconst" function. the pale kwenye "y=d" nimeeka "y=%d".. ushaelewa?

    Naming Conventions:

    function: addConst
    class: AddConst
     
  13. YeshuaHaMelech

    YeshuaHaMelech JF-Expert Member

    #13
    Jan 26, 2011
    Joined: Oct 12, 2010
    Messages: 2,624
    Likes Received: 2
    Trophy Points: 0
    ndugu,
    nafikiri unahitaji ku brush kidogo C. Kifupi tu ni kuwa unapopitisha argument kama array ktk C/C++ compiler inapitisha pointer to the first element. Kwa hiyo ni kama unapitisha array of int. Pass the variables by reference if you want to modify them directly. You have just opened the can of worms!
    Code:
    void addConst(int& x, int& y){
    //modify the x and y and they will be modified out of the function also
    y=y+2;
    x=x+5;
    }
    
    you need to re write again. Just a note, simple program like this needs no comment. Next time just put in a code and whenever someone does not understand a line will ask
     
  14. YeshuaHaMelech

    YeshuaHaMelech JF-Expert Member

    #14
    Jan 26, 2011
    Joined: Oct 12, 2010
    Messages: 2,624
    Likes Received: 2
    Trophy Points: 0
    just ignore the quoted post. That will work with C++ but not C. OP is right on his way of using pointers. Here is a working code

    Code:
    #include <stdio.h>
    
    void addConst(int* , int*); 
    
    int main(){
        int x = 2, y = 4; 
        printf("Before function call x = %d y = %d\n\n", x , y);
        printf("The function adds 3 to x and 6 to y indirectly by using the ");
        printf("addresses of the variables.\n");
        addConst(&x, &y); 
        printf("After function executed x = %d y = %d\n",x,y);
        return 0;
    }
    
    void addConst(int* x, int* y){
        *x = *x + 3;
        *y = *y + 6;
    }
    
     
  15. Mtazamaji

    Mtazamaji JF-Expert Member

    #15
    Jan 26, 2011
    Joined: Feb 29, 2008
    Messages: 5,972
    Likes Received: 27
    Trophy Points: 0
    Nadani mkuu atakuwa kapata suluisho kutoka kwa vichwa vya programming. Hii thead imeongeza afya ya ubongo wangu
     
  16. P

    Papizo JF-Expert Member

    #16
    Jan 27, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160
    Kabisa ndio kujifunza lakini....hata mimi na mpango wa kuijua kiundani zaidi C++
     
  17. YeshuaHaMelech

    YeshuaHaMelech JF-Expert Member

    #17
    Jan 27, 2011
    Joined: Oct 12, 2010
    Messages: 2,624
    Likes Received: 2
    Trophy Points: 0
    papizo, feedback muhimu ili kujua post zetu zimekusaidiaje/zimekuangushaje!
     
  18. P

    Papizo JF-Expert Member

    #18
    Jan 27, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160

    Yap mkuu at the end solution imepatikana sasa na kila kitu kipo fine kabisa.......
     
  19. P

    Papizo JF-Expert Member

    #19
    Jan 27, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160


    Kabisa mkuu hii kitu ni nzuri lakini inaumiza sana kichwa............
     
  20. P

    Papizo JF-Expert Member

    #20
    Jan 27, 2011
    Joined: Feb 24, 2008
    Messages: 4,237
    Likes Received: 138
    Trophy Points: 160
    kwanza ukiangalia wakati anaicall ile function, alieka comment so, ilikuwa haitwi ile "addconst" function. the pale kwenye "y=d" nimeeka "y=%d".. ushaelewa?

    Naming Conventions:

    function: addConst
    class: AddConst[/QUOTE]



    Thanks mkuu problem ipo solved sasa kutokana na michango ya nyinyi wakuu lakini nilikuwa bado sijagundua kabisa kama ndio problem
     
Loading...