Really need help with Arrays

Okay, so I've been at this for hours, editing, deleting, remaking my code and I can get one array easily, but adding the second array is hard and driving me nuts.
I know it's not much, I'm only a beginner.

The problem is:

Write a program that will fill arrays with values read from the user. If the number read is greater than 0, it will be stored in arrayA. If the number read is less than 0, it will stored in arrayB.
Both arrays will be a partially filled arrays and the user will be asked to enter a number only he/she wishes to continue.
However, even if the user wants to enter another number, the program should not read more than 10 numbers.
The program will then print the content of the arrays to screen.

I have this as of right now, which is one array;
include <iostream>

using namespace std; int main() { char choice; int num[10], index = 0;
do

{
cout << "Please enter a number" << endl;
cin >> num[index];
index++;

cout << "Press Y if you'd like to continue" << endl;
cin >> choice;
} while ((choice == 'Y') || (choice == 'y') && (index < 10));


for (int i = 0; i < index; i++)
{
cout << num[i] << " ";

}




system("Pause");
return 0;
}
Since you're not showing any code that has multiple arrays, what exactly is your question?

As a hint, for this assignment you will need two arrays and a "temporary" variable to accept user input. Then you will need to decide which array you need to put that input into, based on if it is either greater than or less than zero.

By the way what happens if the user enters a value of zero?



a little more clarification...
you need 2 arrays, say int positives[10] and int negatives[10]
and 2 indices for these: int pdx = 0; int ndx = 0;
then do a lot like what you did, ask for a number, if it is > 0 then positives[pdx++] = number, else if negative negatives[ndx++] = number. Pdx and ndx will happily tell you how many locations in each were used when printing them back out.


Thanks for the help guys, problem solved.

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

int main()
{
	char choice;
	int numA[10], numB[10], number = 0;
	int num = 0, indexA = 0, indexB = 0;

	do
	{
		cout << "Please enter a number" << endl;
		cin >> number;

		if (number > 0 && indexA < 10)
		{
			numA[indexA] = number;
			indexA++;
		}
		else if (number < 0 && indexB < 10)
		{
			numB[indexB] = number;
			indexB++;
		}

		else if (number == 0)
		{
			cout << "invalid number" << endl;
		}
		cout << "Would you like to enter another number? (Y/N)" << endl;
		cin >> choice;
	} while ((choice == 'y' || choice == 'Y') && (indexA + indexB < 10));

		
	

	cout << "Array A" << endl;
	for (int i = 0; i < indexA; i++)
	{
		cout << numA[i] << " ";
	}

	cout << endl;

	cout << "Array B" << endl;
	for (int j = 0; j < indexB; j++)
	{
		cout << numB[j] << " ";
	}

system("Pause");
return 0;
}
Topic archived. No new replies allowed.