Program To Store Meteorological Data In An Array Using Sequential, Selection, And Repetition Programming Statements
Program Description
- Demonstrate you successfully followed the steps in this lab by preparing screen captures of you running the lab as specified in the Instructions above.
Solution:
Copy the program specified below to the program section in the C compiler
#define NUMMONTHS 12
#define NUMYEARS 5
#include
// function prototypes
void inputdata();
void printdata();
// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {“2011″,”2012″,”2013″,”2014″,”2015”};
char months[NUMMONTHS][12]
={“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”};
int main ()
char enterData = ‘y’;
printf(“Do you want to input Precipatation data? (y for yes)n”);
scanf(“%c”,&enterData);
if (enterData == ‘y’) {
// Call Function to Input data
inputdata();
// Call Function to display data
printdata();
printf(“No data was input at this timen”);
printf(“Please try the Precipitation program again. n”);
return 0;
// function to inputdata
void inputdata() {
/* variable definition: */
float Rain=1.0;
// Input Data
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“Enter rain for %d, %d:n”, year+1, month+1);
scanf(“%f”,&Rain);
Raindata[year][month]=Rain;
n to printdata
void printdata(){
// Print data
printf (“yeart montht rainn“);
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“%st %st %5.2fn”,
years[year],months[month],Raindata[year][month]);
Choose input type as text and enter the input data in the text Section
- Modify the program to add a function to sum the rainfall for each year. (Hint: you need to sum for each year. You can do this using a looping structure). Support your experimentation with screen captures of executing the new code.
Solution:
Program:
#define NUMMONTHS 12
#define NUMYEARS 5
#include
// function prototypes
void inputdata();
void printdata();
void printsumofrainfall();
// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {“2011″,”2012″,”2013″,”2014″,”2015”};
char months[NUMMONTHS][12]
={“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”};
int main ()
{
char enterData = ‘y’;
printf(“Do you want to input Precipatation data? (y for yes)n”);
scanf(“%c”,&enterData);
if (enterData == ‘y’) {
// Call Function to Input data
inputdata();
// Call Function to display data
printdata();
// Call Function to determine the sum of rainfall in each year
printsumofrainfall();
printf(“No data was input at this timen”);
printf(“Please try the Precipitation program again. n”);
return 0;
// function to inputdata
void inputdata() {
/* variable definition: */
float Rain=1.0;
// Input Data
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“Enter rain for %d, %d:n”, year+1, month+1);
scanf(“%f”,&Rain);
Raindata[year][month]=Rain;
// Function to printdata
void printdata(){
// Print data
printf (“yeart montht rainn”);
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“%st %st %5.2fn”,
years[year],months[month],Raindata[year][month]);
// Function to printsumofrainfall
void printsumofrainfall(){
printf(“nThe Sum of rainfall for each year is given below:n”);
printf (“yeart sum_of_rainfalln”);
for (int year=0;year < NUMYEARS; year++) {
float rainfall_sum=0;
for (int month=0; month< NUMMONTHS; month++) {
rainfall_sum += Raindata[year][month];
printf(“%st %5.2fn”, years[year], rainfall_sum);
- Enhance the program to allow the user to enter another meteorological element such as windspeed (e.g. 2.4 mph). Note, the user should be able to enter both rainfall and windspeed in your new implementation. Support your experimentation with screen captures of executing the new code.
Program:
#define NUMMONTHS 12
#define NUMYEARS 5
#include
// function prototypes
void inputdata();
void printdata();
void printaggregatedata();
// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
float Winddata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {“2011″,”2012″,”2013″,”2014″,”2015”};
char months[NUMMONTHS][12] = {“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”};
char enterData = ‘y’;
printf(“Enter data? = n”);
scanf(“%c”,&enterData);
if (enterData == ‘y’) {
// Call Function to Input data
inputdata();
// Call Function to display data
printdata(
// Call Function to determine the aggregate in each year
printaggregatedata();
printf(“No data was input at this timen”);
printf(“Please try the Precipitation program again. n”);
return 0;
// function to inputdata
void inputdata() {
/* variable definition: */
float Rain=1.0;
float Wind=1.0;
// Input Data
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“Enter rain for %d, %d:n”, year+1, month+1);
scanf(“%f”,&Rain);
Raindata[year][month]=Rain;
printf(“Enter wind for %d, %d:n”, year+1, month+1);
scanf(“%f”,&Wind);
Winddata[year][month]=Wind;
// Function to printdata
void printdata(){
// Print data
printf (“yeart montht raint windn”);
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“%st %st %5.2ft %5.2fn”,
years[year],months[month],Raindata[year][month], Winddata[year][month]);
// Function to printaggregatedata
void printaggregatedata(){
printf(“nThe Sum of rainfall and wind for each year is given below:n”);
printf (“yeart sum_of_rainfallt sum_of_windn”);
for (int year=0;year < NUMYEARS; year++) {
float rainfall_sum=0;
float wind_sum=0;
for (int month=0; month< NUMMONTHS; month++) {
rainfall_sum += Raindata[year][month];
wind_sum += Winddata[year][month];
f(“%st %5.2fttt %5.2fn”, years[year], rainfall_sum, wind_sum);
- What happens if you change the NUMMONTHS and NUMYEARS definitions to other values? Be sure to use both lower and higher values. Describe and implement fixes for any issues if errors result. Support your experimentation with screen captures of executing the new code.
Solution:
The lower value for the NUMMONTHS and NUMYEARS definitions is 1. On having the values less than their limit 1, a message is displayed to the user as shown below.
The updated code that takes care of the lower and higher value of number of months and years is given below.
Program:
#define NUMMONTHS 12
#define NUMYEARS 6
#include
// function prototypes
void inputdata();
void printdata();
void printaggregatedata();
// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
float Winddata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {“2011″,”2012″,”2013″,”2014″,”2015”};
char months[NUMMONTHS][12] = {“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”};
int main ()
char enterData = ‘y’;
printf(“Enter data? = n”);
scanf(“%c”,&enterData);
if (enterData == ‘y’)
if(NUMMONTHS > 0 && NUMYEARS > 0)
if(NUMMONTHS <=12 && NUMYEARS <= 5)
// Call Function to Input data
inputdata();
// Call Function to display data
printdata();
// Call Function to determine the aggregate in each year
printaggregatedata();
else {
printf(“The number of years and months can be respectively be atmost 5 and 12 only.n”);
void printdata(){
// Print data
printf (“yeart montht raint windn”);
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf(“%st %st %5.2ft %5.2fn”,
years[year],months[month],Raindata[year][month], Winddata[year][month])
// Function to printaggregatedata
void printaggregatedata(){
printf(“nThe Sum of rainfall and wind for each year is given below:n”);
printf (“yeart sum_of_rainfallt sum_of_windn”);
for (int year=0;year < NUMYEARS; year++) {
float rainfall_sum=0;
float wind_sum=0;
for (int month=0; month< NUMMONTHS; month++) {
rainfall_sum += Raindata[year][month];
wind_sum += Winddata[year][month];
printf(“%st %5.2fttt %5.2f n”, years[year], rainfall_sum, wind_sum);