The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. Any ideas? Something to do with the array again?
Thanks for all your help.
The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. Any ideas? Something to do with the array again?
Thanks for all your help.
Well, I hope you read my last post. Post the updated code
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
I am pretty sure that I have got the code right and made the amendments that you suggested.
Code:#include<stdio.h> void main(void) { int search, c, n, count = 0, number, count1[100]={0}, i; char array[100]; printf("Enter the character of elements in array\n"); n = 5; printf("Enter %d numbers\n", n); for ( c = 0 ; c < n ; c++ ) scanf(" %c ",&array[c]); for(i=0;i<n;i++) { count1[array[i]]++; //if array[i] is N, then count1[N]++// } for(i=0;i<100;i++) { if(count1[i]) printf("%c is present %d times in array.\n",i,count1[i]); else printf(""); } return 0; }
Partially yes
As said, main has to return int.
Now, I asked before, what this line does?
and I say again! array is of char type, so you are indexing an array (the count1 array) with a letter. In C, this is not good.Code:count1[array[i]]++;
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
But you know what? I actually don't like this approach.
Think about the problem. You need for every piece of data a corresponding counter. It's a one to one assignment (I don't know if that makes sense as I said, but in math we have some set that every element corresponds to another. Here we have sets of equal size with elements that match to only one. Well, enough with math :P)
Use a struct, which has as members the piece of data ( a char ) and an integer counter. Then create one array only and then... well I' ll leave that to you. You found your way here, which means that you have understood how things go
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
But array is of char type!!!! In C, you can not index an array with a letter!
I said my opinion about the approach.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Ok, that was what another user suggested. I am new to programming.
Oh, ok then
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Your initial post said this was for numbers. Do you need just letters, now? Or letters and numbers both?
Actually you can because char is just an integer type.
And here you see how it works (assuming ASCII encoding for the characters):
The character 'a' has the ASCII value 97, 'b' is 98, 'c' is 99, 'd' is 100, ...
Now when you do
where array[i] holds a character, you get back the ASCII value for it. But since "count1" is declared having 100 elements (with indices 0 to 99) you are writing outside the legal memory for any lowercase character except 'a', 'b' and 'c'.Code:count1[array[i]]++
If you want to be able to count all characters you have to either increase "count1" (e.g. declaring it as count1[128] for all ASCII characters) or map the ASCII values to a smaller range.
Bye, Andreas
Last edited by AndiPersti; 01-14-2013 at 03:14 PM. Reason: typo
Thank you very much AndiPersti. I have now got it work properly. Hopefully this will be my last post.
Oh, that's why it didn't feel right when I was typing it.. Thanks for the correction! And SHD thanks too I guess
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Nearly finished on this program! Cant wait to say its actually done! Been my life for the last week ( and yours too ).
Don't forget to inform us
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson