Advertisement
Porr011

Lesson 11 activity 1

Mar 10th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. // function prototype
  7. void printData(int [], int);
  8.  
  9. int main ()
  10. {
  11.     // size for array
  12.     const int ARRAY_SIZE = 5;
  13.     srand(time(0)); // seed random number
  14.    
  15.     int Numbers [ARRAY_SIZE] = {-10,-6,0,3,10};
  16.     //generate random index number
  17.     for (int i=0; i < ARRAY_SIZE; i++)
  18.     {
  19.         int index = rand() % ARRAY_SIZE;
  20.        
  21.         //using temporary varibles as random algorithm to switch it around  
  22.         int temp = Numbers[i];
  23.         Numbers[i] = Numbers[index];
  24.         Numbers[index] = temp;
  25.     }
  26.     // function
  27.     printData(Numbers, ARRAY_SIZE);
  28.    
  29.     return 0;
  30. }
  31. // function end
  32.  
  33. //***************
  34. //* function for printing the data
  35. //*
  36. //***************
  37. void printData(int Numbers[], int size)
  38. {
  39.     // loop through array and print values
  40.     for (int i=0; i < 1; i++)
  41.     {
  42.         cout << "Element 1 of the array is: " << Numbers[0] << endl;
  43.         cout << "Element 2 of the array is: " << Numbers[1] << endl;
  44.         cout << "Element 3 of the array is: " << Numbers[2] << endl;
  45.         cout << "Element 4 of the array is: " << Numbers[3] << endl;
  46.         cout << "Element 5 of the array is: " << Numbers[4] << endl;
  47.     }
  48. }
  49. // function end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement