#include <iostream> #include<stdio.h> #include<stdlib.h> void matrica(int *a, int *b, int *c, int n, int &k) { for(int i = 0;i < n;i++) if(a[i] != 0){ b[k] = a[i]; c[k] = i; k++; } }; int main() { int *a, *b, *c, m = 0, k = 0; FILE *in; if((in=fopen("1.txt","r"))==NULL) { printf("\n net faila"); exit(1); } while(fscanf(in, "%d") != EOF) m++; a = (int*)malloc(m*sizeof(int)); b = (int*)malloc(m*sizeof(int)); c = (int*)malloc(m*sizeof(int)); for(int i = 0; i < m; i++){ b[i] = 0; c[i] = 0; } rewind(in); for(int i = 0;i < m;i++) fscanf(in,"%d", &a[i]); matrica(a, b, c, m, k); std::cout<<"Value: "<<std::endl; for(int i = 0;i < k;i++) std::cout<<b[i]<<" "; std::cout<<std::endl; std::cout<<"Index: "<<std::endl; for(int i = 0;i < k;i++) std::cout<<c[i]<<" "; std::cout<<std::endl; return 0; }
|