I am using a function that has a segment (shown below) that was used in qsort to compare variable fields that are sorted in either ascending or desending order. I added a third order (assending is 0, descending is 1 and the third order is 2) where I sort gas and oil well classifications in the NormalizeResClass function that returns a different int for the classifications.
I added the code for (typsort == 2) as shown below, but it only sorts properly when I have the reserve class as the only type of sort. If I sort with a secondary sort like by reserve class and well name it dose not sort.
The original code in the else block uses *((int *) before xval and yval and I am weak on pointers. I know that it is casting to int but the NormalizeResClass function already returns an int so I don't need the cast, and if I use it I get a segmentaion fault.
static char *Strings = NULL; // Global var
int result= 0; // local var
int ft; // local var
// ft is checked before going to this to be >= 0
if (typsort == 2)
{
// the code that I have added
result = strncmp (Strings + NormalizeResClass (xval),
Strings + NormalizeResClass (yval), ft);
}
else
{
// this code was there and the sorting was right
result = strncmp (Strings + *((int *) xval),
Strings + *((int *) yval), ft);
}
This is the Normalize Function below I might need to make sure all of these have a unique
number a few have the same.
int NormalizeResClass (char rc)
{
/
Given a reserve class string , return a reserve class number.
*/
register int i;
register char *p, *q;
static struct
{
int index;
char str;
} RCposs[] =
{
{
1, "PROVED DEVELOPED PRODUCING"},
{
2, "PROVED PRODUCING SECONDARY"},
{
3, "PROVED PRODUCING TERTIARY"},
{
1, "PROVED PRODUCING"},
{
4, "PROVED SHUT-IN"},
{
5, "PROVED DEVELOPED NON-PRODUCING"},
{
5, "PROVED NON-PRODUCING"},
{
6, "PROVED DEVELOPED BEHIND-PIPE"},
{
6, "PROVED BEHIND-PIPE"},
{
8, "PROVED UNDEVELOPED SECONDARY"},
{
9, "PROVED UNDEVELOPED TERTIARY"},
{
7, "PROVED UNDEVELOPED"},
{
1, "PROVED"},
{
10, "PROBABLE DEVELOPED PRODUCING"},
{
11, "PROBABLE PRODUCING SECONDARY"},
{
12, "PROBABLE PRODUCING TERTIARY"},
{
13, "PROBABLE SHUT-IN"},
{
10, "PROBABLE PRODUCING"},
{
14, "PROBABLE DEVELOPED NON-PRODUCING"},
{
14, "PROBABLE NON-PRODUCING"},
{
15, "PROBABLE DEVELOPED BEHIND-PIPE"},
{
15, "PROBABLE BEHIND-PIPE"},
{
17, "PROBABLE UNDEVELOPED SECONDARY"},
{
18, "PROBABLE UNDEVELOPED TERTIARY"},
{
16, "PROBABLE UNDEVELOPED"},
{
20, "PROBABLE PRIMARY/SECONDARY"},
{
21, "PROBABLE SECONDARY"},
{
19, "PROBABLE"},
{
22, "POSSIBLE DEVELOPED PRODUCING"},
{
23, "POSSIBLE PRODUCING SECONDARY"},
{
24, "POSSIBLE PRODUCING TERTIARY"},
{
25, "POSSIBLE SHUT-IN"},
{
22, "POSSIBLE PRODUCING"},
{
26, "POSSIBLE DEVELOPED NON-PRODUCING"},
{
26, "POSSIBLE NON-PRODUCING"},
{
27, "POSSIBLE DEVELOPED BEHIND-PIPE"},
{
27, "POSSIBLE BEHIND-PIPE"},
{
29, "POSSIBLE UNDEVELOPED SECONDARY"},
{
30, "POSSIBLE UNDEVELOPED TERTIARY"},
{
28, "POSSIBLE UNDEVELOPED"},
{
22, "POSSIBLE"},
{
32, "PROSPECTIVE PRIMARY/SECONDARY"},
{
33, "PROSPECTIVE SECONDARY"},
{
31, "PROSPECTIVE"},
{
34, "ANALOGY"},
{
35, "DEPLETE"},
{
36, "PROV+PROB+POSS UNDEVELOPED RESERVES"},
{
37, "PROV+PROB+POSS DEVELOPED RESERVES"},
{
38, "PROV+POSS UNDEVELOPED RESERVES"},
{
39, "PROV+POSS DEVELOPED RESERVES"},
{
40, "PROB+POSS UNDEVELOPED RESERVES"},
{
41, "PROB+POSS DEVELOPED RESERVES"},
{
42, "PROV+PROB UNDEVELOPED RESERVES"},
{
43, "PROV+PROB DEVELOPED RESERVES"},
{
44, "RISKED PROV UNDEVELOPED RESERVES"},
{
45, "RISKED PROB UNDEVELOPED RESERVES"},
{
46, "RISKED POSS UNDEVELOPED RESERVES"},
{
47, "RISKED PROV BEHIND-PIPE RESERVES"},
{
48, "RISKED PROB BEHIND-PIPE RESERVES"},
{
49, "RISKED POSS BEHIND-PIPE RESERVES"},
{
50, "PLANT PRODUCTS SALES"},
{
51, "PROBABLE PLANT PRODUCTS SALES"},
{
52, "POSSIBLE PLANT PRODUCTS SALES"},
{
53, "POTENTIAL PLANT PRODUCTS SALES"},
{
54, "PROVED GAS PIPELINE SALES"},
{
55, "PROBABLE GAS PIPELINE SALES"},
{
56, "POSSIBLE GAS PIPELINE SALES"},
{
57, "POTENTIAL GAS PIPELINE SALES"},
{
58, "PROVED OIL PIPELINE SALES"},
{
59, "PROBABLE OIL PIPELINE SALES"},
{
60, "POSSIBLE OIL PIPELINE SALES"},
{
61, "POTENTIAL OIL PIPELINE SALES"},
{
0, NULL / terminator */ }};
for (i = 0; (q = RCposs[i].str); i++)
{
p = rc;
while (*p++ == *q++)
{
if ((*q == '\0'))
return RCposs[i].index;
}
}
return (0);
}
Based on the code that you have shown, it looks like the global Strings variable is a large block of memory which contains many different strings (one per record, presumably).
The original code:
result = strncmp (Strings + *((int *) xval),
Strings + *((int *) yval), ft);
compares the string at Strings[xval] to the string at Strings[yval], with maximum length ft.
I don't know what the NormalizeResClass(x) function does, but if it is going to work the in a similar fashion to the old code, it needs to return an int which represents the location of the beginning of a string in the Strings memory block. It must not return an index which would fall outside of that memory block, otherwise the call to strncmp would start reading from who knows where.
Update:
It looks like your NormalizeResClass function returns an integer ranking for a resource class string. In that case, you should not be using strncmp() and Strings[] at all. The code should look something like this:
if (typsort == 2)
{
int xRank = NormalizeResClass (xval);
int yRank = NormalizeResClass (yval);
if (xRank == yRank) { result = 0; }
else if (xRank > yRank) { result = 1; }
else if (xRank < yRank) { result = -1; }
}
Related
I would like get rows based on multiple constraints. One criteria works perfect like
A[A[full, 0] % 3 == 0, full];
But when I add a second criteria I get a compiler error. What is the correct way of doing this?
Array<double> A = new double[,] {
{ 1, 9, 20 },
{ 2, 11, 21 },
{ 9, 12, 21 },
{ 9, 13, 23 },
{ 9, 14, 24 },
{ 10, 12, 21 },
{ 14, 13, 23 },
{ 16, 14, 24 },
{ 19, 12, 21 },
{ 27, 13, 23 },
{ 23, 14, 24 },
{ 6, 15, 25 }
};
Array<double> matching =
A[A[full, 0] % 3 == 0 && A[full, 1] > 10, full];
The && operator is not overloaded for Array<T>. It returns a bool in C# - not an array as you might expect. Use the corresponding function ILMath.and(A, B) instead and it will work.
I have a double array where I need to search the first row using a jtextfield input to get the index (position of [i]), then use that index number to identify the second row location and use the variable in the second array row. I'm using this to get the Gross Profit Margin Multiplier in second row of the array based on the position of the percentage margin in the first row array.
Do appreciate your help. I have searched other array links but they deal ether with the locations and not the contents of the locations.
public void getMarginArray(){
double[][] margn = {{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50},
{1.11, 1.12, 1.13, 1.15, 1.16, 1.17, 1.19, 1.20, 1.22, 1.23, 1.25, 1.26, 1.28, 1.30, 1.31, 1.33, 1.35, 1.37, 1.39, 1.41, 1.43, 1.45, 1.47, 1.49, 1.51, 1.55, 1.56, 1.59, 1.61, 1.64, 1.67, 1.70, 1.72, 1.75, 1.79, 1.82, 1.86, 1.89, 1.92, 1.96, 2.00}};
Double MARG = Double.parseDouble( jtxtfldMargin.getText());
Double MAR1;
for (int i=0; i<margn.length; i++){
if (margn[0][i] ==MARG){
Double MAR1=margn[1][i];
System.out.println(margn[0][i]);
System.out.println(margn[1][i]);
System.out.println(MAR1);
};
};
I was able to get what I needed by changing to two separate arrays. Now when the first array matches the text field, the location is used for the second array to get the multiplier:
public void getMarginArray(){
double[] doubleArray1 = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
double[] doubleArray2 = {1.11, 1.12, 1.13, 1.15, 1.16, 1.17, 1.19, 1.20, 1.22, 1.23, 1.25, 1.26, 1.28, 1.30, 1.31, 1.33, 1.35, 1.37, 1.39, 1.41, 1.43, 1.45, 1.47, 1.49, 1.51, 1.55, 1.56, 1.59, 1.61, 1.64, 1.67, 1.70, 1.72, 1.75, 1.79, 1.82, 1.86, 1.89, 1.92, 1.96, 2.00};
Double MARG = Double.parseDouble( jtxtfldMargin.getText());
Double margMult;
for(int i = 0; i<doubleArray1.length; i++){
for(int j = 0; j<doubleArray2.length;j++){
if(doubleArray1[i] == MARG ){
margMult = doubleArray2[i];
System.out.println(margMult);
}
}
}
}
I want to use eratosthenes method
to get prime numbers with swift. I create first function to return new array without those numbers that can be divided for specific multiplier, then create second function to create new array every time with new P multiplier. I wonder why its not work (look like it somehow pass old array, i dont know why). It should print new array of prime numbers at the end:
var simpleArr : [Int] = []
for i in 2...100 {
simpleArr.append(i)
}
func arrayEcludingDivingByP (p: Int, arrToCheck : [Int]) -> Array<Int>{
var tmp : [Int] = []
for (ob, index) in arrToCheck.enumerated() {
var isDividible : Bool = ob % p == 0 ? true : false
if (!isDividible){
tmp.append(ob)
}
}
return tmp
}
var p : Int = 2
func getSimpleNumbersArrayFromArray (p : Int, arrPassed : [Int]) -> Array <Int>{
var tmp : [Int] = []
var newArr = arrayEcludingDivingByP(p: p, arrToCheck: arrPassed)
if (newArr.isEmpty){
// No more p availible, just return tmp
} else {
let newP = p + 1
getSimpleNumbersArrayFromArray(p: newP, arrPassed: newArr)
tmp = newArr
print("tmp array? \(tmp)")
}
return tmp
}
getSimpleNumbersArrayFromArray(p: p, arrPassed: simpleArr)
In console it prints:
tmp array? [1]
tmp array? [1, 2]
tmp array? [1, 2, 3]
tmp array? [1, 2, 3, 4]
tmp array? [1, 2, 3, 4, 5]
tmp array? [1, 2, 3, 4, 5, 6]
tmp array? [1, 2, 3, 4, 5, 6, 7]
tmp array? [1, 2, 3, 4, 5, 6, 7, 8]
tmp array? [1, 2, 3, 4, 5, 6, 7, 9, 10, 11]
tmp array? [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]
tmp array? [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17]
tmp array? [1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23]
tmp array? [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 31]
tmp array? [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47]
tmp array? [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97]
But it suppose to print final iteration with tmp filled with prime numbers. What did i wrong?
You are using newArr to represent the values in the sieve that remain. Your tmp array should be the primes found so far. Also you are using a recursive call which is confusing. I suggest simply grabbing the first value out of newArr which is a prime, add that prime to your list of primes, and then call arrayEcludingDividingByP to filter out the non-primes divisible by that prime. Repeat until newArr isEmpty which will happen when the conditional binding statement while let newP = newArr.first fails and the loop ends:
func getPrimes(arrPassed: [Int]) -> [Int] {
var primes: [Int] = []
var newArr = arrPassed
while let newP = newArr.first {
primes.append(newP)
newArr = arrayEcludingDivingByP(p: newP, arrToCheck: newArr)
}
return primes
}
print(getPrimes(arrPassed: Array(2...100)))
This can be further optimized because once newP * newP is greater than the largest number in newArr (newArr.last!) you are done and you can simply append newArr to primes and set newArr to [].
Also, arrayEcludingDivingByP(p: newP, arrToCheck: newArr) can be replaced by newArr.filter { $0 % newP != 0 }
I'm tyring to create a frequency counter. Basically, there's an array (testArray) with a bunch of numbers. I need to process those numbers and insert them in another array (probabilityArray). probabilityArray is also 2D array with the 1st row being the unique elements from the test array, the 2nd row being how many times a unique number occurs (Ex. probabilityArray[2][5] represents how often the number at probabilityArray[1][5] occurs). I'm having trouble exiting the rows for loop and I have no idea why.
#include <stdio.h>
#include <stdlib.h>
int histogram() {
}
int entropy() {
}
int main() {
int i, j, k, found = 0, currentPosition = 0, l = 0, x = 0, y = 0;
int testArray[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18 ,19, 20},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}};
int row = sizeof(testArray) / sizeof(testArray[0]);
int col = sizeof(testArray[0]) / sizeof(testArray[0][0]);
int elements = (row * col);
printf("Elements: %d\n", elements);
//printf("Rows: %d\nCols: %d\n", row, col);
int probabilityArray[3][elements];
for(x = 0; x < 3; x++) {
for(y = 0; y < elements; y++) {
//printf("X: %d\tY: %d\t", x, y);
probabilityArray[x][y] = 0;
}
//printf("\n");
}
//printf("Got here\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d\n", l);
l++;
int temp = testArray[i][j];
for (k = 0; k < currentPosition; k++) {
if (probabilityArray[1][k] == temp) {
//Element is not unique, increase occurance counter
printf("NOT UNIQUE: %d\n", temp);
probabilityArray[2][k]++;
found = 1;
break;
}
}
if (found == 0) {
//Element is unique, add it to array
printf("FOUND: %d\n", temp);
probabilityArray[1][currentPosition] = temp;
probabilityArray[2][currentPosition]++;
currentPosition++;
printf("Current Position: %d\n", currentPosition);
}
found = 0;
}
printf("I: %d\tC: %d\n", i, j);
}
for (i = 0; i < currentPosition; i++) {
probabilityArray[3][i] = (int)((probabilityArray[2][i] / elements) * 100);
}
for (i = 0; i < currentPosition; i++) {
printf("ELEMENT: %d\t\tFREQUENCY: %d\t\tPROBABILITY:%d\n", probabilityArray[1][i], probabilityArray[2][i], probabilityArray[3][i]);
}
}
Any help is appreciated!
You are using indices 1, 2, and 3, where you probably meant to use 0, 1, and 2.
Here:
if (probabilityArray[1][k] == temp) { // Should it be 0??
//Element is not unique, increase occurance counter
printf("NOT UNIQUE: %d\n", temp);
probabilityArray[2][k]++; // Should it be 1??
and here:
probabilityArray[1][currentPosition] = temp; // Should it be 0??
probabilityArray[2][currentPosition]++; // Should it be 1??
and here:
probabilityArray[3][i] = (int)((probabilityArray[2][i] / elements) * 100);
// Should it be 2 and 1 ??
and here:
printf("ELEMENT: %d\t\tFREQUENCY: %d\t\tPROBABILITY:%d\n",
probabilityArray[1][i], probabilityArray[2][i], probabilityArray[3][i]);
// Should be be 0, 1, and 2
Fixing those might fix your problems.
The following code have an array named ReadOnly[] which contains elements that are pointers to other arrays like AV_ReadOnly, BV_ReadOnly etc. Again AV_ReadOnly, BV_ReadOnly etc are pointer arrays containing elements that points to integer arrays.
The read_arrays() is a function used for printing a particular list/ accessing any particular value of the integer arrays. This approach works well on the test environment. But is there a chance for failure of this approach with a change in the platform/compiler?
#include<stdio.h>
int AV1_ReadOnly[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int AV2_ReadOnly[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int BV1_ReadOnly[] = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int BV2_ReadOnly[] = { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
int MV1_ReadOnly[] = { 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
int MV2_ReadOnly[] = { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60};
int NC1_ReadOnly[] = { 61, 62, 63, 64, 65, 66, 67, 68, 69, 70};
int NC2_ReadOnly[] = { 71, 72, 73, 74, 75, 76, 77, 78, 79, 80};
int * AV_ReadOnly[] =
{
AV1_ReadOnly,
AV2_ReadOnly,
};
int * BV_ReadOnly[] =
{
BV1_ReadOnly,
BV2_ReadOnly,
};
int * MV_ReadOnly[] =
{
MV1_ReadOnly,
MV2_ReadOnly,
};
int * NC_ReadOnly[] =
{
NC1_ReadOnly,
NC2_ReadOnly
};
int ** ReadOnly[] =
{
AV_ReadOnly,
BV_ReadOnly,
MV_ReadOnly,
NC_ReadOnly
};
void read_arrays( int obj, int inst )
{
int ** ArrayPtr = ReadOnly[obj];
int count =0;
while( count <8 )
{
printf( "\n %d", *(ArrayPtr[inst]+count) );
count++;
}
}
void main()
{
read_arrays( 1,1 );
}
Should be OK as long as you keep the int arrays and the int* arrays in the same file.
Also, you can (should) declare them const if you have no intentions to change them.
Also, you can (should) declare them static if you have no intentions to extern them in other files.
BTW, from what you've published, it looks like you can simply use int table[8][10] instead...