The Donor System: An Information System for a Charity

The Donor System: An Information System for a Charity

Assignment Context

To develop a system that accepts details of donors and their donation amount and date and displays the same to them. The system accepts upto 4 records, and notifies the user in case the user tries to exceed entering the number of donors, while also displaying the donation records entered and the donation total. The system provides a menu option for entering donation records, availing help on using the system as well as an option for quitting the system. The system is to be developed using C programming language, which is a high-level procedural programming language.

 

Principles of Procedural Programming

The following are the major principles, features, and characteristics of procedural programming.

  • User-defined functions – Large programs are broken down into smaller modules/parts called functions. This makes it easier to read, reused, and change the code. The following code shows how functions are used in the C language. The program accepts two numbers from the user, adds them, and prints the output. This can be achieved by writing the entire code in the main() function. However, we have used a user-defined function add() to calculate the sum and pass the result to the main() function. The main() function passes the user input to the add() function, and the add() function calculates and returns the result to the main() function. The main() function then prints the result on the screen.

/* Program to show use of function */

#include<stdio.h>

int add(int num1, int num2);           //function declaration

 

// main function

int main(){

int first_num,second_num,sum;

 

// read user input

printf(“\nEnter the first number: “);

scanf(“%d”,&first_num);

printf(“\nEnter the second number: “);

scanf(“%d”,&second_num);

 

// call function add to sum the two numbers

sum=add(first_num,second_num);

printf(“Sum of %d and %d = %d”,first_num, second_num,sum);

return 0;

}

 

int add(int num1,int num2)            //function definition

{

int f_sum;

f_sum= num1 + num2;

return f_sum;                  //return the sum of two numbers to the calling function

 

}

 

  • Use of local and global variables – Local variables can be used and accessed only in the function where they are declared, while global variables can be used and accessed anywhere in the program. Global variables are used when the same data needs to be accessed and used by multiple functions. In the below code, the value of “num” printed in the main() function is 1, which is locally declared. Whereas, when the function display() is called, it prints the global value of the variable “num”, since the global variable can be accessed from anywhere, and also since the function display() does not have a local variable named “num”, whose value can be printed.

/* Program to show use of local and global variables */

#include<stdio.h>

 

// global variable

int num=15;

 

void display()

{

printf(“%d\n”,num);

}

 

int main()

{

int num=1;  //local variable

printf(“%d\n”,num);

display();

getchar();

return 0;

}

 

  • Top-down or sequential approach to programming – Procedural languages like C follow top-down approach of code execution, where the code is executed in a linear fashion, executing one statement after another in the sequence in which they are written.
  • Selection Control – The if..else statements in C language provides for selection control, which guides how the program will be executed. Based on the results of the decision of the if..else control statement, the program selectively follows the execution path. The following code accepts a number from the user and checks if it is negative or positive. The “if” statement is used to check if the number is less than zero and if true, it prints the number is negative. Else it prints the number as positive. Thus, the if..else statement ensures only one output is displayed depending on the result of the condition being checked, which governs the execution path of the program.

/* Program to show use if..else statement */

#include<stdio.h>

 

int main()

{

int num;

printf(“\nEnter a number: “);

scanf(“%d”,&num);

if(num<0) {

printf(“Number is negative”);

}

else

{

printf(“Number is positive”);

}

getchar();

return 0;

}

 

  • Iteration – The for loop, while loop, and do..while loop statements in C language provide for iteration when a same task is to be performed multiple times till a particular condition is reached. This is important while printing a large number of records or while performing a set of instructions till a specific condition is met. The below code accepts a number from the user and prints all the values from 1 to that number. In the “for” loop, the value of the iterator “i” is initially set to 1. Every time the loop executes, it checks if the value of “i” is less than or equal to “num”. It prints all numbers till value of “i” is less than or equal to “num”, after which it terminates.

/* Program to show use for loop */

#include<stdio.h>

 

int main()

{

int i, num;

printf(“\nEnter a number: “);

scanf(“%d”,&num);

for (i=1; i<=num; i++)

printf(“\n%d”,i);

 

getchar();

return 0;

}

 

  • Parameter Passing – Variables can be passed from one function to another in the form of parameters for processing results or for performing calculations. Calculated variables are passed as return values by functions after performing the calculation on the parameters. In the following code, the main() functions calls the add() function to calculate the total of two numbers entered by the user. The main() function passes numbers entered by the user as parameters to add() function. The add() function calculates the sum and passes the result as return value to the calling main() function.

/* Program to show use of parameter passing */

#include<stdio.h>

int add(int num1, int num2);           //function declaration

 

// main function

int main(){

int first_num,second_num,sum;

 

// read user input

printf(“\nEnter the first number: “);

scanf(“%d”,&first_num);

printf(“\nEnter the second number: “);

scanf(“%d”,&second_num);

 

// call function add to sum the two numbers

sum=add(first_num,second_num);

printf(“Sum of %d and %d = %d”,first_num, second_num,sum);

return 0;

}

 

int add(int num1,int num2)            //function definition

{

int f_sum;

f_sum= num1 + num2;

return f_sum;                  //return the sum of two numbers to the calling function

 

}

 

Programming Solution Design

The solution for the program involves breaking the program in several units/modules so that each unit performs a designated task, and the solution is clear to implement and understand. Also, this will lead to increased reusability, legibility, and compactness of the programming solution. The program uses functions, conditional statements, control structures, and data structures to implement the solution.

The program implements a main() function, which contains the primary code of solution implementation. Two other functions named display() and help() are defined that aid the main() function in implementing the solution. The following structure chart describes how the programming solution is organized.

 

 

 

 

 

 

 

 

 

 

 

 

 

The system first displays a menu to the user prompting him to enter his choice. If the user chooses to enter donation records, he is prompted to enter donation details, which are validated by the system. This process uses the display() function to print the donation records and the donated total to the user whenever the user completes entering 4 records or when the user stops entering further records. The help() function is called when the user chooses the menu option of displaying help on using the system. User can anytime exit from the system by choosing the appropriate option from the menu.

 

 

The following IPO chart describes what data is entered, how it is processed, and what output is displayed.

Input Process Output
On choosing Menu Option 1:

selection=1

 

 

 

 

 

 

 

 

 

 

 

 

On choosing Menu Option 2:

selection = 2

 

 

 

On choosing Menu Option 3:

selection =3

 

Check if the user has already 4 records. If yes, call display() to display the records and the donated total.

 

If not, get the donor name, donation amount and donation date from user and validate the same. Prompt user to either continue entering records or stop entering records.

 

 

Call the help() function to display he records entered so far as well as the donated total.

 

Break the while loop and discontinue asking any more choices from the user.

Display Records and the donated total.

 

 

 

If user chooses to continue entering records, prompt him to enter name, donation amount and donation date. If user choose to stop entering records, display the records entered so far and the donated total.

 

Display help section.

 

 

 

 

Close the application.

 

Various data types and data structure have been used for implementing this system. The use of the structure data structure is prominent. It has been used to hold donation records since each donation record contains values of different types; the donor name is a char array, the donation amount is a decimal, and the date field is split into three integer parts. Apart from the struct data structure, various other data types have been used to implement the system. Below is the data dictionary that lists and describes each datatype and its purpose.

 

 

Data Dictionary
Sr. No Variable Name Datatype Size Description
1 d struct 4 To store records of donation
2 name char array

(part of the donor data structure)

100 Stores the name of the donor
3 amount float

(part of the donor data structure)

NA Stores the donation amount
4 day int

(part of the donor data structure)

NA Stores the day of donation
5 month int

(part of the donor data structure)

NA Stores the month of donation
6 year int

(part of the donor data structure)

NA Stores the year of donation
7 selection int NA Stores the menu selection of user
8 struct_index int NA Used to loop over the donor data structure
9 choice int NA Stores the choice whether user wants to continue entering records or not
10 donar_name char array 100 Used to accept donor name from user and copy it to the name variable of donor data structure.
11 total float NA Used to store the donation total.

 

 

 

 

Flowchart for the Programming Solution

The following flowchart represents the flow of the programming solution implemented to design the system.

 

 

 

 

 

 

 

 

 

YES
YES
NO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program Solution Implementation

Following is the complete program code in C language. Various features of the C language including control structures such as do..while loop, for loop, if..else as well as switch have been used to develop the programming solution.

#include<stdio.h>

#include<string.h>

 

/* structure declaration */

struct donor{

char name[100];

float amount;

int day;

int month;

int year;

};

 

/* display() function definition */

void display(struct donor *donors, int len){

 

float total=0;

 

/* below code iterates over the sructure array and displays

the records entered so far as well as the order total */

printf(“\n\t=====================”);

printf(“\n\t\tSUMMARY REPORT”);

printf(“\n\tYOU ENTERED THE FOLLOWING DATA====================”);

printf(“\n”);

for (int i=0; i<len; i++){

printf(“\n%s gave \u20AC%.2f on %d-%d-%d”,donors[i].name, donors[i].amount, donors[i].day, donors[i].month, donors[i].year);

printf(“\n”);

total=total+donors[i].amount;

}

printf(“\nThe total amount is \u20AC%.2f”, total);

}

 

/* help() function definition */

void help(){

 

/* below code guides the user on using the system */

printf(“\n================= Help on Using the System =================”);

printf(“\n”);

printf(“\nAt the start, a menu is displayed showing 3 options to the user.”);

printf(“\nUser can chose option 1 by entering the number 1 on the console.”);

printf(“\nThis option is to be used if user wants to enter donor details.”);

printf(“\nOnce the user selects option 1, he is prompted to enter the donor name.”);

printf(“\nNext, user is prompted to enter the donation amount.”);

printf(“\nAfter this, user is prompted to enter the donation date. The donation date is to be entered by entering the day, month and year in numeric form.”);

printf(“\nThe day should be between 1 and 31, month should be between 1 and 12 and year should be either 2012 or 2013.”);

printf(“\nIf either of these conditions are not fulfilled, user is prompted to enter the date details again till a valid input is provided.”);

 

printf(“\n”);

printf(“\n============”);

printf(“\n”);

printf(“\nThe system then asks the user if he wants to continue entering donation records.”);

printf(“\nUser is required to enter 1 to continue and 0 to discontinue.”);

printf(“\nIf the user enters 1, he is again asked to enter the donor name, donation amount and donation date.”);

printf(“\nIf the user enters 0, the donation records entered so far and the donated total are displayed.”);

printf(“\nUser can enter a maximum of 4 records, after which system will not accepts more records.”);

printf(“\nIf the system already contains 4 records, and the user enters 1 to continue entering more records, he is simply displayed the already existing records and the donated total.”);

printf(“\n”);

 

printf(“\n”);

printf(“\n============”);

printf(“\n”);

printf(“\nComing back to the main menu, user can enter 2 to view the help on using the system.”);

printf(“\nOption 3 is used to exit from the system at any time.”);

printf(“\nUser should keep in mind that if the system already contains 4 records, and the user enters 1 to enter more records, he is informed that he cannot enter more records and is simply displayed the already existing records and the donated total.”);

printf(“\n”);

printf(“\n================= End of Help Section =================”);

}

 

/* main() function starts here */

int main(void)

{

/* declaration of function variables */

int selection;

int struct_index=0, i, choice;

struct donor d[4];                             /* structre arrary declaration */

char donar_name[100];

 

/* displaying program details to user */

printf(“=====================Program Instructions=====================\n”);

printf(“This program allows you to enter upto 4 donor names and amounts.\n”);

printf(“It then prints a summary report of the donations, and the total amount donated\n”);

printf(“==============================================================”);

 

/* do.. while to display menu to user and

prompting user to enter his selction till user selects to exit */

do

{

printf(“\n\nPlease select your menu option”);

printf(“\n\nMENU”);

printf(“\n=====”);

printf(“\n\n1. Enter donor names and print summary report”);

printf(“\n2. Help”);

printf(“\n3. Quit”);

 

printf(“\nEnter a Selection ==> “);

scanf(“%d”, &selection);

 

/* switch case to navigate control to the appropriate code as per user selection */

switch(selection) {

 

/* code to execute if user selects 1 and chooses too enter donation records */

case 1:

if (struct_index>=4){        /* check if already 4 records exists*/

printf(“\nCannot add more than 4 donors;”);

display(d,4);

}

else{

 

do {

/* accept donation details */

printf(“\nEnter the donor’s first and last names separated by space: “);

scanf(” %[^\n]s”,donar_name);

strcpy(d[struct_index].name, donar_name);

printf(“\nEnter amount: “);

scanf(“%f”,&d[struct_index].amount);

printf(“\nEnter Date: “);

 

do{

printf(“\nDay <1 – 31>: “);

scanf(“%d”,&d[struct_index].day);

/*check if day is valid*/

if(d[struct_index].day>=1 && d[struct_index].day<=31)

break;

else

printf(“\nPlease enter a valid date between 1 and 31”);

}while(d[struct_index].day<1 || d[struct_index].day>31);

 

do{

printf(“\nMonth <1 – 12>: “);

scanf(“%d”,&d[struct_index].month);

/*check if month is valid*/

if(d[struct_index].month>=1 && d[struct_index].month<=12)

break;

else

printf(“\nPlease enter a valid month between 1 and 12”);

}while(d[struct_index].month<1 || d[struct_index].month>12);

 

do{

printf(“\nYear: “);

scanf(“%d”,&d[struct_index].year);

/*check if year is valid*/

if(d[struct_index].year>=2012 && d[struct_index].year<=2013)

break;

else

printf(“\nPlease enter a year between 2012 and 2013”);

}while(d[struct_index].year<2012 || d[struct_index].year>2013);

 

/* increment counter to move to next location in struct array*/

struct_index++;

 

/* prompt user if he wishes to continue entering records */

printf(“\nDo you wish to continue? <0=NO/1=YES>: “);

scanf(“%d”, &choice);

 

if (choice==0){

display(d,struct_index); /* display records summary if choice is zero*/

break;

}

}while (choice==1 && struct_index<4); /* Accept donation recorsd if user choice

is one & number of records is less than 4*/

 

if (struct_index>=4){

printf(“\nCannot add more than 4 donors”);

display(d,4);}

}

break;

 

/* code to execute if user selects 2 to display help*/

case 2:

help(); /* call function help() */

break;

 

/* code to execute if user selects 3 to quit the application */

case 3:

break;

/* code to execute if user makes a wrong choice*/

default:

printf(“\nInvalid Choice, Try Again…”);

break;

}

} while (selection!=3);

return 0;

}

