Assessment of Three Programs
  • Category: Information Science and Technology

The following report presents an assessment of three programs, along with flowcharts and outputs. The first assessed program (program 1) prompts the user to enter two numbers and an arithmetic operator (+, -, *, or /), calculates the result, and displays it to the user. The flowchart for this program is shown in figure 3, and the four possible outputs are presented in figure 4.

The second assessed program (program 2) encodes a message using a simple substitution cipher. The program allows the user to specify a key (i.e., the number of positions to shift each letter in the alphabet), and then encodes the user's message using the modified alphabet. The flowchart for this program is presented in figure 6, and a sample output is shown in figure 7.

The third assessed program (program 3) determines the sum and average of a series of numbers entered by the user. The flowchart for this program is given in figure 9, and a sample output is displayed in figure 10.

The code for program 1 is presented below. After debugging the code, it was found that the line with the operation choice was assigning num1 to 0, so to prevent this, the numbers are switched so that after being assigned 0, num1 can be re-assigned by the user.

Program 1 code:

```

#include

int main () {

int (num1);

int (num2);

float results[4] = {0, 1, 2, 3};

char chr[4] = {'+', '-', '*', '/' }, (oper);

oper == '+', '-', '*', '/';

printf("Enter number:");

scanf("%d", &num2);

printf("Enter operation ( '+', '-', '*', '/' ):");

scanf("%s", &oper);

printf("Enter number:");

scanf("%d", &num1);

//calculating sum

results[0] = num2 + num1;

results[1] = num2 - num1;

results[2] = num2 * num1;

results[3] = (float) num2 / num1;

if (oper == '+')

{

printf(" %d\t %c\t %d\t =%.2f\n",(num2),(chr[0]),(num1),(results[0]));

}

else if (oper == '-')

{

printf(" %d\t %c\t %d\t =%.2f\n",(num2),(chr[1]),(num1),(results[1]));

}

else if (oper == '*')

{

printf(" %d\t %c\t %d\t =%.2f\n",(num2),(chr[2]),(num1),(results[2]));

}

else if (oper == '/')

{

printf(" %d\t %c\t %d\t =%.2f\n",(num2),(chr[3]),(num1),(results[3]));

}

return 0;

}

```

The code for program 2 is presented below. This program encodes a message using a simple substitution cipher. The key is used to shift each letter in the alphabet by the specified number of positions, and then the encoded message is displayed to the user.

Program 2 code:

```

#include

int main() {

//key used for change in positions for alphabet

//using size to help rearrange alpha2 which is the encoded alphabet

int i, key, ans;

char alpha[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

char alpha2[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int size = sizeof(alpha2)/sizeof(alpha2[0]);

char message[100], temp;

//temp used here to swap element positions

int temp2;

printf("Please use CAPITAL LETTERS\n");

//fgets used here instead of gets as it has no way to set a data limit

printf("Message to be encoded:");

```

The code for program 3 is not provided in the original text, but the flowchart and a sample output are presented. Therefore, no code rewriting is necessary for program 3.

To initiate the encoding process, the user is required to input the key value. This is achieved using printf("Enter key:"); followed by scanf("%d", &key);.

The encoding process involves the use of a temporary variable named 'temp' which acts as a bridge between the key value and the message[i]. The encoding process is implemented using a for loop, where for(i = 0; message[i]; ++i), the value of temp is set as message[i]. It is then checked whether temp is between 'A' and 'Z', using an if statement. If the condition is true, the next if statement checks whether the value of temp is greater than 'Z'. If it is, the value of temp is then calculated using the formula temp = temp - 'Z' + 'A' - 1.

Similarly, if the value of temp is less than 'A', the value of temp is recalculated using the formula temp = temp + 'Z' - 'A' + 1. The final value of temp is then assigned to message[i], concluding the encoding process.

After the encoding process is completed, the characters in the array alpha[] are printed using a for loop. The array is then shifted by the value of key using a while loop. Here, the value of temp2 is assigned as alpha2[size - 1]. A for loop is then used to shift the values of alpha2[i] by one position. After the loop is completed, temp2 is added at the beginning of the array since it contains the last value. This process is carried out until the value of key is greater than zero.

The final output is displayed using the printf() function which prints the code in message[] along with the arrays alpha[] and alpha2[].

In the second program, the score details for six different football teams are stored in four separate arrays - Played[], GoalsH[], GoalsA[] and Points[]. Here, Played[] keeps track of the number of games played by each team, GoalsH[] stores the total number of goals scored by each team, GoalsA[] stores the total number of goals scored against each team and Points[] stores the total points scored by each team.

To input the score details, a while loop with a true condition is implemented. This infinite loop is only broken when the input values meet specific conditions. The user is first prompted to enter the home team number and away team number within the range of 1 to 6, where a condition check is performed to ensure that both input values are different from each other and lie within the given range.

Next, the user is prompted to enter the number of goals scored by the home team and away team. Here, another condition check is performed to ensure that both input values lie above or equal to zero.

Finally, the score details are stored in the corresponding arrays using addition assignment. This completes the program which calculates and stores the scores of six football teams.

In laboratory 1, we started with a basic calculator that had individual value declarations and display of all operations. Later, we used arrays to store all results and added the option of selecting one operation through char and result arrays, along with if and else if loops.

Moving on to laboratory 2, the program begins by displaying the alphabet and the shifted alphabet, achieved through temp2 and left shifting the alphabet by the key. Next, we encoded the user input message using fgets instead of gets due to its data limit. We used a temp variable between the message[i] and key as it varied and set limits for temp between A and Z if it exceeded these bounds.

Laboratory 3 suggests using customised functions to simplify the program. For instance, GetInt() could be used to limit the team numbers between 1-6. Additionally, AddNumbers(int a, int b) would help tally the table’s score. This would be useful for computing the sum of the table values in the output.

The original program, without any custom functions, used nested while loops and conditions to determine if the user wished to end the program. We employed infinite while loops for inputting teams and scores and imposed limits manually instead of using an array to keep team integers between 1-6. For the table, we used an add and assignment operator for adding up game scores. The points system relied on if and else if conditions and an assignment operator for different win or draw cases. To print out the table, we used printf with increment i values up to 6 to generate multiple rows for each team, with values aligned with the corresponding titles. Lastly, we added another while loop and printf for scenarios when the user wishes to end the program.

Accessed on May 30, 2022, via the online compiler https://www.onlinegdb.com/online_c_compiler#.

Continue by Your Own
Share This Sample