By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Filling in arrays with input

P: 13
If I wish to have an array filled with a set number of user inputed numbers how would i go about this?
Also I could possibly then seek out how many times a certain number was input into the array?

I have a slight idea but is it possible to have
cin >> n
array[n] and get that to work......
or would it require inputting 10 seperate variables?
thank you!
Mar 14 '07 #1
Share this Question
Share on Google+
10 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
If I wish to have an array filled with a set number of user inputed numbers how would i go about this?
Also I could possibly then seek out how many times a certain number was input into the array?

I have a slight idea but is it possible to have
cin >> n
array[n] and get that to work......
or would it require inputting 10 seperate variables?
thank you!
Depends on how many variables you want the user to put in.

I would either do a while statement and have a sentinel value (such as -1 or 0) mean exit, or have a set number in the array. You also don't want to do:
Expand|Select|Wrap|Line Numbers
  1. int n;
  2. int array[];
  3. cin >> n;
  4. array[n];
  5.  
because the user could put in random numbers such as 3, 7, and 34567843567345678 causing an attempt at accessing array[34567843567345678] (which your compiler would most likely complain about). I would recommend having an index initialized to 0 that is incremented each time the loop occurs.
Mar 14 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
If you are talking about declaring an array with a user-defined amount of elements, then you will have to use pointers. You cannot declare an array with 10 elements and then ask the user for however many values they want. Wile your compiler will give you no problems when you start writng to array[10], array[11], ..., array[2175638675], you are actually overwriting memory, which could seriously mess up your computer. You must be sure you are only allowing data to be entered while there is room in the array. A while loop won't do this well - you should use a for loop, as follows:

Expand|Select|Wrap|Line Numbers
  1. int array[10]; // or your number here
  2. for (int i = 0; i < 10; i++) { // must be the same number again!
  3.    cin >> array[i];
  4. }
This will only work if you, the programmer, know how many values will be entered. If you want the user to decide, you should use the following segment:

Expand|Select|Wrap|Line Numbers
  1. int *array;
  2. cout << "How many values: ";
  3. int n;
  4. cin >> n;
  5. array = new int[n];
  6. for (int i = 0; i < n; i++) {
  7.    cin >> array[i];
  8. }
Mar 14 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Ganon11 is right. I was trying to show that you can't cin n and then use it as an index. If you need to read in an undefined number of entries, and save them, you probably want a list or some sort of container that doesn't need to have its size explicitly declared on instantiation.
Mar 14 '07 #4

P: 13
i kno there are to be exactly 10 values........i think i can take that part from here. How then can I check through each entered value to check if its a certain number?
example: the user eneter 5,10,15,7,3 in the array
i want to check how many times 5 shows up....
i would do that using a for statement as well?
Mar 14 '07 #5

DeMan
100+
P: 1,806
That would certainly be one way...(and probably the easiest if there is no limit on the value entered)
Mar 14 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
Yes. Using a for loop, you can access each element of the array and see if it is 5 - or any other number you need.

Do you know the biggest number the user can enter? If so, another solution arises. You can define an array with number of elements equal to the biggest number. Then, looping through the array, increment the element of the second array, using the value of the first array as the index. At the end, the second array will contain integer values representing the number of occurances of its index in the original array.
Mar 14 '07 #7

P: 13
what si wrong with this code??? this is a seperate function to check if any number in the array matches the user's inputed favorite number

[code]int favorite (int n)
{
for (int i = 0; i < 10; i++) {
int x= int num[i];
if ( x==n) {
int count=count++;
}
}

return 0;
}[code]
Mar 14 '07 #8

DeMan
100+
P: 1,806
Expand|Select|Wrap|Line Numbers
  1. int favorite (int n, int[] num)
  2. {
  3.   int x = -1;
  4.   int count = 0;
  5.   for (int i = 0; i < 10; i++) {
  6.     x= num[i];
  7.     if ( x==n) {
  8.      count=count++;
  9.   }
  10. return count;
  11. }
  12.  
also, for future use, you probably should pass in the length of num and use it in the loop, so that you can extend on this problem later if you have to....
Mar 14 '07 #9

P: 13
final question.......
this is in main
Expand|Select|Wrap|Line Numbers
  1. cout << "You Entered Your Favorite Number " <<  ????<< " Times" <<endl;
what goes in place of the question marks to call the count? i originally had just count but it doesnt work. i know i should prob call the function favorite, or initialize count to be that but im not sure of the way to do that
Mar 14 '07 #10

Ganon11
Expert 2.5K+
P: 3,652
Just include the call to favorite a.k.a.

Expand|Select|Wrap|Line Numbers
  1. cout << "You entered your favorite number " << favorite(favNum, intArr) << " times." << endl;
Mar 15 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.