Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Aliens from the planet Floopnagle have an unusual system for representing positi

ID: 3863976 • Letter: A

Question

Aliens from the planet Floopnagle have an unusual system for representing positive integers (they do not appear to have any concept of negative numbers or real numbers). All numbers are represented as a combination of three symbols: {, }, and;. Every Floopnagleian number begins with a left curly brace ({). Curly braces always occur in matched pairs, and may be nested. Each level of nested curly braces represents multiplication by 4. Semicolons may appear (in sequences of one or more) inside sets of curly braces or immediately following a right curly brace, and individually represent values of 1 (so, for example, ;; represents the value 2). For example, {;;} represents the value 8 (the two semicolons count as 2, and the curly braces multiply that value by 4). {;};;; represents 7 ({;} becomes 4, and the three trailing semicolons each add 1). {{;};}; represents 21 (the innermost curly braces represent 4, plus 1, times 4 again, plus 1 again). Write a C program that prompts the user to enter a number in this brace notation, and prints out its "normal" (base 10) value. We haven't covered strings yet, and you don't know how long the input will be, so use a while loop with getchar () to read in characters from the input, one at a time, until you read a newline (' ') character. You may assume that the input only contains curly braces and semicolons (except for the final newline), and that they will always be arranged in a legal order (in other words, the first input character will always be a left curly brace, you'll never have two sets of curly braces nested at the same level, and semicolons will never precede a left curly brace). Program Execution Examples Program output is shown in italics; user input is shown in boldface. Sample Execution 1: Please enter an encoded number to process: {};;; This value decodes to 3

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
int main()
{
//Creates a character pointer
char *c = (char *) malloc(sizeof(char));
char ch;
//Initializes the variables
int x, count = 0, res = 0;
//Asks the user to enter the encoded data
printf(" Please enter an encoded number to process: ");
//Loops till Enter key is pressed
while((ch = getchar())!= ' ')
{
//Checks if the character is left curly bracket or right curly bracket or a semicolon it is valid
if(ch == '{' || ch == '}' || ch == ';')
{
//Checks if the count is zero and the character is not left curly bracket it is invalid
//Simply first character must be left curly bracket
if(count == 0 && ch != '{')
printf(" ERROR: It must be left curly bracket ({)");
//Checks for semicolon cannot appear before the left curly bracket
else if(*(c - 1) == ';' && ch == '{')
printf(" ERROR: Cannot have ; then left curly bracket");
//Otherwise valid character
else
{
//Stores the character
*(c + count) = ch;
//Increase the counter
count++;
}//end of else
}//End of if
}//End of while
//Stores null character at the end
*(c+count) = '';
//Displays the original entered data
printf(" Original String: %s", c);
//Loops till end of entered data
for(x = 0; x < count; x++)
{
//If the we have semicolon and then right curly bracket then
//update the res by increasing the res by 1 and multiply 4 to it
if(*(c+x)==';' && *(c+x+1)=='}')
res = ++res * 4;
//Checks if the data is semicolon then add one to res
else if(*(c+x) == ';')
res += 1;
}//End of for loop
//Displays the result
printf(" This value decodes to = %d", res);
}//End of main

Sample Execution 1:

Please enter an encoded number to process: {};;;

Original String: {};;;
This value decodes to = 3

Sample Execution 2:

Please enter an encoded number to process: {;};;;;

Original String: {;};;;;
This value decodes to = 8

Sample Execution 3:

Please enter an encoded number to process: {{;};};;

Original String: {{;};};;
This value decodes to = 22

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote