| closed account (4y79216C) | |
|
hello everyone can someone help me about array plsss i really need help right now using one-dimensional array. create a program that will count the number of the same intries in a series number based on the user specific no. to count EXPECTED output Enter no.1:2 Enter no.2:1 Enter no.3:1 Enter no4:2 what number to v=count?1 total number 1 is 2 should use a counter i appretiate those who will answer this is my code #include<iostream> using namespace std; int main() { int n,hn,nc; int i; int count; float totaln; count=0; cout<<"ENTER MANY NUMBERS TO INPUT?:"<<endl; cin>>hn; for(i=1;i<=hn;i++) { cout<<"ENTER NUMBER :"<<i<<":"; cin>>n;} cout<<"WHAT NUMBER TO COUNT:"<<endl; cin>>nc ; count=0; totaln+=n; cout<<"THE TOTAL NUMBER IS:"<<totaln; system("PAUSE"); } | |
|
|
|
| Kazekan (83) | |||
|
First off, please put your code in code <> brackets. The button for this can be found under the Format box to the right of the text window when creating/editing a thread. Secondly, you haven't even attempted to implement your array. :( As it stands, your user will enter in "n", "hn" times in a row, but "n" will just be overwritten each time. It is stored nowhere. Also, variables such as "i" that are to be used within a for loop can be completely defined within the for loop itself, unless you need to reference the last value of "i" later on or something. Therefore, your for loop becomes:
Within your first for loop you should be using "i" to also place the values within your array (which you haven't declared yet). Technically you'll be using i-1, since you'll want to start at 0, and not 1, since someArray[0] will coincide with the first spot in the array. | |||
|
|
|||
| sheraz07 (1) | |
|
susied this is not codding for arrays u should declare first array.......like A[10];..........etc then u use for loop for it learn tutorial of arrays...... int a[5]; int count=0; int sum=0; for(int i=1;i<=5;i++) { cout<<"enter number"; cin>>a[i]; sum=sum+a[i]; count++; } cout<<count<<endl; cout<<"array sum="<<a[i]; | |
|
Last edited on
|
|
| closed account (4y79216C) | |
|
yayks,,, like this #include<iostream> using namespace std; int main() { string name[50]={}; int s[]={}; int i; int hn; int l; int k; int n; int largest; int smallest; float average; cout<<"ENTER MANY many students?:"<<endl; cin>>hn; for(i=1;i<hn;i++) { cout<<"ENTER name of students :"<<i<<":" ; cin>>n;{ cout<<"ENTER SCORE:"; cin>>l;} for(i=0;i<hn;i++){ if(s[i]>largest) largest=s[i]; for(i=0;i<hn-1;i++){ if(s[i]<smallest) smallest=s[i];} }} cout<<"THE HIGHEST IS"<<s[i]; system("PAUSE"); } i don't know how to calculate | |
|
|
|
| DEBASISH DAS (1) | |
| string name[50]={}; | |
|
|
|
| EssGeEich (1007) | |
| Code tags please. Also arrays are fixed-size. use std::vector instead. | |
|
|
|
| closed account (4y79216C) | |
here's my new code right now#include<iostream> using namespace std; int main() { int a[]={}; int count=0; int sum=0; int i,hn,w; cout<<"ENTER MANY NUMBERS TO INPUT?:"<<endl; cin>>hn; for(i=1;i<hn;i++) cout<<"ENTER NUMBER :#"<<i-1; cin>>a[i]; cout<<"WHAT NUMBER TO CUONT:"<<endl; cin>>w; { sum=sum+a[i]; count++; } cout<<count<<endl; cout<<"TOTAL:"<<a[i]; system("PAUSE"); } but i think closer | |
|
|
|
| EssGeEich (1007) | |||
Follow, Please. http://www.cplusplus.com/articles/z13hAqkS/ http://www.cplusplus.com/reference/vector/vector/ | |||
|
|
|||
| closed account (4y79216C) | |||
| |||
|
|
|||
| modoran (1245) | |
|
That program does not need any array or std::vector, it works without it. How, it is left as an exercise for the OP. | |
|
|
|
| closed account (4y79216C) | |
| but its say using one dimensional | |
|
|
|
| EssGeEich (1007) | |||
modoran, read it again and try to translate the phrase "Enter many numbers to input?". There is a way not-to-use std::vector I know, but I won't say which one because...
| |||
|
Last edited on
|
|||
| modoran (1245) | |||
I was thinking of something like this:
| |||
|
|
|||
| closed account (4y79216C) | |||
ok thanks for the codes.. i change in the last part
but it doesn't work :( when i enter Enter many numbers to input ? :2 Enter #1:2 Enter #:2 3 WHAT NUMBER TO CUONT: 5 total number 5 is 6 instead of like this total number 5 is 0 beacuse i 5 has not belong to what i enter | |||
|
Last edited on
|
|||
| modoran (1245) | |||
Sorry, I misunderstood your question. If you want to count a occurence of a number, the code could be this:
| |||
|
|
|||
| closed account (4y79216C) | |
|
ahhh .. sorry for disturb but its always answer 0. Im trying to understand this but i dont how to be like this(correct answer) how many want you to enter?: 4 enter #1: 2 enter #2: 2 enter #3: 2 enter #4: 2 What number to be count?: 2 the total number of 2 is "4" hmmmm | |
|
Last edited on
|
|
| EssGeEich (1007) | |
| It seems all right to my eyes. Copy-pasted the code and did what you wrote, worked a charm. | |
|
|
|
| closed account (4y79216C) | |
|
near to finish but my problem is the number i will enter will be add instead of searching how many numbers?? | |
|
|
|
| Imadatobanisa (647) | |
| Be more detailed - and specific. | |
|
|
|
| closed account (4y79216C) | |
| Thanks for everything and for codes it helps me a lot thank for all your concerned | |
|
|
|