-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_array.cpp
53 lines (40 loc) · 1.21 KB
/
01_array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// // Write a program that dynamically allocates an array of integers. Allow the user to input the size, populate the array, and then display the sum of all elements. Finally, use delete to free the allocated memory.
// // Header files
#include <iostream>
#include <stdlib.h>
// // use namespace
using namespace std;
#define MAX 20
// // Main Function Start
int main()
{
int size;
cout << "\nHow Many Numbers You Want to Enter (MAX " << MAX << ") => ";
cin >> size;
// // invalid size
if (size < 1 || size > MAX)
{
cout << "\n!!! Invalid Input..." << endl;
exit(0);
}
int *nums = new int[size]; // // allocate an array of integers dynamically
int sum = 0;
// // Input Numbers
cout << "\nEnter 10 Numbers => ";
for (int i = 0; i < size; i++)
{
cin >> nums[i];
sum += nums[i];
}
// // Print Numbers
cout << "\n>>>>>>>>>>>>>> Numbers <<<<<<<<<<<<<<<<" << endl;
for (int i = 0; i < size; i++)
cout << nums[i] << " ";
cout << "\n\nSum of Numbers => " << sum;
// // deallocate dynamically allocated memory
delete[] nums;
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End