Storing large amount of CSV values in a matrix in C - c

I am trying to store a CSV file that has 430469 rows (excluding the header) and 41 columns(excluding 2 columns of at the beginning) in a matrix in C. For that, I have declared a matrix dynamically.
The data is like this,
Where the first row contains the header. The first 2 columns contains the latitude and longitude respectively and the later rows contains the temperature recorded at those latitude and longitude in that year.
For reading the data and storing them in the matrix I have used this code snippet
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
//counting the number of rows and columns
FILE *mf=fopen("tdata.csv", "r");
if(mf==NULL)
{
perror("Unable to open the file");
exit(1);
}
char line[2000000];
int r=0, c;
while(fgets(line,sizeof(line), mf))
{
char *token;
c=0;
token=strtok(line, ",");
while(token!=NULL)
{
c++;
token= strtok(NULL, ",");
}
r++;
}
r=r-1; //since first row is header
c=c-2; //since first 2 columns are latitude and longitude that we dont need
printf("The number of row and column is is %d %d \n", r, c);
//declaring the matrix of size m[r][c]
double **mat = (double **)malloc(r * sizeof(double *));
for (int i=0; i<r; i++)
mat[i] = (double *)malloc(c * sizeof(double));
// Storing the values in the matrix
FILE *mf1=fopen("test.csv", "r");
if(mf1==NULL)
{
perror("Unable to open the file");
exit(1);
}
char line1[2000];
int i=0,j=0;
while(fgets(line1,sizeof(line1), mf1))
{
char *token1;
char *ptr;
double ret;
j=0;
token1=strtok(line1, ",");
while(token1!=NULL)
{
if(i>0 && j>1)
{
double d;
sscanf(token1, "%lf", &d);
mat[i-1][j-2] = d;
}
token1= strtok(NULL, ",");
j++;
}
i++;
}
//Printing the matrix
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("%lf ", mat[i][j]);
}
printf("\n");
}
//printf("%lf ", mat[23][0]);
//finding the minimum in every year.
//To find the minimum of every year, we must at first declare an array having the size as total number of columns
double* arr;
arr = (double*)malloc(c * sizeof(double));
double min;
for (int j = 0; j < c; j++)
{
min=1000;
for (int i = 0; i < r; i++)
{
if(mat[i][j]<min)
min=mat[i][j];
}
arr[j]=min;
}
printf("%lf \n", arr[0]);
}
But when I print the matrix to my surprise, I see that the first few rows are partially filled and after 10th row all values are 0. like this
-5.670000 -8.260000 -10.700000 -14.130000 -5.150000 -9.850000 -13.160000 -6.830000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-14.230000 -9.640000 -12.350000 -16.820000 -10.760000 -17.540000 -24.110000 -16.380000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-0.870000 -0.770000 -4.720000 -8.000000 -1.220000 -2.900000 -5.060000 -0.110000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-25.190000 -21.960000 -20.880000 -19.400000 -34.940000 -28.290000 -22.710000 -26.630000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-0.940000 0.320000 1.570000 -3.560000 0.210000 0.340000 -3.770000 -2.640000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-19.100000 -19.000000 -14.250000 -21.250000 -18.250000 -17.890000 -19.900000 -16.640000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-2.380000 -1.880000 -2.930000 -6.430000 -5.340000 -5.200000 -10.960000 -7.860000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4.120000 6.810000 4.890000 3.110000 5.630000 4.180000 5.350000 5.750000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-21.450000 -22.410000 -23.990000 -14.130000 -24.540000 -22.970000 -24.410000 -25.730000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
-7.940000 -5.170000 -9.780000 -7.790000 -6.750000 -3.810000 -5.640000 -3.090000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Can someone help me and tell me what is wrong with my code that I am getting this behavior? Thank you.

Related

How to convert pandas dataframe into a transaction matrix

I want to convert my pandas dataframe into a markov chain transaction matrix
import pandas as pd
dict1={'state_num_x': {0: 0, 1: 1, 2: 1,3: 1,4: 2,5: 2,6: 2,7: 3,8: 3,9: 4,10: 5,11: 5,
12: 5,13: 5,14: 5,15: 5,16: 6,17: 6,18: 6,19: 7,20: 7,21: 7},
'state_num_y': {0: 1,1: 1,2: 2,3: 5,4: 1,5: 4,6: 6,7: 1,8: 6,9: 1,10: 1,11: 2,
12: 3,13: 5,14: 6,15: 7,16: 1,17: 2,18: 5,19: 1,20: 4,21: 6},
'Sum_Prob': {0: 0.9999999999999999,1: 0.0369363131137667,2: 0.7408182206817178,
3: 0.22224546620451535,4: 0.0369363131137667,5: 0.7408182206817178,
6: 0.22224546620451535,7: 0.17028359283647593,8: 0.8297164071635239,
9: 0.9999999999999999,10: 0.003599493183089517,11: 0.08889818648180613,
12: 0.13334727972270924,13: 0.021335564755633474,14: 0.012001255175043838,
15: 0.7408182206817178,16: 0.015600748358133354,17: 0.8297164071635239,
18: 0.1546828444783427,19: 0.015600748358133354,20: 0.8297164071635239,21: 0.1546828444783427}}
df=pd.DataFrame.from_dict(dict1)
It looks like
state_num_x state_num_y Sum_Prob
0 1 1.000000
1 1 0.036936
1 2 0.740818
. . .
. . .
7 1 0.015601
7 4 0.829716
7 6 0.154683
let's called the result array arr_tx
arr_tx[0][1] should be equal to 1
arr_tx[1][1] should be equal to 0.036936
arr_tx[1][2] should be equal to 0.740818
it should be an 8x8 matrix and missing values should equal to zero.
So final result should look like
0,1,0,0,0,0,0,0,
0,0.036936,0.740818,0,0,0.222245,0,0
.,.,.,.,.,.,.,.
It looks like you want a pivot_table:
df.pivot_table(index='state_num_x', columns='state_num_y',
values='Sum_Prob', fill_value=0)
output:
state_num_y 1 2 3 4 5 6 7
state_num_x
0 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
1 0.036936 0.740818 0.000000 0.000000 0.222245 0.000000 0.000000
2 0.036936 0.000000 0.000000 0.740818 0.000000 0.222245 0.000000
3 0.170284 0.000000 0.000000 0.000000 0.000000 0.829716 0.000000
4 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5 0.003599 0.088898 0.133347 0.000000 0.021336 0.012001 0.740818
6 0.015601 0.829716 0.000000 0.000000 0.154683 0.000000 0.000000
7 0.015601 0.000000 0.000000 0.829716 0.000000 0.154683 0.000000

How to return the third largest number in an array

My code logic to answer a programming question is:
Find the biggest number in the input array.
Store that number in a new array.
Delete that number from the input array.
Repeat #'s 1-3 until I have three elements in the new array.
Choose the last element of the array to return.
My code is returning three 10's, instead of the three largest elements in the array, 10, 8, and 4. I think it might be because once the inner while loop is done, the code cannot return back to it?
My test code is:
puts(third_greatest([8, 1, 10, 4])).to_s
My code is:
def third_greatest(nums)
greatest_number = nil
three_greatest = []
three_greatest_idx = 0
while three_greatest_idx < 3
number_idx = 0
while number_idx < nums.length
current_number = nums[number_idx]
if greatest_number == nil
greatest_number = current_number
elsif greatest_number < current_number
greatest_number = current_number
end
number_idx += 1
end
three_greatest.unshift(greatest_number)
nums.delete(greatest_number)
three_greatest_idx += 1
end
return three_greatest
end
Once you start to think about solving problems like this the Ruby way, by which I mean leaning more heavily on Enumerable and expressing your intent as a series of simple operations, often chained together, then the solutions become easier to find.
For example, to find the three highest numbers in an arbitrary Array the obvious solution might be this:
def three_greatest(list)
list.sort.reverse.first(3)
end
That sorts the list, which by default is lowest to highest, and then reverses it, making it highest to lowest. The last operation is to copy out the first three entires. This seems pretty reasonable, as it expresses your intent quite clearly and works quite well.
The thing is if you look more closely at the Enumerable offerings there's an even easier solution using max:
def three_greatest(list)
list.max(3)
end
The lesson to learn here is that the Enumerable library, not unlike a mechanic's tool-chest, has a very large number of useful tools. It's important to take some time to at least read through what's in there so you don't end up wasting time re-inventing things that already exist in an elegant form.
In other words, when tackling a problem, check to see if that problems already been solved. In many cases you'll find there's a tool that does exactly what you're looking for.
Just to help people understand the difference in performance between max, sort and not using built-in methods:
require 'fruity'
ary = (1..100).to_a.shuffle
def use_max(a)
a.max(3).last
end
def use_sort(a)
a.sort[-3]
end
def nth_greatest(nums, n)
nums = nums.dup # prevent mutating the original array
result = nil
n.times do
idx, max = -1, -Float::INFINITY
nums.length.times do |i|
idx, max = [i - 1, nums[i - 1]] if nums[i - 1] > max
end
result = nums.delete_at idx
end
result
end
compare do
sorted { use_sort(ary) }
maxed { use_max(ary) }
nth_greatested { nth_greatest(ary, 3) }
end
# >> Running each test 512 times. Test will take about 1 second.
# >> sorted is faster than maxed by 2x ± 0.1
# >> maxed is faster than nth_greatested by 3x ± 0.1
Increasing the size of the array:
ary = (1..1_000).to_a.shuffle
results in:
# >> Running each test 64 times. Test will take about 1 second.
# >> maxed is faster than sorted by 80.0% ± 10.0%
# >> sorted is faster than nth_greatested by 3x ± 0.1
And bumping up the array size again:
ary = (1..10_000).to_a.shuffle
results in:
# >> Running each test 8 times. Test will take about 1 second.
# >> maxed is faster than sorted by 3x ± 0.1
# >> sorted is faster than nth_greatested by 2x ± 0.1
The documentation doesn't mention if max(3) returns a reversed sorted array, even though it looks like it.
The documentation example is:
a.max(2) #=> ["horse", "dog"]
which is descending, but that's not a good example as it's easier to see using numbers:
ary.max(3) # => [100, 99, 98]
Here are some results using Benchmark to show the baseline speeds:
require 'benchmark'
ary = (1..5).to_a.shuffle
10.times do
Benchmark.bm(4) do |b|
b.report('sort') { ary.sort[-3] }
b.report('max') { ary.max(3).last }
end
end
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000010)
# >> max 0.000000 0.000000 0.000000 ( 0.000006)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000004)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000004)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000003)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000004)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000004)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000005)
# >> max 0.000000 0.000000 0.000000 ( 0.000005)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000004)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000003)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000003)
# >> max 0.000000 0.000000 0.000000 ( 0.000003)
And increasing the size of the array:
ary = (1..100).to_a.shuffle
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000020)
# >> max 0.000000 0.000000 0.000000 ( 0.000013)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000013)
# >> max 0.000000 0.000000 0.000000 ( 0.000011)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000010)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000009)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000009)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000008)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000008)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000008)
# >> max 0.000000 0.000000 0.000000 ( 0.000013)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000011)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000008)
# >> max 0.000000 0.000000 0.000000 ( 0.000010)
And:
ary = (1..1_000).to_a.shuffle
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000110)
# >> max 0.000000 0.000000 0.000000 ( 0.000057)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000103)
# >> max 0.000000 0.000000 0.000000 ( 0.000054)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000101)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000100)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000100)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000100)
# >> max 0.000000 0.000000 0.000000 ( 0.000056)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000099)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000099)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000100)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
# >> user system total real
# >> sort 0.000000 0.000000 0.000000 ( 0.000099)
# >> max 0.000000 0.000000 0.000000 ( 0.000053)
You forgot to unset greatest_number
nums.delete(greatest_number)
three_greatest_idx += 1
greatest_number = nil # this line
end
Obviously there is a very simple solution for this in Ruby but I took for granted that you wanted to do it on your own.
I assume this is a task where heavy usage of Enumerable module might be not permitted. There are still easier approaches all around. E.g. you do not need to maintain another array: just remove the highest from the original array N times:
def nth_greatest(nums, n)
nums = nums.dup # prevent mutating the original array
result = nil
n.times do
idx, max = -1, -Float::INFINITY
nums.length.times do |i|
idx, max = [i - 1, nums[i - 1]] if nums[i - 1] > max
end
result = nums.delete_at idx
end
result
end
nth_greatest [8, 1, 10, 4], 2
#⇒ 8
nth_greatest [8, 1, 10, 4], 3
#⇒ 4
Sort it and use Ruby's negative index option for arrays.
[8, 1, 10, 4].sort[-3]
>> 4
Edit: After many tests I have determined that the sort method shown above is faster than the method below if, when searching for the nth biggest number in an array of m elements, n > 1/20th the size of m. Since in your example we are looking for the 3rd biggest number in an array of 4 elements, and 3 is much bigger than .2, the answer above is significantly more efficient than this alternative method:
[8, 1, 10, 4].max(3)[-1]
>> 4

sbrk system call in unix

I studied like malloc uses the sbrk system call. But, some one says, the sbrk is
deprecated one. Now a days malloc using the mmap2 system call to allocate
memory. So, Is there any commands like (ls,cat, grep, sed) using the sbrk
system call. For Ex:
mohanraj#ltsp63:~/Development/chap8$ strace -c ls
a.out files flush.c fopen.c ld.c lld.c malloc.c opendir1.c t2.c t3.c t.c test.c
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
-nan 0.000000 0 12 read
-nan 0.000000 0 1 write
-nan 0.000000 0 13 open
-nan 0.000000 0 16 close
-nan 0.000000 0 1 execve
-nan 0.000000 0 1 time
-nan 0.000000 0 9 8 access
-nan 0.000000 0 3 brk
-nan 0.000000 0 3 ioctl
-nan 0.000000 0 1 readlink
-nan 0.000000 0 5 munmap
-nan 0.000000 0 1 uname
-nan 0.000000 0 11 mprotect
-nan 0.000000 0 1 _llseek
-nan 0.000000 0 1 getsid
-nan 0.000000 0 2 rt_sigaction
-nan 0.000000 0 1 rt_sigprocmask
-nan 0.000000 0 1 getcwd
-nan 0.000000 0 1 getrlimit
-nan 0.000000 0 28 mmap2
-nan 0.000000 0 1 stat64
-nan 0.000000 0 16 fstat64
-nan 0.000000 0 1 getuid32
-nan 0.000000 0 2 getdents64
-nan 0.000000 0 1 1 futex
-nan 0.000000 0 1 set_thread_area
-nan 0.000000 0 1 set_tid_address
-nan 0.000000 0 1 statfs64
-nan 0.000000 0 1 openat
-nan 0.000000 0 1 set_robust_list
-nan 0.000000 0 1 socket
-nan 0.000000 0 1 connect
-nan 0.000000 0 1 send
------ ----------- ----------- --------- --------- ----------------
100.00 0.000000 141 9 total
mohanraj#ltsp63:~/Development/chap8$
The above output shows that the ls command using the above syscall to
execute the output. Likewise any command using the sbrk system call, Is there
any what is that?
Thanks in Advance.
sbrk is not a system call in linux. It's a library function implemented in libc which uses the brk system call. Your strace shows brk being used.
If in libc a malloc implementation is redirecited to mmap instead of sbrk. Then every call to malloc will result in mmap. And you can find sbrk only if it is explicitly used in user level application (normally malloc is used)

Issues on DCT transform: results are incorrect

