reading and printing a .csv file like a 2D matrix with both integer and float values in c - c

Reading a file in c with .csv as extension. The file consisting of both integer and float type data values. Is there any way to read the csv file. Any help is appreciated.
The data is as follows:
Application_No. Actual_Effort (in PM) No of Processes No of Tasks No of partnerLinks Task Variables Element Variables Event Variables Script Developer's Skills Developer's Confidence TPSS TS TCC
1 918.28 1 3 5 33 7 2 3 3.5 1 8 135 143
2 8891.513 3 9 3 100 15 6 12 3 1 36 1197 1233
3 22479.261 5 15 23 125 25 10 20 3 1 190 2700 2890
4 2961.131 2 4 9 70 13 4 17 2 0 72 416 488
5 19650.198 7 14 19 130 28 12 5 2.5 0 231 2450 2681
6 377.75 1 2 4 22 8 2 2 3 1 6 68 74
7 2671.93 1 5 12 55 12 6 4 2 0 17 385 402
8 966.15 3 3 6 31 8 5 7 2.5 0 27 153 180
9 3765.81 2 6 17 73 14 2 3 3.5 1 46 552 590
10 7467.11 4 8 21 87 19 13 1 2 0 116 960 1076

Related

A problem with fscanf() in c , overwriting other variables

I'm traying to read from a file two things :
1- a list of integers(on the top of the file) in to an array of integers inputs[]
2- 3 matrixes 5x5 etch into an array of array of array boards[][][]
the firs part works properly but when the 2 part finished some how the inputs[] change values
The code :
void loadfile(const char* filepath,int *inputs ,int boards [nboards][size][size]){
FILE *inp=fopen(filepath,"r");
//loading the marked numbers
for(int i=0;i<4;i++){
fscanf(inp,"%d",inputs+i);
//to show that inputs reseve the the right values at the start
printf("%d ",*(inputs+i));
fseek(inp,ftell(inp)+1,SEEK_SET);
}
printf("\n");
//loaing the boards
for(int n=1;n<=nboards;n++){
for(int i=0;i<size;i++)
for(int j=0;j<size;j++){
fscanf(inp,"%d",&boards[n][i][j]);
printf("%d ",boards[n][i][j]);
}
printf("\n");
}
fclose(inp);
}
int main(){
int inputs[4]={};
int boards[nboards][size][size]={};
loadfile("inputs.txt",inputs,boards);
for(int i=0;i<4;i++)
printf("%d ",*(inputs+i));
}
The file :
7,4,9,5
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7
The output:
7 4 9 5
22 13 17 11 0 8 2 23 4 24 21 9 14 16 7 6 10 3 18 5 1 12 20 15 19
3 15 0 2 22 9 18 13 17 5 19 8 7 25 23 20 11 10 24 4 14 21 16 12 6
14 21 17 24 4 10 16 15 9 19 18 8 23 26 20 22 11 13 6 5 2 0 12 3 7
21 17 24 4

How to control the format to export discriminant analysis results to word by using asdoc in Stata?

I want to export the results of a discriminant analysis by using asdoc in stata.
I want to show as follows.
three Three Decimal Digits.
compress the table to fit into a page of the word.
However, the format of the results is horrible. I use dec(3) but not working. I read the "help asdoc" in Stata, but it all about regression.
Does anyone know how to export the total results of discriminant analysis to Word with nice format?
Thank you in advance.
The following is the sample code and asdoc code that I use.
input area Revenue age child_number grocery_expense credit_card exercise_week social_week
1 99336 76 1 22453 5 3 1
1 59092 75 4 16995 6 1 3
1 68614 49 0 37709 0 7 5
1 84805 55 3 21642 0 3 1
1 66138 41 3 10490 2 4 7
1 90238 43 2 30254 5 6 4
1 60466 49 2 18136 1 0 4
1 46575 64 0 25053 6 6 7
2 97811 40 4 36925 4 6 5
2 61862 40 0 14480 5 5 6
2 58071 73 0 24754 4 0 1
2 42539 66 2 19903 3 1 6
2 62074 56 3 12560 3 3 7
2 71619 34 2 24523 6 3 6
2 51281 74 2 23625 4 6 3
3 40990 25 3 38943 4 7 4
3 44567 73 2 39898 1 4 7
3 73586 42 2 20159 0 2 3
3 44907 44 3 31378 1 1 6
3 79352 20 3 39968 6 6 1
3 55647 50 1 27122 0 3 6
3 80943 43 1 15177 2 7 4
3 88892 77 2 22537 4 2 7
4 91735 74 3 27505 0 5 2
4 61224 60 5 12374 5 1 0
4 72192 68 4 36817 2 6 1
4 87486 59 0 34846 6 5 1
4 53131 52 4 12584 5 1 1
4 49083 33 5 30652 3 0 5
4 47408 49 0 28938 1 6 0
4 74647 52 2 15291 0 5 6
5 81643 37 0 37993 2 4 2
5 42371 46 1 33436 6 5 4
5 74074 24 3 16618 5 6 7
5 63502 34 3 19887 1 4 3
5 86779 31 5 37290 6 3 4
5 45842 45 5 20383 2 1 5
5 59835 42 5 30708 4 2 1
5 60486 38 2 36167 3 6 2
5 49099 58 0 13157 4 3 7
5 71692 37 5 36317 4 6 3
5 91406 45 5 12451 5 7 1
6 42742 48 1 39088 5 2 0
6 54538 21 2 19657 0 7 3
6 49323 69 4 37173 5 5 5
6 50053 54 4 32193 2 7 7
6 99139 48 1 14647 4 4 1
6 97908 26 0 14319 6 1 4
6 46504 27 1 39478 4 6 2
6 92330 28 3 23676 1 3 0
6 93926 34 3 10871 1 3 3
6 81890 51 2 16914 1 0 1
6 86679 79 1 35967 2 7 6
6 43783 67 2 31009 2 5 0
6 76770 66 5 13220 6 6 7
6 91160 67 2 29346 6 0 3
end
asdoc candisc Revenue age child_number grocery_expense credit_card exercise_week social_week , group (area) dec(3)

Segmentation fault : Reading values from a text file

