Pagini recente » Cod sursa (job #2535879) | Cod sursa (job #2105769) | Cod sursa (job #594057) | Cod sursa (job #2110914) | Cod sursa (job #944302)
Cod sursa(job #944302)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int positiveOrNegativeColumn(int column,int lineSize,int matrix[100][16]){
int i,positive,negative;
positive = 0;
negative = 0;
for(i = 0;i < lineSize;i++){
if(matrix[i][column] >= 0){
positive += matrix[i][column];
}else{
negative += matrix[i][column];
}
}
negative *=-1;
if(negative > positive){
return 1;
}else{
return 0;
}
}
int positiveOrNegativeLine(int line,int columnSize,int matrix[100][16]){
int i,positive,negative;
positive = 0;
negative = 0;
for(i = 0;i < columnSize;i++){
if(matrix[line][i] >= 0){
positive += matrix[line][i];
}else{
negative += matrix[line][i];
}
}
negative *=-1;
if(negative > positive){
return 1;
}else{
return 0;
}
}
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;
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++){
lines[i] = positiveOrNegativeLine(i,m,matrice);
}
for(i = 0;i < m;i++){
columns[i] = positiveOrNegativeColumn(i,n,matrice);
}
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;
}