911-21 guruh talabasi Omonbayev Jaloliddin
Mavzu : Qidiruv algoritmlarining qiyosiy tahlili
Massivni toq va juft elementlari yig’indisi .
#include
using namespace std;
void EvenOddSum(int arr[], int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
cout << "Toq elementlari yig'indisi " << even;
cout << "\nJuft elementlari yig'indisi " << odd;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
EvenOddSum(arr, n);
return 0;
}
2.
#include
int search(int arr[], int N, int x)
{
int i;
for (i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, N, x);
(result == -1)
? printf("Element massivda mavjud emas")
: printf("Element %d indeksida mavjud", result);
return 0;
}
Do'stlaringiz bilan baham: |