In my code I am trying to read values from a .txt file so as to build my adjacency matrix but it keeps on returning a segmentation fault. I don't seem to be able to point out where I am going wrong. Please help.
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#define MAX_VERTICES 1024
int global_adj_matrix[MAX_VERTICES][MAX_VERTICES];
int **graph_tree;
int **node_data;
int global_weight;
int number_threads;
int max_nodes;
int random_node;
int max_weight;
int finish_flag;
void readAdjMatrix();
int main(int argc, char *argv[]){
for(int i = 0 ; i < MAX_VERTICES ; i++){
for(int j = 0 ; j < MAX_VERTICES ; j++){
global_adj_matrix[i][j] = 0;
}
}
number_threads = atoi(argv[1]);
max_nodes = 0;
readAdjMatrix();
}
void readAdjMatrix(){
int source, destination, edge_weight;
max_nodes = INT_MIN;
max_weight = INT_MIN;
FILE *file_pointer = fopen("graph.txt", "r");
while(!feof(file_pointer)){
fscanf(file_pointer, "%d", &source);
fscanf(file_pointer, "%d", &destination);
fscanf(file_pointer, "%d", &edge_weight);
global_adj_matrix[source][destination] = edge_weight;
global_adj_matrix[destination][source] = edge_weight;
if(edge_weight > max_weight)
max_weight = edge_weight;
if(destination > max_nodes)
max_nodes = destination;
}
printf("%d %d", max_weight, max_nodes);
for(int i = 0 ; i <= max_nodes ; i++){
for(int j = 0 ; j <= max_nodes ; j++){
printf("%d\t", global_adj_matrix[i][j]);
}
printf("\n");
}
fclose(file_pointer);
}
This is my .txt file
0 1 281
0 2 242
0 3 344
0 4 340
0 5 372
0 6 161
0 7 49
0 8 278
0 10 190
0 11 213
0 12 55
0 13 239
0 14 321
0 15 162
1 0 281
1 2 249
1 3 58
1 4 331
1 5 189
1 6 84
1 7 259
1 9 256
1 11 188
1 12 149
1 13 330
1 14 17
1 15 370
2 0 242
2 1 249
2 3 125
2 4 179
2 5 355
2 6 11
2 7 232
2 8 199
2 9 67
2 10 390
2 12 312
2 13 3
2 14 237
2 15 96
3 0 344
3 1 58
3 2 125
3 4 105
3 5 192
3 6 180
3 7 335
3 8 280
3 9 185
3 10 66
3 11 65
3 13 274
3 14 72
3 15 282
4 0 340
4 1 331
4 2 179
4 3 105
4 5 149
4 6 286
4 7 265
4 8 359
4 9 341
4 10 211
4 11 367
4 12 340
4 13 14
4 14 69
4 15 128
5 0 372
5 1 189
5 2 355
5 3 192
5 4 149
5 6 167
5 7 268
5 8 20
5 9 270
5 10 210
5 11 369
5 12 131
5 13 133
5 15 167
6 0 161
6 1 84
6 2 11
6 3 180
6 4 286
6 5 167
6 7 208
6 8 335
6 9 353
6 10 12
6 11 307
6 12 199
6 13 273
6 14 118
7 0 49
7 1 259
7 2 232
7 3 335
7 4 265
7 5 268
7 6 208
7 8 182
7 9 327
7 10 272
7 11 198
7 12 103
7 13 132
7 15 161
8 0 278
8 2 199
8 3 280
8 4 359
8 5 20
8 6 335
8 7 182
8 9 108
8 10 112
8 11 344
8 12 192
8 13 264
8 14 207
8 15 231
9 1 256
9 2 67
9 3 185
9 4 341
9 5 270
9 6 353
9 7 327
9 8 108
9 10 395
9 11 205
9 12 365
9 13 8
9 14 57
9 15 132
10 0 190
10 2 390
10 3 66
10 4 211
10 5 210
10 6 12
10 7 272
10 8 112
10 9 395
10 11 11
10 12 7
10 13 288
10 14 143
10 15 226
11 0 213
11 1 188
11 3 65
11 4 367
11 5 369
11 6 307
11 7 198
11 8 344
11 9 205
11 10 11
11 12 203
11 13 136
11 14 252
11 15 168
12 0 55
12 1 149
12 2 312
12 4 340
12 5 131
12 6 199
12 7 103
12 8 192
12 9 365
12 10 7
12 11 203
12 13 90
12 14 344
12 15 11
13 0 239
13 1 330
13 2 3
13 3 274
13 4 14
13 5 133
13 6 273
13 7 132
13 8 264
13 9 8
13 10 288
13 11 136
13 12 90
13 14 39
13 15 39
14 0 321
14 1 17
14 2 237
14 3 72
14 4 69
14 6 118
14 8 207
14 9 57
14 10 143
14 11 252
14 12 344
14 13 39
14 15 154
15 0 162
15 1 370
15 2 96
15 3 282
15 4 128
15 5 167
15 7 161
15 8 231
15 9 132
15 10 226
15 11 168
15 12 11
15 13 39
15 14 154
Your segmentation fault is because you're trying to read a nonexistent index in the argument vector of main. If you want to avoid that, you should rewrite it to match something like this:
int main (int argc, const char *argv[]) {
if (argc > 1 && (number_threads = atoi(argv[1]))) {
max_nodes = 0;
readAdjMatrix();
}
return 0;
}
This ensures that you have an argument to convert to begin with, and also that it is a nonzero number. I believe atoi has undefined behavior if it isn't a valid string though, so you should harden against that. You also do some other unnecessary things. For one, this block here:
for(int i = 0 ; i < MAX_VERTICES ; i++){
for(int j = 0 ; j < MAX_VERTICES ; j++){
global_adj_matrix[i][j] = 0;
}
}
is pointless because if you initialize a 2D array as an external/global variable then it is automatically zeroed upon initialization. Only local/automatic variables will be filled with garbage data. Therefore, you can omit it.
Finally, I would also change your while loop to look more or less like this (Credit: Chux for better loop guard).
while(fscanf(file_pointer, "%d %d %d", &source, &destination, &edge_weight) == 3) {
global_adj_matrix[source][destination] = global_adj_matrix[destination][source] = edge_weight;
if(edge_weight > max_weight)
max_weight = edge_weight;
if(destination > max_nodes)
max_nodes = destination;
}
This ensures you correctly scanned the amount of variables necessary per line. And the extended assignment just saves a bit of room.
Hope this fixed the problem you were having.
I am posting my answer by neglecting the bigger code which you have not mentioned in your question. I have trimmed down the unnecessary code. The code is given below.
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#define MAX_VERTICES 1024
int global_adj_matrix[MAX_VERTICES][MAX_VERTICES];
int global_weight,max_nodes,random_node,max_weight;
void readAdjMatrix();
int main()
{
int i,j;
max_nodes = 0;
readAdjMatrix();
return 0;
}
void readAdjMatrix()
{
int source, destination, edge_weight,i,j;
max_nodes = INT_MIN;
max_weight = INT_MIN;
FILE *file_pointer = fopen("graph.txt", "r");
while(!feof(file_pointer))
{
fscanf(file_pointer, "%d", &source);
fscanf(file_pointer, "%d", &destination);
fscanf(file_pointer, "%d", &edge_weight);
global_adj_matrix[source][destination] = global_adj_matrix[destination][source] =edge_weight;
if(destination > max_nodes)
max_nodes = destination;
}
printf( "%d\n", max_nodes);
for( i = 0 ; i <= max_nodes ; i++){
for( j = 0 ; j <= max_nodes ; j++){
printf("%d\t", global_adj_matrix[i][j]);
}
printf("\n");
}
fclose(file_pointer);
}
PS : Simply execute this code with ./a.out with no command line argument. In case you are using the command line argument (as given in your question), please use the following syntax to execute your code :
./a.out "your desired number which works with the bigger code"

Matlab: repeat and concatenate rows and cols into new array

I have two 4-by-4 arrays:
a1 = [ 1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16 ]
a2 = [ 17 18 19 20; 21 22 23 24; 25 26 27 28; 29 30 31 32 ]
I need to create 16-by-8 array C:
1 2 3 4 17 18 19 20
1 2 3 4 21 22 23 24
1 2 3 4 25 26 27 28
1 2 3 4 29 30 31 32
5 6 7 8 17 18 19 20
5 6 7 8 21 22 23 24
5 6 7 8 25 26 27 28
5 6 7 8 29 30 31 32
9 10 11 12 17 18 19 20
9 10 11 12 21 22 23 24
9 10 11 12 25 26 27 28
9 10 11 12 29 30 31 32
13 14 15 16 17 18 19 20
13 14 15 16 21 22 23 24
13 14 15 16 25 26 27 28
13 14 15 16 29 30 31 32
The left half (from 1st to 4th column) of the result array C should repeat 4 times i-th row of the array a1, the right half (from 5th to 8th column) should repeat 4 times the array a2.
Below is my code.
p=4
n=4
for i=1:n
b=n*i;
a=n*(i-1)+1;
for j=1:p
for k=a:b
C(k,j)=a1(i,j);
end;
end;
end;
for i=1:n
b=n*i;
a=n*(i-1)+1;
for j=p+1:2*p
l=1;
for k=a:b
C(k,j)=a2(l,j-p);
l=l+1;
end;
end;
end;
C;
size_C=size(C)
Question. Is it possible to create result array C without for-loop? Which functions can I use?
Yes it's possible.
One way of doing it is by using kron and repmat
C = [ kron(a1, ones(4,1)) repmat(a2, 4, 1)]
Perhaps the 4 should be derived from the size of one of the matrixes
You can use ndgrid to generate the row indices and then concatenate:
[ii, jj] = ndgrid(1:size(a2,1), 1:size(a1,1));
C = [a1(jj,:) a2(ii,:)];
With focus on performance, here's one using reshape and repmat -
% Store sizes
M = size(a1,1);
N = size(a2,1);
% Get the replicated version for a1 and a2 separately
parte1 = reshape(repmat(reshape(a1,1,M,[]),[N,1,1]),M*N,[])
parte2 = repmat(a2,[M,1]);
% Do columnar concatenatation for final output
out = [parte1 parte2]
Sample run on a generic case -
a1 = % 3 x 4 array
5 2 6 9
7 4 7 6
9 8 6 1
a2 = % 2 x 5 array
7 7 1 9 2
6 8 8 7 9
out =
5 2 6 9 7 7 1 9 2
5 2 6 9 6 8 8 7 9
7 4 7 6 7 7 1 9 2
7 4 7 6 6 8 8 7 9
9 8 6 1 7 7 1 9 2
9 8 6 1 6 8 8 7 9

