Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include "time.h"
- float func(float **a,int k,int m)
- {
- int j,s=0;
- float b=1;
- for(j=0;j<m;j++)
- {
- if(a[k][j]>=-1.5&&a[k][j]<=-1||a[k][j]>=1&&a[k][j]<=1.5)
- {
- b=b*a[k][j]*a[k][j];
- s=s+1;
- }
- }
- if(s==0) b=0;
- return b;
- }
- void main()
- {
- srand(time(0));
- FILE *f_in=NULL;
- FILE *f_out=NULL;
- f_in=fopen("in.txt","r");
- f_out=fopen("out.txt","w");
- int m,n,i,k,j,key,ans;
- float res;
- printf("Choose input. From console-1, from file-2\n");
- scanf("%d",&i);
- if(i==1)
- {
- printf("Enter amount of lines and columns\nLines:");
- scanf("%d",&n);
- printf("Columns:");
- scanf("%d",&m);
- printf("\n");
- }
- if(i==2)
- {
- if(f_in!=NULL)
- {
- fscanf(f_in,"%d %d",&n,&m);
- }
- }
- float** a=(float**) malloc(n*sizeof(float*));
- for(k=0;k<n;k++)
- {
- a[k]=(float*)malloc(m*sizeof(float));
- }
- if(a==NULL) exit(2);
- if(i==1)
- {
- for(k=0;k<n;k++)
- {
- for(j=0;j<m;j++)
- {
- a[k][j]=(0.1)*(-15+rand()%31);
- printf("%f ",a[k][j]);
- }
- printf("\n");
- }
- printf("\n");
- }
- if(i==2)
- {
- for(k=0;k<n;k++)
- {
- for(j=0;j<m;j++)
- {
- fscanf(f_in,"%f",&a[k][j]);
- }
- }
- close(f_in);
- }
- printf("Choose output. To console-1, to file-2\n");
- scanf("%d",&key);
- printf("\n");
- if(key==1)
- {
- for(k=0;k<n;k++)
- {
- res=func(a,k,m);
- printf("b[%d] = %f\n",k+1,res);
- }
- }
- if(key==2)
- {
- if(f_out!=NULL)
- {
- for(k=0;k<n;k++)
- {
- res=func(a,k,m);
- fprintf(f_out,"b[%d] = %f\n",k+1,res);
- }
- close(f_out);
- }
- }
- for(k=0;k<n;k++)
- {
- free(a[k]);
- }
- free(a);
- if(i==1){
- printf("\nDo you want to continue work? Yes-1,No-0\n");
- scanf("%d",&ans);
- if(ans==1) main();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement