Pagini recente » Cod sursa (job #1599735) | Cod sursa (job #2276930) | Cod sursa (job #1595555) | Cod sursa (job #1158804) | Cod sursa (job #944306)
Cod sursa(job #944306)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int writeInFile(int finalValue){
FILE *out;
out = fopen("flip.out","w");
if(out == NULL){
printf("Eroare!Nu s-a deschis fisierul de iesire!\n");
return 1;
}
fprintf(out,"%d",finalValue);
printf("%d\n",finalValue);
fclose(out);
}
int readFromFile(){
FILE *in;
int n,m,matrice[100][16],i,j,lines[100],columns[16],sum,positive,negative;
in = fopen("flip.in","r");
if(in == NULL){
printf("Eroare!Nu s-a deschis fisierul de intrare!\n");
return 1;
}
fscanf(in,"%d",&n);
printf("%d\n",n);
fscanf(in,"%d",&m);
printf("%d\n",m);
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
fscanf(in,"%d",&matrice[i][j]);
printf("%d ",matrice[i][j]);
}
printf("\n");
}
fclose(in);
for(i = 0;i < n;i++){
positive = 0;
negative = 0;
for(j = 0;j < m;j++){
if(matrix[i][j] >= 0){
positive += matrix[i][j];
}else{
negative += matrix[i][j];
}
}
negative *=-1;
if(negative > positive){
lines[i] = 1;
}else{
lines[i] = 0;
}
}
for(i = 0;i < m;i++){
positive = 0;
negative = 0;
for(j = 0;j < n;j++){
if(matrix[j][i] >= 0){
positive += matrix[j][i];
}else{
negative += matrix[j][i];
}
}
negative *=-1;
if(negative > positive){
columns[i] = 1;
}else{
columns[i] = 0;
}
}
sum=0;
for(i = 0;i < n;i++){
for(j = 0;j < m;j++){
if(lines[i] == 1 || columns[j] == 1){
sum += matrice[i][j]*(-1);
}else{
sum += matrice[i][j];
}
}
}
writeInFile(sum);
}
int main(){
readFromFile();
return 0;
}