Why this tabulate function is not reading a two dimensional array in C? - cs50

I could use some help with my CS50 Runoff problem Particularly with my tabulate function.
I wrote the following code:
void tabulate(void)
{
for (int i = 0; i < voter_count; i++)
{
for (int j = 0; j < candidate_count; i++)
{
int c = preferences[i][j];
if (!candidates[c].eliminated)
{
candidates[c].votes++;
break;
}
}
}
return;
}
but when i check it, check50 detects the following errors:
:( tabulate counts votes when multiple candidates are eliminated
tabulate function did not produce correct vote totals
:( tabulate handles multiple rounds of preferences
tabulate function did not produce correct vote totals
I really do not see the problem.
Could someone please help me?
Thank you very much!!!

Related

Bubble sort doesn't print the right winner - CS50 week 3 as a beginner - I'm lost

please help a newbie like me, I'm lost, I don't get it - I looked at answers, it still doesn't work and I can't spot the error.
I'm working on plurality, I know we can solve it without bubble sort but I want to find out why my solution isn't working.
I bubble sorted it - and I can't get the right names to print.
I feel like I tried to put the printf everywhere in any bracket and it is never right and shows me any solution but the right one.
Currently, it prints the last 2 names regardless of who has the most votes.
I feel like it's something pretty simple and basic but it's been 3 days now and I can't get anywhere - please help me, where am I misunderstanding things?
void print_winner(void)
{
int temp;
for (int i = 0; i < candidate_count; i++) //going through each name/vote struct - -1 because c-count is acvg arguments
{
for(int j = 0; j < (candidate_count - i - 1); j++) // c_c - i - 1 is for ignoring already compared candidates
{
if (candidates[j].votes < candidates[j + 1].votes)
{
temp = candidates[j].votes;
candidates[j].votes = candidates[j + 1].votes;
candidates[j + 1].votes = temp;
}
}
}
int top = candidates[candidate_count - 1].votes;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == top)
{
printf("%s\n", candidates[i].name);
}
}
return;
}

How to get direct access to polygons in VTK?

I found this post online (dates back to 2013) when I had trouble getting direct access to a specific cell in a vtkPolyData. I am using the latest version: VTK 8.1.1 and it seems like the newer version of VTK still has this issue.
polys->InitTraversal();
for(int i = 0; i < polys->GetNumberOfCells(); i++)
{
polys->GetNextCell(idList); // This sequential method gets the point IDs correctly
int a = idList->GetId(0);
int b = idList->GetId(1);
int c = idList->GetId(2);
}
However, the direct access method seems to have issues
polys->InitTraversal();
for(int i = 0; i < polys->GetNumberOfCells(); i++)
{
polys->GetCell(i, idList); // This method returns wrong ids
int a = idList->GetId(0);
int b = idList->GetId(1);
int c = idList->GetId(2);
}
How can I get the point IDs in a specific cell without looping through all the cell? Isn't polys->GetCell(i, idList) meant to give you direct access to a specific cell?
For direct access, we can use vtkPolyData::GetCellPoints() method. For example we can do
vtkNew<vtkIdList> idL; // or auto idL = vtkSmartPointer<vtkIdList>::New();
poly->GetCellPoints( 13, idL ); // Assuming you want the points for 13th cell
for(auto i = 0; i < idL->GetNumberOfIds(); ++i)
std::cout<< idL->GetId(i) << std::endl;
For looping over all cells I prefer a while loop:
vtkNew<vtkIdList> idL;
poly->GetPolys()->InitTraversal();
while(poly->GetPolys()->GetNextCell(idL)){
for(auto i = 0; i < idL->GetNumberOfIds(); ++i)
std::cout<< idL->GetId(i) << std::endl;
}

Search an integer in a row-sorted two dim array, is there any better approach?

I have recently come across with this problem,
you have to find an integer from a sorted two dimensional array. But the two dim array is sorted in rows not in columns. I have solved the problem but still thinking that there may be some better approach. So I have come here to discuss with all of you. Your suggestions and improvement will help me to grow in coding. here is the code
int searchInteger = Int32.Parse(Console.ReadLine());
int cnt = 0;
for (int i = 0; i < x; i++)
{
if (intarry[i, 0] <= searchInteger && intarry[i,y-1] >= searchInteger)
{
if (intarry[i, 0] == searchInteger || intarry[i, y - 1] == searchInteger)
Console.WriteLine("string present {0} times" , ++cnt);
else
{
int[] array = new int[y];
int y1 = 0;
for (int k = 0; k < y; k++)
array[k] = intarry[i, y1++];
bool result;
if (result = binarySearch(array, searchInteger) == true)
{
Console.WriteLine("string present inside {0} times", ++ cnt);
Console.ReadLine();
}
}
}
}
Where searchInteger is the integer we have to find in the array. and binary search is the methiod which is returning boolean if the value is present in the single dimension array (in that single row).
please help, is it optimum or there are better solution than this.
Thanks
Provided you have declared the array intarry, x and y as follows:
int[,] intarry =
{
{0,7,2},
{3,4,5},
{6,7,8}
};
var y = intarry.GetUpperBound(0)+1;
var x = intarry.GetUpperBound(1)+1;
// intarry.Dump();
You can keep it as simple as:
int searchInteger = Int32.Parse(Console.ReadLine());
var cnt=0;
for(var r=0; r<y; r++)
{
for(var c=0; c<x; c++)
{
if (intarry[r, c].Equals(searchInteger))
{
cnt++;
Console.WriteLine(
"string present at position [{0},{1}]" , r, c);
} // if
} // for
} // for
Console.WriteLine("string present {0} times" , cnt);
This example assumes that you don't have any information whether the array is sorted or not (which means: if you don't know if it is sorted you have to go through every element and can't use binary search). Based on this example you can refine the performance, if you know more how the data in the array is structured:
if the rows are sorted ascending, you can replace the inner for loop by a binary search
if the entire array is sorted ascending and the data does not repeat, e.g.
int[,] intarry = {{0,1,2}, {3,4,5}, {6,7,8}};
then you can exit the loop as soon as the item is found. The easiest way to do this to create
a function and add a return statement to the inner for loop.

