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!
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: -
int n;
-
int array[];
-
cin >> n;
-
array[n];
-
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.
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: - int array[10]; // or your number here
-
for (int i = 0; i < 10; i++) { // must be the same number again!
-
cin >> array[i];
-
}
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: - int *array;
-
cout << "How many values: ";
-
int n;
-
cin >> n;
-
array = new int[n];
-
for (int i = 0; i < n; i++) {
-
cin >> array[i];
-
}
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.
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?
DeMan 1,806
Top Contributor
That would certainly be one way...(and probably the easiest if there is no limit on the value entered)
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.
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]
DeMan 1,806
Top Contributor -
int favorite (int n, int[] num)
-
{
-
int x = -1;
-
int count = 0;
-
for (int i = 0; i < 10; i++) {
-
x= num[i];
-
if ( x==n) {
-
count=count++;
-
}
-
return count;
-
}
-
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....
final question.......
this is in main - 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
Ganon11 3,652
Recognized Expert Specialist
Just include the call to favorite a.k.a. - cout << "You entered your favorite number " << favorite(favNum, intArr) << " times." << endl;
Sign in to post your reply or Sign up for a free account.
Similar topics |
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) {
|
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...
|
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...
|
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 ...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |