Vous êtes sur la page 1sur 10

SAMPLE CODE FOR MINI PROJECT

WEATHER PROGRAM
Given the following weather review for period November 2017 to February 2018, State, Duration and Monthly Rainfall Estimation as follows:

Sample expected inputs and outputs as follows


Weather program as follows
/* File : weather.c
Owner : Mdm Norzanah Md Said
Tasks : Weather program using array and function
Semester July 2017 */
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#define MAX 4

//prototype function
void chooseMenu(short &menu);
void callSel(char *state[], char *tempoh[], int hjnSelmin[], int hjnSelmax[] );
void callMel(char *state[], char *tempoh[], int hjnSelmin[], int hjnSelmax[] );
void callTG(char *state[], char *tempoh[], int hjnSelmin[], int hjnSelmax[] );
void SelMMX(char *state[], char *tempoh[], int hjnSelmin[], int hjnSelmax[] );
void TGMMX(char *state[], char *tempoh[], int hjnTGmin[], int hjnTGmax[] );
void callMel(char *state[], char *tempoh[], int hjnMelmin[], int hjnMelmax[]);
void callExit(int &resp);
void callSignature();
int callError();
bool callRepeat(int no, int menu);
void Summary(char *state[], char *tempoh[], int S1[],int S2[],int M1[],int M2[], int T1[],int T2[]);

//main() function
int main()
{
int menu, no ;

char *state[] = {"Selangor","Melaka", "Terengganu"};


char *tempoh[] = {"November", "Disember", "January", "February"};
int hjnSelmin[MAX] = {240, 200, 140, 150};
int hjnSelmax[MAX] = {360, 300, 220, 210};
int hjnMelmin[MAX] = {190, 110, 80, 70};
int hjnMelmax[MAX] = {280, 170, 110, 110};
int hjnTGmin[MAX] = {580, 450, 110, 50};
int hjnTGmax[MAX] = {870, 670, 160, 80};

bool resp;

resp = 1;
//repetition statements
while (resp)
{
chooseMenu(menu); //function call for chooseMenu()

//selection statements
switch (menu)
{
case 1 : callSel(state, tempoh, hjnSelmin, hjnSelmax ); //function call for callSel()
resp = callRepeat(no,menu);
break;
case 2 : callMel(state, tempoh, hjnMelmin, hjnMelmax); //function call for callMel()
resp = callRepeat(no,menu);
break;
case 3 : callTG(state, tempoh, hjnTGmin, hjnTGmax); //function call for callTG()
resp = callRepeat(no,menu);
break;
case 4 : Summary(state, tempoh, hjnSelmin, hjnSelmax, hjnMelmin, hjnMelmax,hjnTGmin, hjnTGmax);
resp = callRepeat(no,menu);
break;
case 5 : callExit(resp); //function call for callExit()
break;
default : resp = callError(); //function call for callExit()
resp = callRepeat(no,menu);
break;
}//switch

} //while
callSignature(); //function call for callSignature()
return 0;
}

//function definition for chooseMenu() using parameter, call by reference


