Sunday, February 1, 2015
Format
Kode C berikut adalah cara untuk mengkonversi format matrix. Malloc digunakan sehingga kode ini mampu menghandle data yang sangat besar.
Berikut adalah Kode format yang digunakan dalam C:
Code Format
---- ------
%c character
%d signed integers
%i signed integers
%e scientific notation, with a lowercase "e"
%E scientific notation, with a uppercase "E"
%f floating point
%lf double
%g use %e or %f, whichever is shorter
%G use %E or %f, whichever is shorter
%o octal
%s a string of characters
%u unsigned integer
%x unsigned hexadecimal, with lowercase letters
%X unsigned hexadecimal, with uppercase letters
%p a pointer
%% a '%' sign
Untuk contoh ini, saya memiliki filein dengan format scientific:
1.7159866e-001 5.0353191e-002 8.8042207e-002
5.8189807e-001 2.3275270e-001 6.8729395e-001
Dan Mengoutputkannya dengan floating point:
0.171599 0.050353 0.088042
0.581898 0.232753 0.687294
#include <stdio.h>
#include <stdlib.h>
float **matrixA;
int rowA,columnA,row,column;
void readA()
{
FILE *inputA=fopen("filein","r");
for(row=0;row<rowA;row++)
{
for(column=0;column<columnA;column++)
{
fscanf(inputA,"%e",&matrixA[row][column]);
}
}
fclose(inputA);
}
//Main
int main()
{
//Define row and col
rowA=2;columnA=3;
matrixA=(float **)malloc(rowA*sizeof(float));
for(row=0;row<rowA;row++)
matrixA[row]=(float *)malloc(columnA*sizeof(float));
readA();
int i,j;
for(i=0;i<rowA;++i)
{
for(j=0;j<columnA;++j)
{
printf("%f ",matrixA[i][j]);
}
printf("\n");
}
for(row=0;row<rowA;row++)
free(matrixA[row]);
free(matrixA);
}
Berikut adalah Kode format yang digunakan dalam C:
Code Format
---- ------
%c character
%d signed integers
%i signed integers
%e scientific notation, with a lowercase "e"
%E scientific notation, with a uppercase "E"
%f floating point
%lf double
%g use %e or %f, whichever is shorter
%G use %E or %f, whichever is shorter
%o octal
%s a string of characters
%u unsigned integer
%x unsigned hexadecimal, with lowercase letters
%X unsigned hexadecimal, with uppercase letters
%p a pointer
%% a '%' sign
Untuk contoh ini, saya memiliki filein dengan format scientific:
1.7159866e-001 5.0353191e-002 8.8042207e-002
5.8189807e-001 2.3275270e-001 6.8729395e-001
Dan Mengoutputkannya dengan floating point:
0.171599 0.050353 0.088042
0.581898 0.232753 0.687294
#include <stdio.h>
#include <stdlib.h>
float **matrixA;
int rowA,columnA,row,column;
void readA()
{
FILE *inputA=fopen("filein","r");
for(row=0;row<rowA;row++)
{
for(column=0;column<columnA;column++)
{
fscanf(inputA,"%e",&matrixA[row][column]);
}
}
fclose(inputA);
}
//Main
int main()
{
//Define row and col
rowA=2;columnA=3;
matrixA=(float **)malloc(rowA*sizeof(float));
for(row=0;row<rowA;row++)
matrixA[row]=(float *)malloc(columnA*sizeof(float));
readA();
int i,j;
for(i=0;i<rowA;++i)
{
for(j=0;j<columnA;++j)
{
printf("%f ",matrixA[i][j]);
}
printf("\n");
}
for(row=0;row<rowA;row++)
free(matrixA[row]);
free(matrixA);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment