473,508 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filling in arrays with input

13 New Member
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
10 26121
sicarie
4,677 Recognized Expert Moderator Specialist
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
3,652 Recognized Expert Specialist
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
4,677 Recognized Expert Moderator Specialist
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
hey77
13 New Member
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
1,806 Top Contributor
That would certainly be one way...(and probably the easiest if there is no limit on the value entered)
Mar 14 '07 #6
Ganon11
3,652 Recognized Expert Specialist
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
hey77
13 New Member
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
1,806 Top Contributor
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
hey77
13 New Member
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
3,652 Recognized Expert Specialist
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

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

Similar topics

0
1546
by: kf4pfw | last post by:
Hi: I have a form with 4 arrays in it. Each array can contain up to 11 elements. I am using the following code (this is for proof of concept): <? if($submit) {
50
6115
by: jacob navia | last post by:
As everybody knows, the C language lacks a way of specifying bounds checked arrays. This situation is intolerable for people that know that errors are easy to do, and putting today's powerful...
12
8012
by: pjhyett | last post by:
standard 2d array filling with increasing numbers for rows and columns: for(int i=0;i<n;i++) for(int j=0;j<n;j++) a = i + j; problem is it's O(n^2). I'm looking for a method to decrease the...
4
2046
by: mantrid | last post by:
Im using arrays generated from my records displayed in a table on my site to update the corresponding records in a mysql database ie on the web page col1 col2 col3 1 2 2 1 ...
2
7035
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
1
1848
by: BlackJackal | last post by:
Alright here is the problem I have for homework. I understand most of it but I am not exactly sure what the problem is asking me to do or how to search the seperate arrays using the account number...
1
1837
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
5
7404
by: markr1000 | last post by:
I must have looked searched in 500+ places that showed up in Google searchs, but not one has an example of what I want to do. I have a Listbox on a User Control because I want to control the...
12
4321
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.