Is it possible to do a Levenshtein distance in Excel without having to resort to Macros?

Let me explain.
I have to do some fuzzy matching for a company, so ATM I use a levenshtein distance calculator, and then calculate the percentage of similarity between the two terms. If the terms are more than 80% similar, Fuzzymatch returns "TRUE".
My problem is that I'm on an internship, and leaving soon. The people who will continue doing this do not know how to use excel with macros, and want me to implement what I did as best I can.
So my question is : however inefficient the function may be, is there ANY way to make a standard function in Excel that will calculate what I did before, without resorting to macros ?
Thanks.
If you came about this googling something like
levenshtein distance google sheets
I threw this together, with the code comment from milot-midia on this gist (https://gist.github.com/andrei-m/982927 - code under MIT license)
From Sheets in the header menu, Tools -> Script Editor
Name the project
The name of the function (not the project) will let you use the func
Paste the following code
function Levenshtein(a, b) {
if(a.length == 0) return b.length;
if(b.length == 0) return a.length;
// swap to save some memory O(min(a,b)) instead of O(a)
if(a.length > b.length) {
var tmp = a;
a = b;
b = tmp;
}
var row = [];
// init the row
for(var i = 0; i <= a.length; i++){
row[i] = i;
}
// fill in the rest
for(var i = 1; i <= b.length; i++){
var prev = i;
for(var j = 1; j <= a.length; j++){
var val;
if(b.charAt(i-1) == a.charAt(j-1)){
val = row[j-1]; // match
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
row[j - 1] = prev;
prev = val;
}
row[a.length] = prev;
}
return row[a.length];
}
You should be able to run it from a spreadsheet with
=Levenshtein(cell_1,cell_2)
While it can't be done in a single formula for any reasonably-sized strings, you can use formulas alone to compute the Levenshtein Distance between strings using a worksheet.
Here is an example that can handle strings up to 15 characters, it could be easily expanded for more:
https://docs.google.com/spreadsheet/ccc?key=0AkZy12yffb5YdFNybkNJaE5hTG9VYkNpdW5ZOWowSFE&usp=sharing
This isn't practical for anything other than ad-hoc comparisons, but it does do a decent job of showing how the algorithm works.
looking at the previous answers to calculating Levenshtein distance, I think it would be impossible to create it as a formula.
Take a look at the code here
Actually, I think I just found a workaround. I was adding it in the wrong part of the code...
Adding this line
} else if(b.charAt(i-1)==a.charAt(j) && b.charAt(i)==a.charAt(j-1)){
val = row[j-1]-0.33; //transposition
so it now reads
if(b.charAt(i-1) == a.charAt(j-1)){
val = row[j-1]; // match
} else if(b.charAt(i-1)==a.charAt(j) && b.charAt(i)==a.charAt(j-1)){
val = row[j-1]-0.33; //transposition
} else {
val = Math.min(row[j-1] + 1, // substitution
prev + 1, // insertion
row[j] + 1); // deletion
}
Seems to fix the problem. Now 'biulding' is 92% accurate and 'bilding' is 88%. (whereas with the original formula 'biulding' was only 75%... despite being closer to the correct spelling of building)

Android drawing path on mapview using map overlay and GeoPoints

I am trying to draw a path between a number of geepoints on a map,whilst i iterate through an arraylist of geopoints. from this i am receiving a index out of bounds error, i have tired to debug it but haven't gotten any where. So could someone please point me in the right direction and thank you.
//making the array and inserting the geopoints
List <GeoPoint> points = new ArrayList<GeoPoint> ();
for (Iterator<Placemark> iter=places.iterator();iter.hasNext();) {
Placemark point = (Placemark)iter.next();
points.add(new GeoPoint(p.getLatitude(),p.getLongitude()));
}
//the loop which gives out a null exception
for (int j=0; j <points.size(); j++){
MyOverlay poin = new MyOverlay(points.get(j-1),points.get(j)); }
use the loop like this,
for (int j=0; j <points.size()-1; j++){
MyOverlay poin = new MyOverlay(points.get(j),points.get(j+1)); }
if still didn't work, try this in the for loop,
j<points.size()-2

Resources