Vasilena

PracticeDayTwoExercises1,2

Jul 7th, 2021 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //Ex1
  4. int swap(int* a, int* b)
  5. {
  6.     int temp = *a;
  7.     *a = *b;
  8.     *b = temp;
  9. }
  10.  
  11. //Ex2
  12. int sumArr (int* arr, int n)
  13. {
  14.     int sum = 0;
  15.     for (int i = 0; i < n; i++)
  16.     {
  17.         sum += *(arr + i);
  18.     }
  19.     return sum;
  20. }
  21.  
  22. int main()
  23. {
  24.     int arr[] = {1, 4, 5, -1, 3, 5};
  25.     int s = sumArr(arr, 6);
  26.     int a, b;
  27.     printf("------------------Exercise2------------------\n");
  28.     printf("Enter a: ");
  29.     scanf("%d", &a);
  30.     printf("Enter b: ");
  31.     scanf("%d", &b);
  32.     printf("Before swapping:\n");
  33.     printf("a = %d, b = %d\n", a, b);
  34.     swap(&a, &b);
  35.     printf("After swapping:\n");
  36.     printf("a = %d, b = %d\n", a, b);
  37.     printf("------------------Exercise2------------------\n");
  38.     printf("Sum = %d\n", s);
  39.     return 0;
  40. }
  41.  
Add Comment
Please, Sign In to add comment