Wakuu msaada wa C programming please

Papizo

JF-Expert Member
Feb 24, 2008
4,944
1,429
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...
 
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...


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!
 
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.......................................

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
 
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()

NB.
Sio kwamba ninakukuosa ila napenda kujua kwa kweli ni mchovu wa C napenda kuchemsha kichwa tu

Hiyo * ina maanisha nini soory kukurudiha nyuma napenda kujikumbusha

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);
 
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);

Ok thank you very much je kwa hiyo ni sawa alivyofanya kuideclare kabla ya main()
 
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!


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
 
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


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!!
 
Ok thank you very much je kwa hiyo ni sawa alivyofanya kuideclare kabla ya main()



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!!
 
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..
 
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...

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.
 
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.

RedSilverDog said:
sijawahi fanya C, this was my first time!.. lakini haina tafauti na C++ niliofanya..

tupe somo program logic yake ilikuwa na kosa wapi?
 
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
 
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
 
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
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;
}
 
Nadani mkuu atakuwa kapata suluisho kutoka kwa vichwa vya programming. Hii thead imeongeza afya ya ubongo wangu
 
Daaah! umenifanya nikatafute macompiler, so here it works!.. Haya ndo maswali ya kuuliza.. :ranger:

Mkuu ndio hivyo inabidi kusaidiana kimtindo na nashukuru sana kwa moyo wako na program inafanya kazi vizuri kabisa na ina work bila tatizo....Pia mkuu kama inawezekana unaweza ukawapa wadau na wenyewe maana wengine hawana compiler kwa ajili ya hiyo program so ungeweza labda kuwajulisha wewe unatumia nini kui compile...........


sijawahi fanya C, this was my first time!.. lakini haina tafauti na C++ niliofanya..

Kabisa ndio kujifunza lakini....hata mimi na mpango wa kuijua kiundani zaidi C++
 
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 ?!

Mkuu ni kweli ndio hapo wakuu hapo juu wamekuja kufind out kwamba hiyo ndio problem iliyokuwepo kwenye hiyo program ila kila kitu kingine ni sawa kabisa...........

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?

Hiyo ipo fine haiwezi kuleta tatizo mkuu ni sawa hapo ilivyo.......


- 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.


Yap mkuu at the end solution imepatikana sasa na kila kitu kipo fine kabisa.......
 
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?



Kabisa mkuu hii kitu ni nzuri lakini inaumiza sana kichwa............
 
tupe somo program logic yake ilikuwa na kosa wapi?

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
 

Similar Discussions

0 Reactions
Reply
Back
Top Bottom