Advertisement
bhok

3.5 Functions Array Passing

Jul 13th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. // More tutorials at BrandonHok.com
  2.  
  3. #include <iostream>
  4.  
  5. // C++ Crackdown 3.5 Passing and Using An Array
  6.  
  7. // An array is a long list of items
  8. // It is literally.... [stuff, stuff, stuff, stuff, pancakes, chocolates]
  9. // Let's check out how to declare one and then pass one into a function
  10.  
  11. // This is the function prototype for an array
  12. // Notice some slight differences here
  13. void arrayPowerGoodness(int arrayname[], int sizeofthearray);
  14.  
  15. int main()
  16. {
  17.     // Set up the array
  18.     // A constant is needed here because when passing, we cannot have the array be manipulatable
  19.     // This part is a bit confusing, you may need to break it down and fully understand what's going on
  20.     const int arraySize = 5;
  21.     int arrayName[arraySize] = { 1,2,3,5,6 } ;
  22.    
  23.     // Call the array propertly after making the function below
  24.     arrayPowerGoodness(arrayOne, arraySize);
  25.     system("pause");
  26. }
  27.  
  28. // This function allows to print every element of the array
  29. // Try it out
  30. void arrayPowerGoodness(int a[], int size)
  31. {
  32.     for (int i = 0; i < size; ++i)
  33.         std::cout << a[i];
  34. }
  35.  
  36. // Can you make your own array function but use a while method instead?
  37. // Can you select specific numbers of the array and add them up?
  38. // Can you modify a specific element of the array?
  39. // What do you think arrays are useful for?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement