Friday, November 3, 2017

Day 13 - Higher Dimensional Arrays

CLASSWORK:
Power Plant Data Code
/* */
/* This program computes power averages over ten weeks. */
#include <stdio.h>
#define NROWS 8
#define NCOLS 7
#define FILENAME "power1.txt"
int main(void)
{
/* Declare variables. */
int i, j;
int power[NROWS][NCOLS], col_sum;
FILE *file_in;
/* Read information from a data file. */
file_in = fopen(FILENAME,"r");
if (file_in == NULL)
printf("Error opening input file. \n");
else
{
for (i=0; i<=NROWS-1; i++)
for (j=0; j<=NCOLS-1; j++)
fscanf(file_in,"%d",&power[i][j]);
/* Compute and print daily averages. */
for (j=0; j<=NCOLS-1; j++)
{
col_sum = 0;
for (i=0; i<=NROWS-1; i++)
col_sum += power[i][j];
printf("Day %d: Average = %.2f \n",j+1,(double)col_sum/NROWS);
}
fclose(file_in); /* Close file. */
}
return 0; /* Exit program. */
}




HOMEWORK:
Terrain Data Code:
/* */
/* This program determines the locations of peaks in an */
/* grid of elevation data; also displays highest peak & lowest */  /* valley. */
#include <stdio.h>
#include <math.h>
#define N 25
#define FILENAME "grid2.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
double col_initial=0;
double row_initial=6;
double peak_multiplier=100;
double elevation[N][N];
FILE *grid1;
/* Read information from a data file. */
grid1 = fopen(FILENAME,"r");
if (grid1 == NULL)
printf("Error opening input file\n");
else
{
/* Read the header from my txt file to know the # of rows and cols */
fscanf(grid1,"%d %d",&nrows,&ncols);
printf("There are %i rows and %i columns. \n", nrows, ncols);
for (i=1; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
{
fscanf(grid1,"%lf",&elevation[i][j]);
printf("%f ", elevation[i][j]);
if (j == ncols-1)
printf("\n");
}
/* Determine and print peak locations. */
printf("\nTop left point defined as row 1, column 0. \n");
int peak_count = 0;
int valley_count = 0;
double peak_max = 0;
double valley_min = 0;
for (i=2; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) && // The first four lines
(elevation[i+1][j]<elevation[i][j]) && // are checking the four
(elevation[i][j-1]<elevation[i][j]) && // directly adjacent
(elevation[i][j+1]<elevation[i][j]) && // values.
(elevation[i-1][j-1]<elevation[i][j]) && // The last four lines
(elevation[i-1][j+1]<elevation[i][j]) && // are checking the four
(elevation[i+1][j-1]<elevation[i][j]) && // values diagonal to it.
(elevation[i+1][j+1]<elevation[i][j])) //------------------------
{
// Calculate distance between two pts on the grid
double d_horiz = (double)j - col_initial;
double d_vertical = (double)i - row_initial;
double peak_dist = peak_multiplier*sqrt(d_horiz*d_horiz + d_vertical*d_vertical);

// Print peak location, peak value, and distance between peak and given point
printf("\nPeak at row: %d, column: %d,\t",i,j);
printf("Value= %f, \t", elevation[i][j]);
printf("Distance from lower left corner: %5.2f feet\n", peak_dist);
peak_count++;
if (elevation[i][j] > peak_max)
{
peak_max = elevation[i][j];
}
}
else if ((elevation[i-1][j]>elevation[i][j]) &&
(elevation[i+1][j]>elevation[i][j]) &&
(elevation[i][j-1]>elevation[i][j]) &&
(elevation[i][j+1]>elevation[i][j]))
{
printf("\nValley at row: %i, column: %i,\t",i,j);
printf("Value= %f, \n", elevation[i][j]);
valley_count++;
if (elevation[i][j] < peak_max) {
valley_min = elevation[i][j];
}
}

printf("\nNumber of peaks: %i \n", peak_count);
printf("\nHighest Peak: %f \n", peak_max);
printf("\nNumber of valleys: %i \n", valley_count);
printf("\nLowest Valley: %f \n", valley_min);
fclose(grid1); /* Close file. */
}
return 0; /* Exit program. */

}

No comments:

Post a Comment