Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. // This program displays all the square numbers up to a user input
  8.  
  9. // Create an array with square numbers up to the limit entered sequentially
  10. vector<int> squares(int arraySize) {
  11. vector<int> squareList;
  12. for (int i = 1; i < arraySize; i++) {
  13. squareList.push_back(i * i);
  14. }
  15. return squareList;
  16. }
  17.  
  18. int main() {
  19. cout << "Lists square numbers up to chosen limit" << endl << endl; // Two endl to leave a space before listing the numbers
  20.  
  21. cout << "Please type in a number to use as a limit, then press 'Enter'" << endl;
  22.  
  23. int limit;
  24. cin >> limit;
  25. cout << endl; // Visually split input number from output list
  26. int arraySize = int(sqrt(limit)) + 1; // We will need to know the size to make the array in a moment
  27.  
  28. vector<int> squareList = squares(arraySize);
  29. for (int i : squareList) {
  30. cout << squareList[i] << endl;
  31. }
  32.  
  33. cout << endl << "Press 'Enter' to exit program" << endl;
  34. cin.ignore(100, '\n'); // ignore any characters in the input buffer until we find an enter character
  35. cin.get(); // get one more char from the user
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement