I have a population of N people. Then randomly I shall have to assign their sex male or female. Also they need to be randomly assigned some wealth.
Then the further arithmetical operation will be different for males and females, i.e, the if the person is male his wealth in next step shall be 1.2 times and if female, say will be 1.5 times, the scheme is something like that.
How can I implement this in fortran? For this I shall have to store the wealth and gender of people inside an array. Is there any way of storing a character like 'Male' and also some numerical value for wealth both inside a fortran variable?
Like, If a(100) is male, then the wealth of a(100)=a(100)*1.2 in next step.
How to implement this by storing both character and numerical value inside an array in fortran?
Related
I don't have knowledge on XSLTs with nested looping and keeping values global. Below is what I want to achieve.
Standard (Hierarchy 1)
Student (Hierarchy 2)
Results (Hierarchy 3) -> This calls a procedure to look at every result(mark) of all subjects and display when the mark is above 40.
Now the requirement is, I need to add results/marks of all subjects which are above 40. Also, I need to get total results of all student.
In XSLT, the scope is no longer valid when it loops through the next student. How do I retain the sum of all students in a global variable?
Thanks for any help provided.
Sample
Need to calculate sum of all subjects for all students who marks are greater than 40. The yellow highlighted ones should not be considered for calculating sum.
XSLT is a functional language; variables are assigned a value when declared, and their value never changes.
So in XSLT, unlike the other programming languages which appear to have shaped your thinking, you do not calculate the sum of all marks for each student, or all marks which are greater than 40. Instead, you declare a top-level variable and you specify what its value should be. Something like
<!--* calculate the total sum of marks awarded to all students.
* (This makes no sense, but it is what the description says.
* Mine not to reason why.)
*-->
<xsl:variable name="total-marks"
select="sum(//student/subject/#mark)"/>
<!--* calculate the total sum of marks greater than 40 awarded
* to all students.
* (This also makes no sense, but it is again what the description
* appears to say. Glad this is someone else's problem, not mine.)
*-->
<xsl:variable name="total-marks-over-40"
select="sum(//student/subject/#mark[. > 40])"/>
It is possible that these variables don't calculate the right thing, because I do not really understand from your description what you are doing. I suppose that if you want, later, to use these to calculate the average mark, and the average mark greater than 40, by dividing them over the number of marks given, and the number of marks over 40, then they would make sense.
It is not just possible but almost certain that these variables don't fit the structure of the XML you have in mind, since you haven't described it for us. The sample expressions given assume that each student is represented by a student element, which contains a number of subject children, each with a numeric mark attribute representing the mark given to that student in that subject.
I imagine what I'm asking is pretty basic, but I'm not entirely certain how to do it in SAS.
Let's say that I have a range of variables, or an array, x1-xn. I want to be able to run a program that uses the number of variables within that range as part of its calculation. But I want to write it in such a way that, if I add variables to that range, it will still function.
Essentially, I want to be able to create a variable that if I have x1-x6, the variable value is '6', but if I have x1-x7, the value is '7'.
I know that :
var1=n(of x1-x6)
will return the number of non-missing numeric variables.. but I want this to work if there are missing values.
I hope I explained that clearly and that it makes sense.
Couple of things.
First off, when you put a range like you did:
x1-x7
That will always evaluate to seven items, whether or not those variables exist. That simply evaluates to
x1 x2 x3 x4 x5 x6 x7
So it's not very interesting to ask how many items are in that, unless you're generating that through a macro (and if you are, you probably can have that macro indicate how many items are in it).
But the range x1--x7 or x: both are more interesting problems, so we'll continue.
The easiest way to do this is, if the variables are all of a single type (but an unknown type), is to create an array, and then use the dim function.
data _null_;
x3='ABC';
array _temp x1-x7;
count = dim(_temp);
put count=;
run;
That doesn't work, though, if there are multiple types (numeric and character) at hand. If there are, then you need to do something more complex.
The next easiest solution is to combine nmiss and n. This works if they're all numeric, or if you're tolerant of the log messages this will create.
data _null_;
x3='ABC';
count = nmiss(of x1-x7) + n(of x1-x7);
put count=;
run;
nmiss is number of missing, plus n is number of nonmissing numeric. Here x3 is counted with the nmiss group.
Unfortunately, there is not a c version of n, or we'd have an easier time with this (combining c and cmiss). You could potentially do this in a macro function, but that would get a bit messy.
Fortunately, there is a third option that is tolerant of character variables: combining countw with catx. Then:
data _null_;
x3='ABC';
x4=' ';
count = countw(catq('dm','|',of x1-x7),'|','q');
put count=;
run;
This will count all variables, numeric or character, with no conversion notes.
What you're doing here is concatenating all of the variables together with a delimiter between, so [x1]|[x2]|[x3]..., and then counting the number of "words" in that string defining word as thing delimited by "|". Even missing values will create something - so .|.|ABC|.|.|.|. will have 7 "words".
The 'm' argument to CATQ tells it to even include missing values (spaces) in the concatenation. The 'q' argument to COUNTW tells it to ignore delimiters inside quotes (which CATQ adds by default).
If you use a version before CATQ is available (sometime in 9.2 it was added I believe), then you can use CATX, but you lose the modifiers, meaning you have more trouble with empty strings and embedded delimiters.
In the textbook "Starting Out In C++" by Gaddis in chapter 1 the author says that some numbers like zip codes are intended for humans to read, to be printed out on the screen to look at and to not calculate with so they should be stored in string data type not numeric data types. But there is a couple of other reasons why this statement is true. The only other reason why I can think this would be true is if you were to enter a zip code with an ending like 37217-1221 you may have to use string catenation to only use the first five digits chopping of the characters after the -1221. What would be some other reasons for the statement "If a number is not going to be used in an arithmetic operation, store it in a string data type". Any answers would be greatly appreciated.
Zipcodes simply are not numeric data. As you point out, zipcodes can contain extensions, which numeric data does not represent. They can also contain significant leading zeros. Some postal code schemes can also contain letters.
Your questions was a bit...not of a questions? That's the best I can explain it. Anyway, a string is text and an integer or number is numerical and should only be used for calculations or counting. For example:
A zip code is a number but you will never do calculations with it. A zip code is something you reference as a place and has no counting purpose. If you think this could confuse you later on try to give the variable with the zip code an assignment of a String so that you cannot try to do any sort of math with the variable.
suppose I have following paragraph of strings.
"A tape suggests the crew of the Costa Concordia ship mentioned only a "blackout" in a communication with Italian officials after hitting rocks.A tape suggests the crew of the Costa Concordia ship mentioned only a "blackout" in a communication with Italian officials after hitting rocks."
Now I have to write a C++ pgm which in single traversal gives the output like
A 2
tape 2
suggests 2
the 2
......... and so on.
Note: Only one iteration, NO LOOP. using pointers.
As you traverse the text, keep a record of what words you've seen so far and how many times you've seen each.
Then, when you've finished, print out the results.
You must show us what you wrote yet.
If you are stuck, and assuming you have to write in C ( you spoke about pointer ), take a look at
strtok -- Split string into tokens
The operation would then be linear with a hash table ( n call to strtok, ~O(1) access to the hash at each iteration, plus ~O(1) insertion if the word does not exist).
Hash table can be tricky to write in full C (but you can look at prefix tree for example). You can use libc hash table, or if it is an option, use c++ map.
The following function returns a data.frame with two columns:
fetch_count_by_day=function(con){
q="SELECT t,count(*) AS count FROM data GROUP BY t"
dbGetQuery(con,q) #Returns a data frame
}
t is a DATE column, so output looks like:
t count(*)
1 2011-09-22 1438
...
All I'm really interested in is if any records for a given date already exist; but I will also use the count as a sanity check.
In C++ I'd return a std::map<std::string,int> or std::unordered_map<std::string,int> (*).
In PHP I'd use an associative array with the date as the key.
What is the best data structure in R? Is it a 2-column data.frame? My first thought was to turn the t column into rownames:
...
d=dbGetQuery(con,q)
rownames(d)=d[,1]
d$t=NULL
But data.frame rownames are not unique, so conceptually it does not quite fit. I'm also not sure if it makes using it any quicker.
(Any and all definitions of "best": quickest, least memory, code clarity, least surprise for experienced R developers, etc. Maybe there is one solution for all; if not then I'd like to understand the trade-offs and when to choose each alternative.)
*: (for C++) If benchmarking showed this was a bottleneck, I might convert the datestamp to a YYYYMMDD integer and use std::unordered_map<int,int>; knowing the data only covers a few years I might even use a block of memory with one int per day between min(t) and max(t) (wrapping all that in a class).
Contingency tables are actually arrays (or matrices) and can very easily be created.The dimnames hold the values and the array/matrix at its "core" holds the count data. The "table" and "tapply" functions are natural creators. You access the counts with "[" and use dimnames( ) followed by an "[" to get you the row annd column names. I would say it was wiser to use the "Date" class for dates than storing in "character" vectors.