Mini Internal Assessment Example

In this post , we observe some code which establishes a menu driven interface and shows we can implement data processing from the menus.

This code will be demonstrated in class.

See the code below:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
/*
Demonstrates a 2 level menu with
a simple data processing cycle

When faced with the main menu, select 1 then 1 again to
execute the process of calculating area.


Author: Vedesh Kungebeharry
*/


//constants
const double Pi=3.14;


//global static  variables
char choice;


//function declarations
void    initialization();
void    showMainMenu();
void    showMathMenu();
void    runMainMenu();
void    runMathMenu();
void    pause();
void    exitProgram();
void    AreaOfCircleUserInteraction();
double  AreaOfCircle(double radius);

//main flow of control from process to process
int main()
{

    initialization();
    runMainMenu();
    exitProgram();

    return 0;
}


/*
*initialize all instance variables and arrays here
*
*/
void initialization()
{
    choice = '_';
}

//displays the main menu and accepts input
void runMainMenu()
{
    int sentinel=0;//used to break  out of our menu loop
    while (sentinel>=0)//loop menu here
    {
        showMainMenu();//display menu text

        choice = getch();//get a character from the input buffer

        system("@cls");// clear screen after getting input

        switch (choice)//based on the choice take the desired action
        {
            case '1':   printf("\nYou chose Option 1: Math Calculations\n");
                        runMathMenu();//displays the math sub menu and accepts input
                        break;
            case '2':   printf("\nYou chose option 2\n");
                        break;
            case '3':   printf("\nYou chose option 3\n");
                        break;
            case 'q':   printf("\nYou chose to quit\n");
                        sentinel=-1;//update the sentinel so that looping will cease
                        break;
            case 'Q':   printf("\nYou chose to quit\n");//cease if upper case
                        sentinel=-1;
                    break;
            default:
                    printf("\nYou have entered an option that is not in the menu\n");
                    break;


        }
        if(sentinel>=0)//if we have continued regular execution , continue looping
        {
            printf("\nreturning to main menu in 2 seconds...\n");
            pause(2000);
            system("@cls");
        }

    }



}

//shows and accepts input for the math sub menu
void runMathMenu()
{
    int sentinel=0;
    while (sentinel>=0)
    {
        showMathMenu();//show options

        choice = getch();//get a character from the input buffer

        system("@cls");

        switch (choice)
        {
            case '1':   printf("\nYou chose option 1: Area\n");
                        AreaOfCircleUserInteraction();//start interaction to calculate area
                        break;
            case '2':   printf("\nYou chose option 2\n");
                        break;
            case 'r':   printf("\nYou chose to quit\n");
                        sentinel=-1;
                        break;
            case 'R':   printf("\nYou chose to quit\n");
                        sentinel=-1;
                        break;
            default:
                    printf("\nYou have entered an option that is not in the menu\n");
                    break;


        }
        printf("\nreturning to Math menu in 2 seconds...\n");
        pause(2000);
        system("@cls");
    }

}

void showMainMenu()
{
    printf("1. Option 1: Math Calculations\n");
    printf("2. Option 2\n");
    printf("3. Option 3\n");
    printf("Q. Quit    \n");
    printf("\nPlease select an option...");
}

void showMathMenu()
{
    printf("1. Area of circle\n");
    printf("2. Volume of Circle\n");
    printf("R. Return to main menu    \n");
    printf("\nPlease select an option...");
}

//pauses execution
void pause(int milliseconds)
{
    clock_t now = clock();
    while(clock()< now+milliseconds);

}
//prompt user to enter data and output results
void AreaOfCircleUserInteraction()
{
    double area,radius=-999.99;
    printf("Enter the radius...");
    scanf(" %lf", &radius);
    area=AreaOfCircle(radius);
    printf("\nThe area of a circle of radius \t%.2lf is \t%.2lf\n\n",radius,area);
    system("pause");

}
//helper function to calculate area
double AreaOfCircle(double radius)
{
    return Pi*radius*radius;
}
//exit gracefully
void exitProgram()
{
    printf("\nExiting program in 2 seconds...\n");
    pause(2000);

}



