Arrays

Hi there,

I'm really new with programming, I haven't done any before this class I am taking now, so I'm really struggling.
I know it's really simple, but I'm having a lot of trouble figuring out how to do my next assignment. I've tried writing it down in pseudocode, but I just don't think I understand so it's not helping me. This is my assignment:

"Write and test a C++ program to print a set of real numbers in descending order. The program should also print out the median of the numbers input and how many numbers were input. The program should read in numbers until a negative number is read. The negative number serves as a sentinel or marker telling the program when to stop reading numbers. The program should then sort the numbers and print them out in order from largest to smallest. Note, the sentinel number is not to be considered as one of the numbers, it should not be printed."

Right now I am just trying to figure out how to do the part that talks about putting the numbers into an array and quitting when a negative is entered, without including the negative in the array. It's really confusing me, nothing I have tried is working.
I would like to concentrate on this part by itself right now, if possible I'd like to just forget about the sorting right now.

**I know there is a LOT wrong with this code, you don't need to tell me!** I just wanted to put it there so it's known I'm actually trying to do this and not just trying to get people to do my homework for me. I know it's embarrassing and horrible so please don't say much about it. Haha

But if someone could talk me through putting numbers in an array and stopping with a negative, without putting the negative in the array, that would be absolutely fantastic.

Thank you!!

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
  #include <iostream>;
using namespace std;

int main()
{
	int i, array[10];
	
	for (i=0; i<=9; i++)
	{
		cout << "\nEnter a number: ";
		cin >> i;
		
		if (i>=0)
		{
			i=array[i];
		}
		else
		{
			cout << "\nNumbers are " << array[i];
			//i want it to print all numbers except negative
                        //butnopenopenope
                        //killmenow
		}
	}
return 0;	
}
Don't use i to store a value just create another variable, and to stop loop after negative number is inputed

If array at i is less than zero then break the loop
You just need to think about what you need to do, and make a list of your conditions:

There are 2 cases where you need to stop reading in numbers. 1. when the user enters a negative number, and 2. When you have run out of space in your array to store more numbers (in your case, 10)

Next, you need to keep track of how many numbers a user has input. You need this for a few things. You need to know that you haven't exceeded the size of your array, and you also need it to print how many numbers were input.

So you need to think about how to now read in and populate your array. One way to do this would be using a while loop. You want to keep reading in values until something tells you to stop.


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
//initial code goes here...

int stillreading = 1; //You are reading numbers initially until something tells you to stop.
int arraysize = 0; //this is only a count of the number of elements in your array.
                   //you previously defined your array as having a max size of 10 elements.

while(stillreading) {
  cout << "\nEnter a number: ";
  cin >> i; //read a number into a temporary variable (i in your case)

  //if the number is >= 0, store it in your array and increment your array size counter
  if(i >= 0) {
    //store the value in your array
    array[arraysize] = i; //need to use arraysize for the index here.

    //increment a count variable to keep track of the size of your array.
    arraysize++; //move to the next position in your array
  }
  //if the number < 0 OR your array size counter >= 10, set 'stillreading' to 0 to exit the input phase
  //I'll leave this part up to you. You can either do it as a single if statement, or as two separate if
  //statements to check either case.  The only contents of those should be stillreading=0; That will
  //tell the while loop to exit since while(stillreading) will be false.
}

//At this point, you should have an array populated with non-negative values
//You also know the size of the array, so you can use a for loop to output the contents 

Thank you SO much, this is extremely helpful!! I really appreciate it!
Topic archived. No new replies allowed.