Need help on this homework array question

closed account (EAp4z8AR)
Prompts the user to enter floating point numbers that represent daily sale amounts at a store o Allows the user to indicate the end of user inputs with -1
o Validates the user input for numbers greater than 0
o Stores the numbers in an array
• Determines and displays the maximum, minimum, and arithmetic mean (average) of the sale amounts

-This is what my professor wants me to do, but I don't understand how to get unlimited elements in arrays. He also has not taught us how to use vectors and told us not to use vectors. I also do not understand how to use the -1 to stop accepting values.


int main()
{
int sum = 0, average;
float numbers[100];
int numEnter = 0, max = 0, min = 10;

while (numEnter < 100)
{
cout << "Please enter the sale amount: ";
int num;
cin >> num;
while (num < -1)
{
cout << "Invalid amount, try again.\n";
cout << "Please enter the sale amount: ";
cin >> num;
}
numbers[ numEnter++ ] = num;
}
}
closed account (EAp4z8AR)
I also need to caluclate the minimum without the -1
Roughly, an approach could look like this:
Dynamically allocate an array of size C. We'll call C the "capacity" of the array.
Keep track of an integer named S, which stands for the "size" of the array. Let S contain the value 0.

Each time a value needs to be added to your array, write it at index S, and increment S.
If and only if S is equal to C, allocate a new array with size C * 2. Then, copy the elements from the old array to the new array, and deallocate the old array.

This is the basic idea behind dynamic containers like std::vector.
closed account (EAp4z8AR)
hmm.. do you have an example. It doesn't have to fit the requirements
Hello vmansuria,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

I do not know if you are stuck using "float", but the preferred floating point type these days is a "double".

You have a few problems in your code that I have noted with comments.

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
// <--- needs header files.

int main()
{
        constexpr size_t MAXSIZE{ 100 };  // <---Added
	double sum = 0.0, average{};  // <--- Changed.
	double numbers[MAXSIZE]{};  // <--- Changed. Type and array size
	int numEnter = 0;
	double max = 0.0, min = 10.0;  // <--- Changed.

	while (numEnter < 100)
	{
		cout << "Please enter the sale amount: ";
		double num;
		cin >> num;

		// <--- Need if statement to check for -1. If true break out of while loop.

		while (num < 1)  // <--- Changed. A valid entry is anything greater than zero.
		{
			cout << "Invalid amount, try again.\n";
			cout << "Please enter the sale amount: ";
			cin >> num;
		}

		numbers[numEnter++] = num;
	}
}


Once you have your array filled the variable "numEnter" will let you know how much of the array is used.

If you have learned about functions they would be useful for the min, max and mean if not you can just add to your code after line 25.

Other than using a vector there is no unlimited array. If you fill your fixed array then you will have to create a dynamic array larger and copy the old array to the new array. I have not used a dynamic array in this context, so someone else may have more insight.

You have a good start with a few changes and additions.

Hope that helps,

Andy
Topic archived. No new replies allowed.