Massivni e`lon qilish - Узбекистон Республикаси Олий ва Урта Махсус

//Haftalik o’rtacha tempraturani hisoblash dasturi include include using namespace std; include using namespace std; int main () { int x[ ] = {1, 2, 56, 78, -7, -45, 34, 12, 9, -1}; int s=0, n=0; include include using namespace std; int main () { int x[10], s=0, i, n=0;include using namespace std; int main () { float x[ ]={1,2.23,5.6,-78,-7,-45.12,34.0,12,9,-1}; float s, min, max; include include using namespace std; int main ( ) { float a[10], b[5], c[5]; for (int i=0; i{ c[i] = a[2*i +1]; b[i] = a[2*i]; cout cout getch ( ); include include using namespace std; int main ( ) { float a[5], b[5], c[10]; int i; for ( i=0; i |
Узбекистон Республикаси Олий ва Урта Махсус
|
bet | 6/15 | Sana | 12.02.2024 | Hajmi | 148,64 Kb. | | #154895 |
Bog'liq 20-topshiriq tayyorBu sahifa navigatsiya:
- //Haftalik o’rtacha tempraturani hisoblash dasturi include include using namespace std;
- include using namespace std; int main () { int x[ ] = {1, 2, 56, 78, -7, -45, 34, 12, 9, -1}; int s=0, n=0;
- include include using namespace std; int main () { int x[10], s=0, i, n=0;
- include using namespace std; int main () { float x[ ]={1,2.23,5.6,-78,-7,-45.12,34.0,12,9,-1}; float s, min, max;
- include include using namespace std; int main ( ) { float a[10], b[5], c[5]; for (int i=0; i
- { c[i] = a[2*i +1]; b[i] = a[2*i]; cout cout getch ( );
- include include using namespace std; int main ( ) { float a[5], b[5], c[10]; int i; for ( i=0; i
Massivni e`lon qilish:
char ch[4]={‘a’, ‘b’, ‘c’, ‘d’}; //simvolli massiv
int in[6]={10, 20, 30, 40}; // butun sonli massiv
char str[]="abcd"; //satr uzunligi 5 ga teng, chunki uning oxiriga ‘\0’ belgisi qo’shiladi
char str[]={'a', 'b', 's', 'd'}; // YUqoridagi satrning boshqacha yozilishi
Misol. Bir hafta ichidagi kundalik temperatura berilgan bo’lsa, hafta uchun o’rtacha temperaturani hisoblash dasturini tuzish:
//Haftalik o’rtacha tempraturani hisoblash dasturi
#include
#include
using namespace std;
int main()
{
const int n=7;
int temp[n];
int i, s, or_tem;
s=0;
cout<<"\n";
cout<<" Kunlik tempraturani kiriting: \n";
cout<<"\n";
for( i=1; i<=n; i++)
{ cout<< " temp["< cin>>temp[i]; s=s+temp[i]; }
or_tem=s/n;
cout<<" ----------------------------------------";
cout<<"\n";
cout<<" Haftalik o’rtacha tempratura = "<
getch();
return 0;
}
Misollar kurib o’tamiz.
1-misol. Massiv elementlaridan musbatlarining soni va summasini hisoblash dasturi.
# include
# include
using namespace std;
int main ()
{ int x[ ] = {1, 2, 56, 78, -7, -45, 34, 12, 9, -1};
int s=0, n=0;
for (int i = 0; i < 10; i++)
{ if (x[i] < 0) continue;
s = s + x[i]; n++;}
cout<<"\n";
cout<<"musbat elementlar soni n="<
cout<<"\n";
cout<<"musbat elementlar yig`ndisi s="<
getch ( );
}
Yoki huddi shu dasturni for va kiritish operatori cin orqali massivning 10 ta elementlarini kiritib hisoblash dasturini tuzish ham mumkin
# include
# include
using namespace std;
int main ()
{
int x[10], s=0, i, n=0;
for (i=0; i<10; i++)
{ cout<<" x["<> x[i];}
for (i=0; i<10; i++)
{ if (x[i]<0) continue;
s =s+x[i]; n++ ;
}
cout<<"\n";
cout<<"musbat elementlar soni n="<
cout<<"\n";
cout<<"musbat elementlar yig`ndisi s="<
getch ( );
}
2-misol. 10 ta elementdan iborat massivning eng katta, eng kichik elementlarini va ularning o’rta qiymatini hisoblash dasturi.
#include
#include
using namespace std;
int main ()
{
float x[ ]={1,2.23,5.6,-78,-7,-45.12,34.0,12,9,-1};
float s, min, max;
int i;
min = x[0]; max=x[0];
for (i=0; i<10; i++)
{ if (min > x[i]) min = x[i];
if (max < x[i]) max = x[i]; }
s = (min + max) / 2;
cout << "\n";
cout <<" min= "<
cout <<" max= "<
cout <<" o'rta qiymat = "<
getch ( );
}
3-misol. Bazaviy toifasi haqiqiy bo’lgan 10 ta elementli A massivi berilgan. Juft indeksli elementlardan alohida, toq indeksli elementlardan alohida massiv hosil qiling.
# include
# include
using namespace std;
int main ( )
{ float a[10], b[5], c[5];
for (int i=0; i<10; i++)
{ cout<<" a["<> a[i];
}
cout<<"\n ********************** \n";
cout<<"\n";
for (int i=0; i<5; i++)
{ c[i] = a[2*i +1];
b[i] = a[2*i];
cout << " b["<<2*i<<"]=\a" << b[i];
cout << " \t c["<<2*i+1<< "]=\a" << c[i] <
getch ( );
}
4-misol. A va V massivlari berilgan. Yangi S massivini quyidagicha hosil qiling: A massivining elementlari yangi massivning juft indekslari,
V massivining elementlari esa yangi massivning toq indekslarini tashkil etsin.
# include
# include
using namespace std;
int main ( )
{ float a[5], b[5], c[10]; int i;
for ( i=0; i<5; i++)
{ cout<<" a["<> a[i];
cout<<" b["<> b[i]; }
for ( i=0; i< 5; i++)
{ c[2*i+1] = b[i];
c[2*i] = a[i]; }
cout<<"\n";
for (i = 0; i < 10; i++)
cout << " c[" << i << "]= " << c[i] <
getch ( );
}
C++ algoritmik tilida faqat bir o’lchovli massivlar bilan emas, balki ko’p o’lchovli massivlar bilan ham ishlash mumkin. Agar massiv o’z navbatida yana massivdan iborat bulsa, demak 2 o’lchovli massiv, ya`ni matritsa deyiladi. Massivlarning o’lchovi kompyuterda ishlashga to’sqinlik qilmaydi, chunki ular xotirada chiziqli ketma-ket elementlar sifatida saqlanadi. Ko’p o’lchovli massivlarni xuddi 1 o’lchovli massivga o’xshab e`lon qilinadi, faqat indeks toifasi sifatida massivning satrlari (qatorlari) va ustunlari toifasi ko’rsatiladi va ular aloxida [ ][ ] kavslarda kursatiladi.
Masalan: A nomli butun sonlardan iborat 2 o’lchovli massiv berilgan bo’lsa va satrlar soni n ta, ustunlar soni m ta bo’lsa: int a[n][m] Ikki ulchovli massiv elementlarini kiritish-chikarish, ular ustida amallar bajarish ichma-ich joylashgan parametrli sikllar ichida bo’ladi, ya`ni 1-sikl satrlar uchun, 2-tsikl ustunlar uchun. Masalan:
|
| |