C Programming Sample Exam Solution
Exam Question:
The conformation of a seabed is stored in a map of size NxN. The map describes the depth in meters in the range 0-5000 meters. N is a constant and known a priori defined through the #define directive. The integers that constitute the map are separated by a space.
Write a C program to check for any changes in the seabed due to volcanic eruptions under the sea and/or tectonic shift. In this regard, assume that you have to have two text files containing Map2.txt
and Map1.txt, and the depth of the seabed is taken at 36 months after one another. The program receives 3 integer numbers passed as arguments from the command line. The first two represent the coordinates of the center of a square of size MxM all inside of the map, the third represents the value of M (assume M odd and less than N). Let’s assume that the square of interest defined by the
user is entirely contained in the map.
The program must check for changes in the selected region, in case of an actual change, the program must:
1. Print on the screen coordinates of the points inside of the square of interest and the corresponding percentages of variation (two decimal places) only if they are different from zero .
2. If all points inside of the square of interest have undergone the same type of variation (towards the up or down direction) but not necessarily by the same amount, print on screen the message "TECTONIC SHIFT".
Example: N = 6
Map1.txt
1200 1205 1213 1220 1225 999
1240 1225 1120 1130 1164 1110
1320 1230 1011 963 1102 1017
1410 1340 1100 940 1010 960
1501 1345 1204 923 1002 1001
1507 1370 1230 1100 1001 901
Map2.txt
1200 1205 1213 1220 1225 999
1240 1225 1120 1130 1164 1110
1320 1230 1011 963 1102 1017
1410 1340 1100 940 1010 960
1522 1352 1150 923 1002 1001
1537 1380 1245 1100 1001 901
Map3.txt
1200 1205 1213 1220 1225 999
1240 1225 1120 1130 1164 1110
1320 1230 1011 963 1102 1017
1310 1240 1000 940 1010 960
1401 1245 1104 923 1002 1001
1407 1270 1130 1100 1001 901
Ex.1
C:\> fondale 5 2 3
5,1: 1.40%
5,2: 0.52%
5,3: -4.49%
6,1: 1.99%
6,2: 0.73%
6,3: 1.22%
Ex.2
C:\> fondale 5 2 3
4,1: -7.09%
4,2: -7.46%
4,3: -9.09%
5,1: -6.66%
5,2: -7.43%
5,3: -8.31%
6,1: -6.64%
6,2: -7.30%
6,3: -8.13%
TECTONIC SHIFT
==> SOLUTION:
#include <stdio.h>
#include <stdlib.h>
#define N 6
/// passed 3 command line arguments + 2 text files
/// first two are coordinates of the center of a square of size MxM
/// third is value of M (odd and less than N)
void convertToMatrix(FILE *fp,int mat [][N]);
void checkVariation(int mat1 [][N], int mat2 [][N],int r, int c, int M);
int main(int argc, char *argv[]){
int i,j;
int mat1[N][N],mat2[N][N], mat3[N][N];
int r = atoi(argv[1]);
int c = atoi(argv[2]);
int M = atoi(argv[3]);
FILE *fp1, *fp2, *fp3;
///read file and convert to matrix
fp1=fopen("Map1.txt","r");
fp2=fopen("Map2.txt","r");
fp3=fopen("Map3.txt","r"); /// for second exercise with different map file
///check for null pointer
if(fp1==NULL || fp2==NULL || fp3== NULL){
fprintf(stderr,"error opening of file.\n");
exit(1);
}
convertToMatrix(fp1,mat1);
convertToMatrix(fp2,mat2);
convertToMatrix(fp3,mat3);
///for ex1 checking variation
printf("Checking variation for ex1.\n");
checkVariation(mat1,mat2,r,c,M);
///for ex2 checking variation
printf("Checking variation for ex2.\n");
checkVariation(mat1,mat3,r,c,M);
// printf("%d \t%d\t%d\t",r,c,M);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
void convertToMatrix(FILE *fp,int mat [][N]){
int i,j;
for(i=1;i<=N;i++){
for(j=1;j<=N;j++){
if(!fscanf(fp,"%d",& mat[i][j])){
printf("error scanning\n");
}
}
}
}
void checkVariation(int mat1 [][N], int mat2 [][N],int r,int c,int M){
// two cases : same variation and not same variation
// first for not same variation
int i,j,temp=0;
int sub;
float res ;
for(i=r-1;i<=r+1;i++){
for(j=c-1;j<=c+1;j++){
if(mat1[i][j]!=mat2[i][j]){
sub = mat2[i][j]-mat1[i][j];
res = (float) sub/mat1[i][j];
printf("%d,%d : %d --> %d : %f % \n ",i,j,mat1[i][j],mat2[i][j],res*100);
if(res<0){
temp+=0;
}
else{
temp+=1;
}
}
}
}
if(temp==0) printf("TECTONIC SHIFT\n");
}
The conformation of a seabed is stored in a map of size NxN. The map describes the depth in meters in the range 0-5000 meters. N is a constant and known a priori defined through the #define directive. The integers that constitute the map are separated by a space.
Write a C program to check for any changes in the seabed due to volcanic eruptions under the sea and/or tectonic shift. In this regard, assume that you have to have two text files containing Map2.txt
and Map1.txt, and the depth of the seabed is taken at 36 months after one another. The program receives 3 integer numbers passed as arguments from the command line. The first two represent the coordinates of the center of a square of size MxM all inside of the map, the third represents the value of M (assume M odd and less than N). Let’s assume that the square of interest defined by the
user is entirely contained in the map.
The program must check for changes in the selected region, in case of an actual change, the program must:
1. Print on the screen coordinates of the points inside of the square of interest and the corresponding percentages of variation (two decimal places) only if they are different from zero .
2. If all points inside of the square of interest have undergone the same type of variation (towards the up or down direction) but not necessarily by the same amount, print on screen the message "TECTONIC SHIFT".
Example: N = 6
Map1.txt
1200 1205 1213 1220 1225 999
1240 1225 1120 1130 1164 1110
1320 1230 1011 963 1102 1017
1410 1340 1100 940 1010 960
1501 1345 1204 923 1002 1001
1507 1370 1230 1100 1001 901
Map2.txt
1200 1205 1213 1220 1225 999
1240 1225 1120 1130 1164 1110
1320 1230 1011 963 1102 1017
1410 1340 1100 940 1010 960
1522 1352 1150 923 1002 1001
1537 1380 1245 1100 1001 901
Map3.txt
1200 1205 1213 1220 1225 999
1240 1225 1120 1130 1164 1110
1320 1230 1011 963 1102 1017
1310 1240 1000 940 1010 960
1401 1245 1104 923 1002 1001
1407 1270 1130 1100 1001 901
Ex.1
C:\> fondale 5 2 3
5,1: 1.40%
5,2: 0.52%
5,3: -4.49%
6,1: 1.99%
6,2: 0.73%
6,3: 1.22%
Ex.2
C:\> fondale 5 2 3
4,1: -7.09%
4,2: -7.46%
4,3: -9.09%
5,1: -6.66%
5,2: -7.43%
5,3: -8.31%
6,1: -6.64%
6,2: -7.30%
6,3: -8.13%
TECTONIC SHIFT
==> SOLUTION:
#include <stdio.h>
#include <stdlib.h>
#define N 6
/// passed 3 command line arguments + 2 text files
/// first two are coordinates of the center of a square of size MxM
/// third is value of M (odd and less than N)
void convertToMatrix(FILE *fp,int mat [][N]);
void checkVariation(int mat1 [][N], int mat2 [][N],int r, int c, int M);
int main(int argc, char *argv[]){
int i,j;
int mat1[N][N],mat2[N][N], mat3[N][N];
int r = atoi(argv[1]);
int c = atoi(argv[2]);
int M = atoi(argv[3]);
FILE *fp1, *fp2, *fp3;
///read file and convert to matrix
fp1=fopen("Map1.txt","r");
fp2=fopen("Map2.txt","r");
fp3=fopen("Map3.txt","r"); /// for second exercise with different map file
///check for null pointer
if(fp1==NULL || fp2==NULL || fp3== NULL){
fprintf(stderr,"error opening of file.\n");
exit(1);
}
convertToMatrix(fp1,mat1);
convertToMatrix(fp2,mat2);
convertToMatrix(fp3,mat3);
///for ex1 checking variation
printf("Checking variation for ex1.\n");
checkVariation(mat1,mat2,r,c,M);
///for ex2 checking variation
printf("Checking variation for ex2.\n");
checkVariation(mat1,mat3,r,c,M);
// printf("%d \t%d\t%d\t",r,c,M);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
void convertToMatrix(FILE *fp,int mat [][N]){
int i,j;
for(i=1;i<=N;i++){
for(j=1;j<=N;j++){
if(!fscanf(fp,"%d",& mat[i][j])){
printf("error scanning\n");
}
}
}
}
void checkVariation(int mat1 [][N], int mat2 [][N],int r,int c,int M){
// two cases : same variation and not same variation
// first for not same variation
int i,j,temp=0;
int sub;
float res ;
for(i=r-1;i<=r+1;i++){
for(j=c-1;j<=c+1;j++){
if(mat1[i][j]!=mat2[i][j]){
sub = mat2[i][j]-mat1[i][j];
res = (float) sub/mat1[i][j];
printf("%d,%d : %d --> %d : %f % \n ",i,j,mat1[i][j],mat2[i][j],res*100);
if(res<0){
temp+=0;
}
else{
temp+=1;
}
}
}
}
if(temp==0) printf("TECTONIC SHIFT\n");
}
Comments
Post a Comment