/*end of program */

 

Programming Solution Test

The program was tested once the coding was complete and the following errors were identified and fixed.

Syntax Errors

While calling the function display(), it was wrongly spelt as “dispaly” owing to which the program did not compile and threw an exception during compile time. A snapshot of the compilation error is shown below. The function name was later spelled correctly.

 

 

Logical Errors

Once again during a function call to the display() function, a required parameter was not passed. The function display() requires a parameter of datatype “int” so that it could correctly iterate over the number of records in the structure array to print the records. During one of the function calls, the parameter was not passed, which resulted in an error as shown in the compilation snapshot below. The function code was later changed to include the missing parameter.

Run-Time Errors

While printing the records for donation using the display function, the formatter for the amount variable used was integer instead of float. This resulted in the amount being displayed incorrectly during run time as shown in the snapshot below. The format specifier was later changed from %d to %.2f.

Test Cases

Various test cases were developed to check if the program was running properly with different types of inputs. The following test cases were tested and the expected and actual results were summarized.

Test Case 1
Variables and Test Values Expected Output Actual Output
selection =1

name = George Shaw

amount = 100.50

day = 2

month = 3

year = 2015

Please enter a year between 2012 and 2013 Please enter a year between 2012 and 2013

 

Snapshot of test case 1 output.

 

Test Case 2
Variables and Test Values Expected Output Actual Output
selection =1

name = Mary Kane

amount = 50.80

day = 32

Please enter a valid date between 1 and 31. Please enter a valid date between 1 and 31.

 

Snapshot of test case 2 output.

 

Test Case 3
Variables and Test Values Expected Output Actual Output
selection =1

name = John Cooper

amount = 100

day = 15

month = 3

year = 2012

 

choice=1

name = Martha Marshall

amount = 50.80

day = 22

month = 9

year = 2012

 

choice=1

name = Joey Penn

amount = 133.50

day = 18

month = 5

year = 2013

 

choice=1

name = Sheila Debb

amount = 90.50

day = 3

month = 11

year = 2013

 

choice=1

Cannot add more than 4 donors

You entered the following data.

 

John Cooper gave Ç100.00 on 15-3-2012

 

Martha Marshall gave Ç50.80 on 22-9-2012

 

Joey Penn gave Ç133.50 on 18-5-2013

 

Sheila Debb gave Ç90.50 on 3-11-2013

 

The total amount is Ç374.80

 

Cannot add more than 4 donors

You entered the following data.

 

John Cooper gave Ç100.00 on 15-3-2012

 

Martha Marshall gave Ç50.80 on 22-9-2012

 

Joey Penn gave Ç133.50 on 18-5-2013

 

Sheila Debb gave Ç90.50 on 3-11-2013

 

The total amount is Ç374.80

 

 

Snapshot of test case 3 output.

 

 

The above test cases indicate that the program is running and functioning as desired. It is performing as desired. Although, the program can be improved by making the following changes.

  • Program can accommodate more records.
  • Menu option to display records can be added so that user can view entered records at any time.
  • Menu option to delete records can be added.
  • Menu option to modify records can be added.

 

The above suggestions can be implemented and the system can be tuned to perform in a more efficient and useful manner by increasing its utility.

 

 

 

 

References

Abbas, M. (2014, October 20). Programming. Retrieved July 25, 2015, from http://shaeelmbroprogramming.blogspot.in/2014/10/key-features-of-procedural-programs.html

 

C Programming: Programming language paradigms. (2015, June 9). Retrieved July 25, 2015, from https://en.wikibooks.org/wiki/C++_Programming/Programming_Languages/Paradigms

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: