473,569 Members | 2,428 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 26138
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 345678435673456 78 causing an attempt at accessing array[345678435673456 78] (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

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

Similar topics

0
1554
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
6128
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 microprocessor to do a few instructions more at each array access will not make any difference what speed is concerned. Not all C applications are...
12
8026
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 time, any suggestions? I'm googling for dynamic programming solutions, but not coming up with much.
4
2049
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 6 2 7 4 which I post to next page as col1array col2array col3array problem is some
2
7039
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, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created...
1
1855
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 to return the values. Any help would be appreciated I just don't think I understand what is being asked of me. The problem is as follows A...
1
1843
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 is the looping is not displaying the all contents of the arrays. Do you have any idea what the problem is and how to fix the problem?
5
7413
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 visibility and/or presence of the Listbox and associated label, and I want to encapsulate some of associated code. I want to be able to pass the...
12
4325
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 from which to start filling. I need to use recursion here. for example, to be clear: input: (ignore the line, think of that as blank space)...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.