An warware: cika tsararru da 0

Tabbas, bari mu fara.

A cikin wannan labarin, muna tattauna matsalar gama gari da ke fuskantar masu haɓakawa ta amfani da yaren shirye-shiryen C++: yadda ake cika tsararru da sifili. Yana ɗaya daga cikin waɗannan ayyuka waɗanda suke kama da na farko, duk da haka yana da mahimmanci a fahimci yadda ake aiwatar da shi yadda ya kamata don kula da ingantaccen aikin lamba. A cikin C++, akwai hanyoyi da yawa don cika tsararru tare da 0, kuma za mu yi nazarin waɗannan hanyoyin cikin zurfi don fahimtar tasirin su akan aiwatar da lambar ku.

A cikin C++, tsararraki tsarin bayanai ne da suka ƙunshi nau'ikan abubuwa iri ɗaya, ana adana su gabaɗaya a cikin ƙwaƙwalwar ajiya. Ɗaya daga cikin sifofin farko na tsararraki a cikin C++ shine cewa suna da ƙayyadaddun girman. Wannan ƙayyadaddun sifa mai ƙayyadaddun ƙayyadaddun ƙayyadaddun ƙayyadaddun ƙayyadaddun ƙayyadaddun ƙayyadaddun ƙayyadaddun ƙayyadaddun yanayin yakan haifar da yanayin da muke buƙatar farawa ko cika tsararru tare da takamaiman ƙima, kamar 0.

Maganin Matsala

#include
#include
int babban () {
int arr[5];
std :: cika_n (arr, 5, 0);
don (int i = 0; i<5; i++) {std::cout << arr[i] << "; } dawo 0; } [/ code] Wannan lambar ta cika int array tare da sifili ta amfani da aikin std :: fill_n.

Bayanin mataki-mataki na Code

Da farko, an ayyana int array arr na girman 5. Muna amfani da aikin std :: fill_n don cika tsararru da sifili. Aikin fill_n na cikin algorithm library wanda ya zo daidai da C ++. Babban taken algorithm yana bayyana ayyuka don dalilai daban-daban (misali, bincike, rarrabawa, sarrafa) waɗanda zamu iya amfani da su akan jeri na abubuwa. A wannan yanayin, ana amfani da fill_n don cike kewayon abubuwa tare da ƙima - a wannan yanayin, sifili.

Aikin fill_n yana ɗauka a cikin muhawara guda uku - na farko shine farkon adireshin tsararru, na biyu shine girman tsararrun, kuma na uku shine ƙimar da za a cika shi. A cikin wannan snippet, muna gaya masa cewa a fara a farkon tsararrun arr, a cika abubuwa 5, kuma a cika shi da sifili.

Laburaren da ke ciki: Algorithm

Wannan ɗakin karatu na algorithm wani yanki ne na daidaitaccen ɗakin karatu na C++ kuma yana ɗaukar jerin ayyuka waɗanda akasari ake amfani da su don yin ayyuka akan tsararru da sauran tsarin bayanai. Kasancewar irin wannan ɗakin karatu yana sauƙaƙa da yawa ayyuka na ƙididdigewa, yana 'yantar da mai haɓakawa daga rubuta layukan lambobi da hannu da hannu, kuma ta haka inganta inganci da iya karantawa.

Wata Hanyar Cika Tsari tare da Sifili

Akwai wasu hanyoyin kuma, kamar amfani da madauki don saita kowane kashi da hannu zuwa sifili.

int babban () {
int arr[5];
don (int i = 0; i<5; i++) {arr[i] = 0; } dawo 0; } [/code] A cikin wannan hanyar, mun yi madauki a sarari a kan kowane ɓangaren tsararrun kuma mun saita ƙimarsa zuwa 0. Ko da yake ɗan tsayi kaɗan, yana da sauƙin fahimta ga masu farawa. Amma a fili, hanyar yin amfani da std :: fill_n da ɗakin karatu na algorithm ya fi dacewa kuma an fi so.

Bambanci Tsakanin Hanyoyi Biyu

Yayin da sakamakon ƙarshe ya zama iri ɗaya, hanyoyin biyu sun bambanta wajen aiwatar da su. Hanya ta farko ta amfani da std ::fill_n yana da sauri kuma mafi inganci saboda yana amfani da algorithms na musamman don cika tsararru tare da 0. Hanya na biyu mai sauƙi ne kuma mai sauƙin fahimta, amma yana da hankali tun da hannu yana sanya darajar 0 ga kowane kashi na XNUMX. tsararru.

Ta hanyar fahimtar hanyoyi daban-daban don cika tsararraki tare da 0 a cikin C++, mutum na iya haɓaka ingancin lambar da iya karantawa. Tuna mahimmancin amfani da ɗakunan karatu, yayin da suke ba da izini don mafi tsafta kuma mafi ƙayyadadden lamba, yana haifar da kyakkyawan aiki gabaɗaya.

Shafi posts:

Leave a Comment