Advertisement
BojidarDosev

array basics

Jan 9th, 2024
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void reverseArray(int arr[], int size) {
  5.     int start = 0;
  6.     int end = size - 1;
  7.  
  8.     while (start < end) {
  9.         // Swap elements at start and end indices
  10.         int temp = arr[start];
  11.         arr[start] = arr[end];
  12.         arr[end] = temp;
  13.  
  14.         // Move towards the center of the array
  15.         start++;
  16.         end--;
  17.     }
  18. }
  19.  
  20. int main()
  21. {
  22.     int numbers[5] = { 2,5,8,10,15 };
  23.     int sum = 0;
  24.     int max = 0;
  25.     int input;
  26.     int flag;
  27.  
  28.     //array size
  29.     int arr = sizeof(numbers) / sizeof(int);
  30.     //cout << arr;
  31.  
  32.     for (int i = 0; i < arr; i++)
  33.     {
  34.         sum = numbers[i] + sum;
  35.     }
  36.     cout << "Sum of the array is: " <<sum <<"\n";
  37.  
  38.     cout << "Avg of the array is: " << sum/arr << "\n";
  39.     for (int i = 0; i < arr; i++)
  40.     {
  41.         if (numbers[i] > max)
  42.         {
  43.             max = numbers[i];
  44.         }
  45.     }
  46.  
  47.     cout << "The max element of the array is: " << max << "\n";
  48.    
  49.     cout << "Your number is: " << "\n";
  50.     cin >> input;
  51.     for (int i = 0; i <arr; i++)
  52.     {
  53.         if (input == numbers[i])
  54.         {
  55.             flag = 1;
  56.             break;
  57.         }
  58.         else flag = 0;
  59.     }
  60.     if (flag == 1)
  61.     {
  62.         cout << "Your number is in the array!" << "\n";
  63.     }
  64.     else cout << "Your number is not in the array!" << "\n";
  65.  
  66.     //smqna na 3ti element v masiva i otpechatwane
  67.     for (int i = 0; i <arr; i++)
  68.     {
  69.         if (i == 2)
  70.         {
  71.             numbers[2] = 20;
  72.         }
  73.         cout << numbers[i] << " ";
  74.         cout << "\n";
  75.     }
  76.     reverseArray(numbers, arr);
  77.  
  78.     // Print the reversed array
  79.     cout << "Reversed Array: ";
  80.     for (int i = 0; i < arr; i++) {
  81.         cout << numbers[i] << " ";
  82.     }
  83.     cout << endl;
  84.  
  85.    
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement