arrayone.cpp 999 B

123456789101112131415161718192021222324252627282930
  1. // arrayone.cpp -- small arrays of integers
  2. #include <iostream>
  3. int main()
  4. {
  5. using namespace std;
  6. int yams[3]; // creates array with three elements
  7. yams[0] = 7; // assign value to first element
  8. yams[1] = 8;
  9. yams[2] = 6;
  10. int yamcosts[3] = {20, 30, 5}; // create, initialize array
  11. // NOTE: If your C++ compiler or translator can't initialize
  12. // this array, use static int yamcosts[3] instead of
  13. // int yamcosts[3]
  14. cout << "Total yams = ";
  15. cout << yams[0] + yams[1] + yams[2] << endl;
  16. cout << "The package with " << yams[1] << " yams costs ";
  17. cout << yamcosts[1] << " cents per yam.\n";
  18. int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1];
  19. total = total + yams[2] * yamcosts[2];
  20. cout << "The total yam expense is " << total << " cents.\n";
  21. cout << "\nSize of yams array = " << sizeof yams;
  22. cout << " bytes.\n";
  23. cout << "Size of one element = " << sizeof yams[0];
  24. cout << " bytes.\n";
  25. // cin.get();
  26. return 0;
  27. }