I am implementing DCT transform using this formula:
But the results are incorrect. For some 8 by 8 matrix,
0 0 0 0 0 0 0 0
210 210 210 210 210 210 210 210
255 255 255 255 255 255 255 255
210 210 210 210 210 210 210 210
0 0 0 0 0 0 0 0
210 210 210 210 210 210 210 210
255 255 255 255 255 255 255 255
210 210 210 210 210 210 210 210
The results I got after passing the data to dct transform function are:
1350.000000 0.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000
-250.897627 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
-0.000000 0.000000 0.000000 -0.000000 0.000000 -0.000000 0.000000 -0.000000
-461.931139 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
-510.000000 0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
156.770200 0.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000
-0.000000 -0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000 -0.000000
-260.946562 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
(Only 1st column has non-zero values)
The problem is that I was told that the correct results should be only non-zero values at upper left corner of the matrix. And I am not sure where might be wrong in my code. Can anyone help me? Thanks.
Here is my DCT code:
static double C(int val){
if(val == 0)
return 1.0 / sqrt(2.0);
else
return 1.0;
}
void dctTransform(int matrix[8][8], double dctMatrix[8][8]){
int u, v, x, y;
double temp;
for(u=0; u<8; u++)
for(v=0; v<8; v++){
temp = 0;
dctMatrix[u][v] = 0;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
temp += matrix[y][x]*cos(((2*x+1)*u*M_PI) / 16)*cos(((2*y+1)*v*M_PI) / 16);
}
}
dctMatrix[u][v] = C(u) * C(v) * 0.25 * temp;
}
}
Aren't you supposed to first change the input values from 0..255 to -128..127? Haven't you also switched x and y in matrix[y][x]?
If I subtract 128 from the input values to center them around zero, and then switch x and y, your code at least gives me the correct values according to the example in Wikipedia.

what could cause glEnable(GL_LIGHT0) crash glDrawElements?

I passed 3 array pointers to opengl with:
glColorPointer()
glNormalPointer()
glVertexPointer()
everthing works fine,but when I try to enable light by glEnable(GL_LIGHT0);
glDrawElements just crashed,I assume there is something wrong with normal? my normal comes from this file:
1.000000 1.000000 -1.000000 0.000000 0.000000 -1.000000
1.000000 -1.000000 -1.000000 0.000000 0.000000 -1.000000
-1.000000 -1.000000 -1.000000 0.000000 0.000000 -1.000000
1.000000 0.999999 1.000000 -0.000000 0.000000 1.000000
-1.000000 1.000000 1.000000 -0.000000 0.000000 1.000000
-1.000000 -1.000000 1.000000 -0.000000 0.000000 1.000000
1.000000 1.000000 -1.000000 1.000000 -0.000001 -0.000000
1.000000 0.999999 1.000000 1.000000 -0.000001 -0.000000
0.999999 -1.000001 1.000000 1.000000 -0.000001 -0.000000
1.000000 -1.000000 -1.000000 -0.000000 -1.000000 -0.000000
0.999999 -1.000001 1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 -1.000000 -1.000000 0.000000 -0.000000
-1.000000 -1.000000 1.000000 -1.000000 0.000000 -0.000000
-1.000000 1.000000 1.000000 -1.000000 0.000000 -0.000000
1.000000 0.999999 1.000000 0.000000 1.000000 0.000000
1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
-1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
-1.000000 1.000000 -1.000000 0.000000 0.000000 -1.000000
0.999999 -1.000001 1.000000 -0.000000 0.000000 1.000000
1.000000 -1.000000 -1.000000 1.000000 0.000000 0.000000
1.000000 1.000000 -1.000000 1.000000 0.000000 0.000000
0.999999 -1.000001 1.000000 1.000000 0.000000 0.000000
-1.000000 -1.000000 -1.000000 -0.000000 -1.000000 0.000000
-1.000000 1.000000 -1.000000 -1.000000 0.000000 -0.000000
-1.000000 1.000000 1.000000 0.000000 1.000000 0.000000
first 3 is vertex position,and last 3 is normal,I have questions:
does the normal changes when I translate or rotate or scale the mesh?
what could cause glDrawElements crash by enable lighting?
3.since my normals come from file,and its already normalized do I have to call glEnable(GL_NORMALIZE) ?
You probably passed invalid arguments to glNormalPointer causing an access violation when glDrawElements tries to access your memory. The old fixed function per-vertex lighting code only makes use of normals when lighting is enabled.
Regarding your other questions:
You have to rotate your normals when rotating your mesh. Translations and scaling does not affect normals. This is automatically done by the fixed function pipeline (which I suppose you're using) and only has to be done manually when writing a custom vertex shader or when rotating the mesh on the CPU. glEnable(GL_NORMALIZE) is only needed when you scale your mesh, otherwise you can discard this function.

Resources