dynamic array that stores a list of integers

I have to write a program that asks a user to enter the size of a dynamic array that stores a list of integers then create the dynamic array and a loop that allows the user to enter an integer value for each array element. Loop through the array, find the largest value in the array and output it, as well as its position in the array then I have to delete the memory allocated to my dynamic array before exiting my program.



I'll have to use a For-loop and inside of it a while loop , if im certain right?
I'll have to add "delete [] arr;" to delete the array right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main() {
	int x, n;
	cout << "Enter the number of items:" << "\n";
	cin >>n;
	int *arr = new int(n);
	cout << "Enter " << n << " items" << endl;
	for (x = 0; x < n; x++) {
		cin >> arr[x];
	}
	cout << "You entered: ";
	for (x = 0; x < n; x++) {
		cout << arr[x] << " ";
	}
	return 0;
}


can anyone help me piece it together? the above code isn't the code itself , its just a templet im gonna use as an "example" - is above correct?
> new int(n);
This allocates space for one integer, and initialises it to n.

Compare with
new int[n];
which allocates space for n integers.

Other than that, it seems to meet your requirements.

> I'll have to add "delete [] arr;" to delete the array right?
Yes.
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main() {
	size_t n {};

	std::cout << "Enter the number of items: ";
	std::cin >> n;

	const auto arr {new int[n]};

	size_t pos {};

	std::cout << "Enter " << n << " items: ";
	for (size_t x = 0; x < n; ++x) {
		std::cin >> arr[x];

		if (arr[x] > arr[pos])
			pos = x;
	}

	std::cout << "The largest value is " << arr[pos] << " at position " << pos << '\n';

	delete[] arr;
}

Hello Anonomys,

Your instructions feel to be a bit lacking in information:

I have to write a program that asks a user to enter the size of a dynamic array
that stores a list of integers.

1. Then create the dynamic array and a loop that allows the user to enter an integer
   value for each array element.

2. Loop through the array, find the largest value in the array and output it,
   as well as its position in the array.

3. Then I have to delete the memory allocated to my dynamic array before exiting my
   program.

I see no indication here that a while loop is needed. for loops should be all that
is needed.


This is the way I see it.

It is always best to include the complete instructions that you were given, so everyone will know what to do.

In addition to what salem c has pointed out. You need a prompt in the first for loop to let the user what to do.

So far you code is a good start. You have covered part 1, with noted changes and part 3 has be covered, you just need to add it.

Part 2 is missing.

Consider this as an ides/suggestion:
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
#include<iostream>

using namespace std;

int main()
{
    int arraySize{};  // <--- ALWAYS initialize all your variables.
    int pos{};

    cout << "\nEnter the number of items: ";
    cin >> arraySize;

    int *arr = new int[arraySize] {};

    // <--- Enter the numbers.
    cout << "\nEnter " << arraySize << " items\n";

    for (int idx = 0; idx < arraySize; idx++)
    {
        std::cout << " Enter #" << idx + 1 << ": ";
        cin >> arr[idx];
    }

    // <--- Print the array.
    cout << "\nYou entered:\n";

    for (int idx = 0; idx < arraySize; idx++)
    {
        cout << arr[idx] << " ";
    }

    // <--- Find the largest number. TODO

    std::cout << "\n\nThe largest number: " << arr[pos] << " is at position: " << pos << '\n';

    delete[] arr;

    return 0;  // <--- Not required, but makes a good break point for testing.
}

A good variable name and some blank lines make the code much easier to read. The use of the {}s is you choice. I personally find the above use of {}s much easier to read and follow. It also helps when you miss a closing }.

Right now you have enough to finish the last for loop.

Andy
Topic archived. No new replies allowed.