void chooseMenu(int &menu)
{

system("cls");

printf("\t=====================================\n");
printf("\t== WEATHER REVIEW FOR PERIOD ==\n");
printf("\t== November 2017 TO FEBRUARY 2018 ==\n");
printf("\t=====================================\n\n");

printf("\tSelect State\n");
printf("\t[1] Selangor\n");
printf("\t[2] Melaka\n");
printf("\t[3] Terengganu\n");
printf("\t[4] Summary\n");
printf("\t[5] Exit\n\n\n");
scanf("%d",&menu);
printf("\tYou have select number #0%d\n\n",menu);

//function definition for callSel()with no return value that accepts four parameters
void callSel(char *state[], char *tempoh[], int hjnSelmin[], int hjnSelmax[] )
{
int i;
printf ("\tNegeri : %s\n", state[0]);
printf("\n\tTempoh \t Monthly Rainfall Expected (mm)\n");
printf("\n\t====== \t ==============================\n");
for (i = 0; i < MAX ; i++) {
printf("\t%s\t \t%d - %d\n",tempoh[i],hjnSelmin[i],hjnSelmax[i]);
}
printf("\n\n");
}

//function definition for callMel()with no return value that accepts four parameters
void callMel(char *state[], char *tempoh[], int hjnMelmin[], int hjnMelmax[] )
{
int i;
printf ("\tNegeri : %s\n", state[1]);
printf("\n\tTempoh \t Monthly Rainfall Expected (mm)\n");
printf("\n\t====== \t ==============================\n");
for (i = 0; i < MAX ; i++) {
printf("\t%s\t \t%d - %d\n",tempoh[i],hjnMelmin[i],hjnMelmax[i]);
}
printf("\n\n");
}
//function definition for callTG()with no return value that accepts four parameters
void callTG(char *state[], char *tempoh[], int hjnTGmin[], int hjnTGmax[] )
{
int i;
printf ("\tNegeri : %s\n", state[2]);
printf("\n\tTempoh \t Monthly Rainfall Expected (mm)\n");
printf("\n\t====== \t ==============================\n");
for (i = 0; i < MAX ; i++) {
printf("\t%s\t \t%d - %d\n",tempoh[i],hjnTGmin[i],hjnTGmax[i]);
}
printf("\n\n");
}

// function definition for MelMMX() to find the minimum and maximum value of the Rainfall Estimates for
// the state of Melaka and display the values on the screen
void MelMMX(char *state[], char *tempoh[], int hjnMelmin[], int hjnMelmax[] )
{
int i, lowMel, highMel, hold1, hold2;

lowMel = hjnMelmin[0];
for (i = 1; i < MAX ; i++) {
if (hjnMelmin[i] < lowMel){
lowMel = hjnMelmin[i];
hold1 = i;
}
}

highMel = hjnMelmax[0];
for (i = 1; i < MAX ; i++) {
if (hjnMelmax[i] > highMel){
highMel = hjnMelmax[i];
hold2 = i;
}
}

printf("\t%s : Lowest Rainfall (mm) is %d on %s \n", state [1],lowMel, tempoh[hold1], );


printf("\t%s : Highest Rainfal (mm) is %d on %s \n", state [1],highMel, tempoh[hold2]);

printf("\n\n");
}

// function definition for SelMMX() to find the minimum and maximum value of the Rainfall Estimates for
// the state of Selangor and display the values on the screen

void SelMMX(char *state[], char *tempoh[], int hjnSelmin[], int hjnSelmax[] )


{
int i, lowSel, highSel, hold1, hold2;

lowSel = hjnSelmin[0];
for (i = 1; i < MAX ; i++) {
if (hjnSelmin[i] < lowSel){
lowSel = hjnSelmin[i];
hold1 = i;
}
}
highSel = hjnSelmax[0];
for (i = 1; i < MAX ; i++) {
if (hjnSelmax[i] > highSel){
highSel = hjnSelmax[i];
hold2 = i;
}
}

printf("\t%s : Lowest Rainfall (mm) is %d on %s \n", state [0],lowSel, tempoh[hold1], );


printf("\t%s : Highest Rainfal (mm) is %d on %s \n", state [0],highSel, tempoh[hold2]);

printf("\n\n");
}

// function definition for TGMMX() to find the minimum and maximum value of the Rainfall Estimates for //
the state of Terengganu and display the values on the screen

void TGMMX(char *state[], char *tempoh[], int hjnTGmin[], int hjnTGmax[] )


{
int i, lowTG, highTG, hold1, hold2;

lowTG = hjnTGmin[0];
for (i = 1; i < MAX ; i++) {
if (hjnTGmin[i] < lowTG){
lowTG = hjnTGmin[i];
hold1 = i;
}
}

highTG = hjnTGmax[0];
for (i = 1; i < MAX ; i++) {
if (hjnTGmax[i] > highTG){
highTG = hjnTGmax[i];
hold2 = i;
}
}

printf("\t%s : Lowest Rainfall (mm) is %d on %s \n", state [2],lowTG, tempoh[hold1], );


printf("\t%s : Highest Rainfal (mm) is %d on %s \n", state [2],highTG, tempoh[hold2]);

printf("\n\n");
}

// function definition for Summary() to display summary report


void Summary(char *state[], char *tempoh[], int S1[],int S2[],int M1[],int M2[], int T1[],int T2[])
{
printf("\tSUMMARY REPORT : \n");

SelMMX(state, tempoh, S1, S2);


MelMMX(state, tempoh, M1, M2);
TGMMX(state, tempoh, T1, T2);
}

//function definition for callError() has a return value of integer type


int callError()
{
int resp;
printf("\tYour have enter invalid selection\n");
printf("\tPlease re-enter\n");
return resp = 1;

//function definition for callExit() with no return value


// using parameter, call by reference
void callExit(int &resp)
{
printf("\t--> Thank You for using our weather system <--\n");
resp = 0;
}
//function definition for callSignature() with no return value and no //argument)
void callSignature()
{
printf("\n\tPrepared by :\n");
printf("\tMdm Norzanah Md Said \n");
printf("\tSemester July 2017\n\n\n");
}

//function definition for callRepeat() has a return value of boolean type


bool callRepeat(int no, int menu){
bool resp;

printf("\n\tDo you want to continue [1] yes, [Press any] No\n");


scanf("%d",&no);

if ((no != 1) || (menu ==4 ))


return resp = 0;

else
return resp = 1;
}

Vous aimerez peut-être aussi