SQL Query Help after 13th row Dynamic and in 12th row static values with a formula

I have below table structure with the similar to below output.
S_NO_ T_P O_P H_P L_P C_P SC_12
1 1 509.75 515 508 512.4500122
2 2 511.7000122 511.7000122 506.1499939 506.5499878
3 4 507.1499939 510.25 507.1499939 510.25
4 5 510 512.3499756 509.2999878 512.3499756
5 3 512.5 512.5 511.1499939 512
6 8 512.25 512.5 510.1000061 510.9500122
7 1 510.5499878 511.7999878 510 511.7999878
8 2 511.1000061 511.8500061 508.1499939 508.8999939
9 5 508.8999939 510 508.5 509.9500122
10 6 509.8999939 509.8999939 508.5 508.8500061
11 8 509.5 511.2000122 509 510.5
12 9 510.5 511.7999878 510.1000061 510.2000122 **510.4**
13 12 510.2999878 511.3500061 510.25 510.75 510.25
14 14 510.3500061 512 510.3500061 510.9500122 510.62
15 15 510.9500122 511.7999878 510.6000061 511.1000061 510.69
16 17 511.0499878 511.3500061 509.1000061 509.1000061 510.42
17 13 509.5 509.5 508.1000061 508.5 510.13
18 13 508.4500122 508.9500122 507 507 509.8
19 19 507 508.2000122 503.2999878 503.2999878 509.09
20 11 504 505 503.5 504.6499939 508.74
21 17 505.4500122 506.3500061 504 504.7000122 508.3
22 18 504.7000122 505.5 504.2000122 505.5 508.02
23 19 505.3500061 505.7000122 503.1000061 503.6499939 507.45
I want write a SQL Query to get results in C12WR column.
And I want on row of C12WR Column use a static value which is in “SC_12” (In the shown table the value is “510.4”) and in the SC_12 Column it should calculate the below formula after 13th row number and it continue it till the end of the table records
After 13th Row in C12WR Column = (the value of above row*11 + Current row value from C_P Column) /12
The final output should be similar to below
S_NO_ T_P O_P H_P L_P C_P SC_12 c12-WWR
1 1 509.75 515 508 512.4500122
2 2 511.7000122 511.7000122 506.1499939 506.5499878
3 4 507.1499939 510.25 507.1499939 510.25
4 5 510 512.3499756 509.2999878 512.3499756
5 3 512.5 512.5 511.1499939 512
6 8 512.25 512.5 510.1000061 510.9500122
7 1 510.5499878 511.7999878 510 511.7999878
8 2 511.1000061 511.8500061 508.1499939 508.8999939
9 5 508.8999939 510 508.5 509.9500122
10 6 509.8999939 509.8999939 508.5 508.8500061
11 8 509.5 511.2000122 509 510.5
12 9 510.5 511.7999878 510.1000061 510.2000122 510.4 510.4
13 12 510.2999878 511.3500061 510.25 510.75 510.25 510.3833344
14 14 510.3500061 512 510.3500061 510.9500122 510.62 510.4138898
15 15 510.9500122 511.7999878 510.6000061 511.1000061 510.69 510.4585667
16 17 511.0499878 511.3500061 509.1000061 509.1000061 510.42 510.51202
17 13 509.5 509.5 508.1000061 508.5 510.13 510.3943521
18 18 508.4500122 508.9500122 507 507 509.8 510.2364895
19 19 507 508.2000122 503.2999878 503.2999878 509.09 509.966782
20 11 504 505 503.5 504.6499939 508.74 509.4112158
21 17 505.4500122 506.3500061 504 504.7000122 508.3 509.0144473
22 18 504.7000122 505.5 504.2000122 505.5 508.02 508.6549111
23 19 505.3500061 505.7000122 503.1000061 503.6499939 507.45 508.3920018
I don't know what you mean by a static value which is in “SC_12” But if you have that value in the table then use this:
select T_P, O_P, H_P, L_P, C_P, SC_12
case when t1.S_NO_ > 12
then cast ((t2.SC_12 * 11 + t1.C_P)/12 as varchar(50))
else ''
end as [c12-WWR]
from t as t1
inner join t as t2 on t2.S_NO_ = t1.S_NO_ -1
order by t1.S_NO_

Resources