Hi,
I have a list which contains numeric strings. The numeric strings in list may start with any number from 1 to 9 (it doesn't start with "0"). Now i need to find three top occurrences in the list.
Ex:
1234
2345
2343
4356
6434
2343
2222
4545
6666
6653
top three occurrences in the above list respectively are 2,6,4.
Loading

VulpesRank #55 · 29.2K reputationPosted Sep 28, 2011, 4:06 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2011, 3:09 AM EDT
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
int main(array
{
array
{
"1234",
"2345",
"2343",
"4356",
"6434",
"2343",
"2222",
"4545",
"6666",
"6653"
};
List
array
for each(String ^s in list)
{
int i = s[0] - 48;
results[i]++;
}
for(int i = 1; i < 10; i++)
{
results[i] = 10 * results[i] + i; // EDITED
}
Array::Sort(results);
Array::Reverse(results);
Console::WriteLine("Top 3 occurrences are\n");
for(int i = 0; i < 3; i++) Console::WriteLine("{0} occurs {1} time(s)", results[i] % 10, results[i] / 10); // EDITED
Console::ReadKey();
return 0;
}
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 23, 2011, 3:36 AM EDT
what does that 48 mean?
results[i] = 100000 * results[i] + i;
what does this 100000 mean and why are you multiplying it with results[i] and adding with i?
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2011, 4:12 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 23, 2011, 4:15 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 23, 2011, 4:39 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2011, 5:01 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 23, 2011, 5:30 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 26, 2011, 1:24 AM EDT
its like i need binary value to occupy 5 places even though it is lees than that. can you please tell me how i can make that happen?
VulpesRank #55 · 29.2K reputationPosted Sep 26, 2011, 1:34 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2011, 1:47 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2011, 3:01 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 27, 2011, 3:04 AM EDT
I think Single::Parse(fStr); will generate and exception if it couldn't be parsed right? anyways the method i stated is good i think. anyways thanks vulpes.
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 27, 2011, 3:57 AM EDT
I am incrementing my array size
Array::Resize(lang, lang->Length + 1);
after that sometimes one of the array element is not defined and hence causing break to the normal execution. how to validate whether anything is validated or not?
And more thing is i have created an instance to a class as follows:
static namespace::classname ^ instancename;
instancename= gcnew namespace::classname ();
Now i want create instance in some other place same as above but if i do that a array which is written in above class is getting loaded again which causing the length and elements to get loaded again. so how can i avoid this.
I wrote like this which is working fine but showing a warning. will this work perfectly or should i change that.?
classname ^ instancename2;
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2011, 4:43 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 27, 2011, 4:50 AM EDT
and for the second question i am asking how to declare an instance if i should not populate the array in the class again. If i write a line like this
static namespace::classname ^ instancename;
instancename= gcnew namespace::classname ();
again when i want to create instance of the same class for second time the array gets loaded again so i declared as follows,
LanguageTable ^ PrintLanguageData;
will this work or else should i change that?
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2011, 6:20 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 27, 2011, 6:57 AM EDT
If i write the line
instancename = new classname();
classname(void)
{
language_string_id = gcnew array
}
~classname(void)
{
delete language_string_id;
}
The text marked in red contains data which i load when i call it with 1st instance, will be lost if i create instance as you stated. So i tried writing as below:
classname ^ instancename2;
which is showing up a warning c4101 : unreferenced local variable. Can i use it that way or is there any other way to do?
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2011, 7:14 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 27, 2011, 7:18 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2011, 7:54 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 27, 2011, 9:06 PM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 28, 2011, 2:14 AM EDT
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 28, 2011, 2:42 AM EDT
What about my other question?
By default all the array elements will be "undefined values" if i just create it as follows:
String ^ copy = "";
array
for(int i = 0; i < 6; i++)
{
Array::Resize(lang, lang->Length + 1);
lang[lang - 1] = lang[lang - 1] + copy;
}
Due to some operation i am missing the last element of the array, which shows up as undefined value. Is there a way how i can validate this i.e i want to be sure that all the elements of the array are valid before doing some operation on the array.
I want do as
if(lang[lang - 1] != null) //null how we do it in "C"
{
//my operation on "lang" array
}
VulpesRank #55 · 29.2K reputationPosted Sep 28, 2011, 3:12 AM EDT
Incidentally, why are you resizing arrays when the List
Karthik AgarwalRank #2,270 · 620 reputationPosted Sep 28, 2011, 3:26 AM EDT