Simplify

Nice easy one for you guys I'm sure.. My brain has just had enough. how do I simplify this so that the loop will exit upon entry -1?

1
2
3
4
5
6
7
8
	while(donations[index] != -1)
	{
		cout << "Enter Donation Amount: ";
		cin >> donations[index];
		cout << donations[index] << endl; // error checking
		if(donations[index] != -1)
			index++;
	}.
Change the loop to a do while and increment index with the postfix increment operator like this:

1
2
3
4
5
6
7
8
do
	{
	    std::cout << index << "\n";

		std::cout << "Enter Donation Amount: ";
		std::cin >> donations[index];
		std::cout << donations[index] << std::endl; // error checking
	}while(donations[index++] != -1);
ah yes thanks! there is a second part that requires pointer arithmetic to calculate the mean and median, I've managed to do this but I'm unsure whether this is pointer arithmetic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float MeanCalc(int* ptrarr[], int index)
{
	int SumTotal;
	float Mean;

	for(int j = 0; j < index; j++)
	{
		SumTotal += *ptrarr[j];
	}

	Mean = SumTotal / index;

	return Mean;
}


Is this correct?
That asterisk on Line 8 is a dereference operator so this isn't pointer arithmetic, this is arithmetic that happens to involve a pointer which when you get down to it is just straight math. You're doing just fine from what I can see. There isn't really a scenario where 'j' would need to be a signed integer, but that is really "grasping at straws"* and barely worth mentioning.

*: That was really only written a snarkey\passive-aggressive jab at someone here who is apparently unfamiliar with the term.
ah ok i thought as much,
haha who was the jab at?
Considering how juvenile it is I'm going to leave that one unanswered for now.
haha fair enough, last question:
Without using a linked list, or defining a large sized array, write a simple program which prompts the user to
enter in a set of positive numbers. There can be an arbitrary number of numbers in the set.
You can assume the numbers are of type integer. A user can stop entering numbers by using a sentinel such
as -1.

how would I go about doing that? I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
	int index = 0;
	int input[index];

	while(input[index] != -1) //input will continue till -1 is entered
	{
		cout << "Enter Integer Value: ";
		cin >> input[index];
		cout << input[index] << endl;
		if(input[index] != -1)
		{
			index++;
		}
	}


but I've been informed that this is a bad idea... thoughts?
ah I thought someone would say vectors :) thanks but I'm not allowed to use them just yet :( but from what I understand so far they would solve all my problems :P
I thought that might be the case, but I was hoping this would be easy. :)
I think they want you to use dynamic allocation. That is, using the 'new' operator to dynamically allocate new memory as you need it. Line 2 in your current code is not valid, you cannot create an array with a variable, and even if it could it would have a size of zero in this case.

Edit: Here's an article on how to do this.
http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html
Last edited on
ah yeh this is what ive been playing around with for an hour now.

thanks ill get that working in no time at all :)
Topic archived. No new replies allowed.