Shaymatov Dilmurod 840-20
17-varyant
Berilgan ikki oʻlchovli butun sonli massiv [0..m-1, 0..n-1], uning har bir satri va ustuni oʻsish tartibida joylashgan (misolga qarang). Berilgan 2 o‘lchovli massivda ko‘rsatilgan sonning satr, ustun o‘rnini topish dasturini yozing.
#include
using namespace std;
int main()
{
int arr[4][5]={{2,3,5,6,6}, {2,4,5,7,8}, {5,5,6,8,8}, {5,7,7,8,9} };
cout<<"Son kiriting: ";
int n;
cin>>n;
for(int i=0;i<4;i++){
for(int j=0; j<5; j++){
if(arr[i][j]==n){
cout<<"Satr indeksi: "<cout<<"Ustun indeksi: "<}
}
}
}
2.Binar qidirish usulidan foydalanib quyidagi massiv elementlari orasidan ‘42’ massiv elementining indeks nomerini toppish dasturini tuzing: [14,25,29,31,34,38,42,58,64]
#include
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main(void)
{
int arr[9]={14,25,29,31,34,38,42,58,64};
int x;
cout<<"Searching number: ";
cin>>x;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index= " << result;
return 0;
}
3.Stekda 20 ta element mavjud. Elementlarni bitta-bitta tanlovchi dasturni tuzing.
#include
using namespace std;
void PrintStack(stack s)
{
if (s.empty())
return;
int x = s.top();
s.pop();
PrintStack(s);
cout << x << " ";
s.push(x);
}
int main()
{
stack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
s.push(8);
s.push(9);
s.push(10);
s.push(11);
s.push(12);
s.push(13);
s.push(14);
s.push(15);
s.push(16);
s.push(17);
s.push(18);
s.push(19);
s.push(20);
PrintStack(s);
return 0;
}
//Stack
#include
#include
#include
#define MAX 20
int top=-1,stack[MAX];
void push();
void pop();
void display();
int main()
{
int ch;
while(1)
{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
Do'stlaringiz bilan baham: |