I have an array of structure records and a function insert to insert or update the records.
The insert function take list(an array of records),name (book name),author, year,copies and size n of the list
It updates the record if it find's one otherwise inserts a new one. here n=7
void insert(struct books *list,char name[],char author[],int year,int copies,int n)
{
int i,found=0,empty;
for(i=0;(i<n) && (found==0);i++)
{
// update works fine
if( strcmp(name,list[i].name)==0 && strcmp(author,list[i].author)==0 )
{
list[i].copies=copies;
list[i].year=year;
printf("\n\n####################################################\n");
printf("####\tRecord was successfully updated!\t####\n");
printf("####################################################\n");
found=1;
}
//get an empty record
if(strcmp(list[i].author,"i")==0){empty=i;}
}
//insert gives segmentation error
if(found==0)
{
strcpy(list[empty].name,name);
strcpy(list[empty].author,author);
list[empty].year=year;
list[empty].copies=copies;
printf("\n\n####################################################\n");
printf("####\tRecord was successfully inserted!\t####\n");
printf("####################################################\n");
}
}
My list array is:
A
Ruby On Rails
2004
100
B
Inferno
1993
453
C
Harry Potter and the soccers stones
2012
150
D
Harry Potter and the soccers stone
2012
150
E
Learn Python Easy Way
1967
100
F
Ruby On Rails
2004
130
i
i
0
0
Why is it giving Segmentation error: 11?
Probably you need to initialize empty. Do
empty=0;
And really, SO is not a debugging service. So stop asking questions like this.
Related
am currently working on a project wherein am supposed to model public acceptance on pricing schemes.
The independent variables being used for model:- Age, gender,income etc... which are categorical in nature, so I converted them into factored variables using as.factor() function.
Age Gender Income
0 1 2
0 0 0
0 0 1
I have certain other variables like Transit satisfaction, Environment improvement etc... which are ordered factors on scale of 1 to 5 . 1 being extremely dissatisfied and 5 being very satisfied.
My model is as follows :-
mdl = oglmx( prcing ~Ann_In1+Edu+Env_imp+rs_imp,data=cpdat, link = "logit", constantMEAN = F, constantSD = F, delta = 0, threshparam = NULL)
summary(mdl)
Estimate Std. error t value Pr(>|t|)
Ann_In11 0.1605540 0.3021613 0.5314 0.5951749
Ann_In12 -0.9556992 0.4218504 -2.2655 0.0234824 *
Edu1 0.0710699 0.2678081 0.2654 0.7907196
Edu2 1.0732587 0.7112519 1.5090 0.1313061
Env_imp.L -0.8524288 0.4899275 -1.7399 0.0818752 .
Env_imp.Q 0.0784353 0.3936332 0.1993 0.8420595
Env_imp.C 0.4589036 0.4498676 1.0201 0.3076878
Env_imp^4 -0.2219108 0.4423486 -0.5017 0.6159032
rd_sft.L 2.6335035 0.7362206 3.5771 0.0003475 ***
rd_sft.Q -0.7064391 0.5773880 -1.2235 0.2211377
rd_sft.C 0.0130127 0.4408486 0.0295 0.9764519
rd_sft^4 -0.2886550 0.3582014 -0.8058 0.4203318
I obtained the results as below. Am unable to interpret the results. Any leads in this can be very helpful.
In case of rd_sft (road safety ) as rd_sft.L (linear) is signiicant than other levels, can we neglect the other levels i.e Q,C,^4 in model formation ??
please through some light on model formulation and its intepretation as i am new to R.
I am creating a program that works with geographic information. I have data that contains what geographic units touch each other. The function that fails is intended to add neighboring units to an array based on population (for example, it starts with a unit, then adds the most populous neighboring unit to the array, and then adds the most populous unit that touches one of the units in the array, and continues this until it reaches a population limit). The way I am doing this is using a for loop, and then using an array of the total units that have been sorted by population. I then find the index of the first (and therefore most populous) neighbor using the intersection between the neighbors of the units in the array and the neighbors of each unit in the array of total units. The following is my code (please excuse the clunkiness):
func createDistrict () {
if useBoard.isEmpty == false {
useBoard.sort(by: {$0.population > $1.population})
var maxPop = useBoard.first!.population
district.removeAll()
district.append(useBoard.first!)
useBoard.removeFirst()
for i in 0...useBoard.count - 1 {
if useBoard.indices.contains(i) {
if useBoard[i] == nil || district.map({$0.population}).reduce(0,+) > districtMax {
break
}
} else {
break
}
useBoard.sort(by: {$0.population > $1.population})
var superArray:[Precinct] = []
district.forEach { (z) in
superArray += z.neighbors
Array(Set(superArray))
}
var nextPre = useBoard.firstIndex { (l) -> Bool in
Set(l.neighbors).intersection(Set(superArray)).isEmpty == false
}
if nextPre == nil {
break
}else {
var temporary = Set(useBoard[nextPre!].neighbors).intersection(Set(superArray))
var newString = ""
var newTemp = Array(temporary)
for t in 0...newTemp.count - 1 {
var next = useBoard.firstIndex { (k) -> Bool in
k == newTemp[t]
}
newString.append("\(newTemp[t]) (\(next)), ")
}
print("\(useBoard[nextPre!].precinctID) (\(nextPre!)) touches \(newString)")
}
district.append(useBoard[nextPre!])
useBoard.remove(at: nextPre!)
}
}
district.forEach { (p) in
print("\(p.precinctID)")
}
}
In this function, var nextPre = useBoard.firstIndex { (l) -> Bool in Set(l.neighbors).intersection(Set(superArray)).isEmpty == false} is used to find the index of the most populous neighbor. However, when I test it using print, I get an incorrect output. In the following excerpt, the values in the parentheses are just the indices and don't really matter. The output:
2104 (8) touches 1987 (Optional(710)), 2676 (Optional(1591)),
2387 (10) touches 2105 (Optional(2140)),
2274 (11) touches 2273 (Optional(52)), 2386 (Optional(236)),
2275 (14) touches 2276 (Optional(22)), 2105 (Optional(2138)), 2273 (Optional(51)),
2276 (21) touches 2389 (Optional(1638)), 2273 (Optional(50)), 2274 (nil), 2275 (nil), 2277 (Optional(2771)), 2386 (Optional(234)),
2067 (35) touches 2404 (Optional(76)), 2212 (Optional(944)),
2406 (40) touches 2404 (Optional(75)), 2070 (Optional(1771)),
2440 (42) touches 2212 (Optional(942)), 2388 (Optional(497)), 2441 (Optional(1364)),
2273 (46) touches 2386 (Optional(230)), 2276 (nil), 2064 (Optional(384)), 2275 (nil), 2105 (Optional(2133)), 2274 (nil), 2387 (nil),
1795 (55) touches 1891 (Optional(1212)),
1908 (41) touches 2638 (Optional(2568)), 1869 (Optional(474)),
2404 (70) touches 2212 (Optional(938)), 2070 (Optional(1766)), 2069 (Optional(365)), 2068 (Optional(581)), 1743 (Optional(2453)), 2405 (Optional(2442)), 2387 (nil), 2105 (Optional(2130)), 2284 (Optional(2792)),
2736 (70) touches 2548 (Optional(1314)), 2420 (Optional(1305)),
1798 (52) touches 2419 (Optional(270)),
1907 (45) touches 1912 (Optional(1611)), 2737 (Optional(2082)),
As you can see, the neighbors are off by 1. For example, 2104 is the first unit. Then 2387 touches 2105, which is 2104 + 1. Then 2274 touches 2386, which is 2387 - 1. Then 2275 touches 2105, which is 2104 + 1. The .intersection should find the first unit that touches the units in the array, yet it finds the first unit that touches a unit's name + 1. I have no idea how this is occurring, as the geographic units are stored in a custom object, not an integer or any other number variable. Here is the custom object:
class Precinct {
var precinctID:String
var population:Int
var neighbors:[Precinct]
init(precinctID:String, population:Int, neighbors:[Precinct]){
self.precinctID = precinctID
self.population = population
self.neighbors = neighbors
}
}
extension Precinct: Equatable {
static func == (lhs: Precinct, rhs: Precinct) -> Bool {
return lhs.precinctID == rhs.precinctID && lhs.population == rhs.population && lhs.neighbors == rhs.neighbors
}
}
extension Precinct: Hashable {
var hashValue: Int {
return precinctID.hashValue ^ population.hashValue
}
}
extension Precinct: CustomStringConvertible {
var description: String {
return "\(precinctID)"
}
}
What's going wrong and how can I fix it? Thanks.
The problem is due to the fact that you are removing elements of useBoard while at the same time iterating over useBoard. You are printing the indices "\(newTemp[t]) (\(next)), " and then five lines later removing an index; before repeating the process. While you can change the values of a collection you are iterating over, never change the size of the collection at the same time.
A first step may be to copy useBoard before running the outer loop. Keep it constant so that you iterate over all of its contents but use the copy for all of your logic. I have trouble following the intent of your code.
As far as the hash, that is not your problem. However it is not ideal. Swift provides a default hash that is almost always better. Just allow Swift to synthesize its own algorithm by changing that extension to this.
extension Precinct: Hashable {}
There are a couple of issues. Please remove the call to sort useBoard within the outer loop. That has no effect because useBoard was already sorted before entering the loop. Also Array(Set(superArray)) is not doing anything for you.
Good luck.
I'm trying to follow a document that has some code on text mining clustering analysis.
I'm fairly new to R and the concept of text mining/clustering so please bear with me if i sound illiterate.
I create a simple matrix called dtm and then run kmeans to produce 3 clusters. The code im having issues is where a function has been defined to get "five most common words of the documents in the cluster"
dtm0.75 = as.matrix(dt0.75)
dim(dtm0.75)
kmeans.result = kmeans(dtm0.75, 3)
perClusterCounts = function(df, clusters, n)
{
v = sort(colSums(df[clusters == n, ]),
decreasing = TRUE)
d = data.frame(word = names(v), freq = v)
d[1:5, ]
}
perClusterCounts(dtm0.75, kmeans.result$cluster, 1)
Upon running this code i get the following error:
Error in colSums(df[clusters == n, ]) :
'x' must be an array of at least two dimensions
Could someone help me fix this please?
Thank you.
I can't reproduce your error, it works fine for me. Update your question with a reproducible example and you might get a more useful answer. Perhaps your input data object is empty, what do you get with dim(dtm0.75)?
Here it is working fine on the data that comes with the tm package:
library(tm)
data(crude)
dt0.75 <- DocumentTermMatrix(crude)
dtm0.75 = as.matrix(dt0.75)
dim(dtm0.75)
kmeans.result = kmeans(dtm0.75, 3)
perClusterCounts = function(df, clusters, n)
{
v = sort(colSums(df[clusters == n, ]),
decreasing = TRUE)
d = data.frame(word = names(v), freq = v)
d[1:5, ]
}
perClusterCounts(dtm0.75, kmeans.result$cluster, 1)
word freq
the the 69
and and 25
for for 12
government government 11
oil oil 10
I have a little problem with my code. I have a table containing car details, name, price and quantity, so I am trying to create a function called buy which will be used to buy a specific car. When a user buys eg 5 BMW cars, they will call buy_car(bmw,5). Now after this I want to update the new value of quantity for BMW cars.
My attempt is below but I can't seem to work around it, I am new to Erlang.
buy_car(X,Ncars) ->
F = fun() ->
%% ----first i find the number of car X available in the shop
[Xcars] = mnesia:read({car,X}),
Nc = Xcars#car.quantity,
Leftcars = Xcars#car{quantity = Nc - Ncars},
%% ---now we update the database
mnesia:write(Leftcars),
end,
mnesia:transaction(F).
Please help me with how I can write a function that buys cars from the shop.
But your implementation works fine except you added illegal comma after mnesia:write(Leftcars).
Here is code that works (I tried your implementation as buy_car2).
-module(q).
-export([setup/0, buy_car/2, buy_car2/2]).
-record(car, {brand, quantity}).
setup() ->
mnesia:start(),
mnesia:create_table(car, [{attributes, record_info(fields, car)}]),
mnesia:transaction(fun() -> mnesia:write(#car{brand=bmw, quantity=1000}) end).
buy_car(Brand, Ncars) ->
F = fun() ->
[Car] = mnesia:read(car, Brand), % crash if the car is missing
mnesia:write(Car#car{quantity = Car#car.quantity - Ncars})
end,
mnesia:transaction(F).
buy_car2(X,Ncars) ->
F = fun() ->
%% ----first i find the number of car X available in the shop
[Xcars] = mnesia:read({car,X}),
Nc = Xcars#car.quantity,
Leftcars = Xcars#car{quantity = Nc - Ncars},
%% ---now we update the database
mnesia:write(Leftcars)
end,
mnesia:transaction(F).
I would do something like below:
Considering the record is defined as :
-record(car_record, {car, quantity}).
The following function will update the data:
buy_car(X,NCars) ->
Row = #car_record{car = X, quantity = NCars}.
mnesia:ets(fun()-> mnesia:dirty_write(Row) end),
mnesia:change_table_copy_type(guiding_data, node(), disc_copies).
To use the above method, mnesia table must be created as "ram_copies" and with no replication nodes. Also, if there are lot of updates happening, you might not want to copy the ram_copies to disk for every update due to performance issues, rather you will do it in time triggered manner.
i have a table
Fields
class 1
class 2
class 3
class 4
a1
10
240
340
401
a2
12
270
340
405
a3
12
270
340
405
a4
15
270
360
405
a5
17
720
530
450
i have this in grid as well as in Json.store , what i have to do is perform mathematical calculation each time the grid is refreshed by "table name".reconfigure(..... , ....)
consider the column "class1" ,
value(a5) = ( value(a1)+ 2*value(a2) + 3*value(a3) ) /value(a4)
can anybody please help he on this problem ,
I will be very very Thankful for help :)
As I'm not sure what aspect of the problem you are having difficulty with, I'll address both at a high level.
Generally speaking you want to have your reconfigure method update the Ext Store, which will then trigger an event that the Grid should handle. Basically, change the Store and your Grid will be updated automatically.
As far as generating the correct new row... it seems fairly straightforward - a rough pass:
/*for each field foo_X through foo_N:*/
var lastElementIndex = store.data.size-1;
var total = 0;
for (var i=0; i<; i++) {
if (i != lastElementIndex) {
total += store.data[i].get(foo_X)*i;
} else {
total = total/store.data[i].get(foo_x);
}
}
/*construct your json object with the field foo*/
/*after looping through all your fields, create your record and add it to the Store*/