Updates to this post

28th Sept 2023 – changed “This code will be demonstrated in class tomorrow.” to “This code will be demonstrated in class.”

© 2019  Vedesh Kungebeharry. All rights reserved. 

What is a problem?

A problem is defined as the “difference between the current state of things and a desired future state in an information processing system”

Example


CURRENT STATE:

A score taker for a football game could be using a paper based information processing system consisting of

Hardware:

  • A notebook
  • Pencil

Data management:

  • Ruled lines forming a table in the notebook,
  • Columns for scores, fouls, Events etc
  • Calculations such as cumulative minutes or penalties in the game (these are calculated mentally by the score taker and recorded during the progression of the game.)

Inputs :

  • Goals scored,
  • Scorer’s name
  • Team scoring


  • Fouls
  •  Attacking player name , victim name
  • Minute of foul

Outputs (to officials and score board operator):

  • Scores
  • Fouls
  • Minutes

Desired future state:

Input:

  • Scores along with player’s Number entered manually
  • Fouls with attacker and victim names number manually via keyboard

Processing:

  • Store player’s names from looking up numbers
  • Automatically timestamp goals and fouls

Output:

  • Statistics directly to website and scoreboard.

© 2019  Vedesh Kungebeharry. All rights reserved.

The Importance of algorithms

In our discussion, we consider well defined precise algorithms (in flowcharts or pseudocode) as compared with narrative algorithms.

Some of the reasons why algorithms are beneficial or important to the problem solving process are:

  1. Algorithms serve as a blueprint for other programmers to follow

  2. When problems are complex, an algorithm’s precise structure allows for an accurate development of a solution in parts.  This documentation (the algorithm itself) is useful since it is difficult for  the problem solver to remember many sub-processes , procedures and inputs/outputs to the problem.

  3. Because algorithms are intended for a computer system and programming language, it allows us to design for that particular programming language’s structure and for efficiency ,given    the limitations of system resources.

    For example, an algorithm which processes DNA sequence information for matching DNA strands together (see Longest common sub sequence) can be developed strictly for solving the problem without considering system resources.   In this case the algorithm still resembles programming language. 

    Usually in this type of general solution, array sizes are allowed to be very large or of infinite size. If memory becomes a constraint, we can now modify the algorithm to use say, array sizes at a maximum of  1000 to allow for the intended program to run within the limitation of our system resources.

  4. Algorithms are easily testable when using dry runs and trace tables.

© 2019  Vedesh Kungebeharry. All rights reserved.

Decimal to binary

Task: Create an algorithm which accepts a decimal integer and outputs it’s binary equivalent.

This algorithm was designed and implemented in Flowgorithm:

See the video below for an explanation of how the algorithm works:

Decimal to binary algorithm explained

The code in C is shown below:



Copy and paste from here:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>//library needed for string operations

int main()
{
    //declare and identify integers
    int number, remainder;

    //declare, identify and initialize all strings
    char  result[20]="";
    char  remainderString[20]="";


    //initialize all other variables
    number=-1;
    remainder=-1;

    //get input
    printf("Enter a number in decimal\n");
    scanf("%d", &number);
    printf("\n");

    while (number!=0)
    {
        remainder=number%2;

            /* #Implementation_Result

                Here we implement the following:

                    result=remainder + result
            */

            //first convert the remainder to a string,
            //and store the converted data in "remainderString"
            itoa(remainder,remainderString,10);

            //add "remainderString" to "result" in that order.
            //the result of the concatenation is stored in "remainderString"
            strcat(remainderString,result);

            //now copy our data so far back to "result"
            strcpy(result,remainderString);
            /*End #Implementation_Result*/


        number=number/2;

    }

    printf("The binary Result is %s \n",result);
    return 0;
}

© 2019  Vedesh Kungebeharry. All rights reserved.