Solution
Sanskar answered on
Jun 09 2023
CSC – Final
Assignment 1 Solution-:
*
* File: main.cpp
* Author: YOUR NAME HERE
* Created on DATE AND TIME HERE
* Purpose: Input something, output it reversed with some modifications
* Note:Range should be 5 digits, 321 = 00321 reverse = 12300 before subtraction
* 12300 - 999 = 11301 after subtraction
* 12300 = 00321 = 321 after reversal and no subtraction
*
System Li
aries Here
#include cin,cout,endl
#include
strlen()
using namespace std;
User Li
aries Here
Global Constants Only, No Global Variables
Like PI, e, Gravity, or conversions
Function Prototypes Here
ool inRange(const char [],unsigned short &);
Output true,unsigned or false
ool reverse(unsigned short,signed short &);
Output true,short or false
short subtrct(signed short,int);
ool inRange(const char digits[], unsigned short& num) {
int len = strlen(digits);
if (len > 5)
return false;
Check if all characters are digits
for (int i = 0; i < len; i++)
{
if (digits[i] < '0' || digits[i] > '9')
return false;
}
Convert the digits to unsigned short
num = 0;
for (int i = 0; i < len; i++)
{
num = num * 10 + (digits[i] - '0');
}
return true;
}
ool reverse(unsigned short num, signed short& reversed) {
signed short temp = 0;
while (num > 0)
{
temp = temp * 10 + (num % 10);
num /= 10;
}
if (temp < -32768 || temp > 32767)
Range of signed short is -32768 to 32767
return false;
reversed = temp;
return true;
}
short subtrct(signed short num, int subtract) {
if (num >= subtract) {
num = num - subtract;
return num;
}
else {
return num;
}
}
Program Execution Begins Here
int main(int argc, char** argv) {
Declare all Variables Here
const int SIZE=80;
More than enough
char digits[SIZE];
Character digits or not
unsigned short unShort;
Unsigned short
short snShort;
Signed short
Input or initialize values Here
cout
"Reverse a number and subtract if possible."
endl;
cout
"Input a number in the range of an unsigned short"
endl;
cin
digits;
Test if it is in the Range of an unsigned short
if(!inRange(digits,unShort)){
cout
"No Conversion Possible"
endl;
return 0;
}
Reverse and see if it falls in the range of an signed short
if(!reverse(unShort,snShort)){
cout
"No Conversion Possible"
endl;
return 0;
}
Now subtract if the result is not negative else don't subtract
snShort=subtrct(snShort,999);
Output the result
cout
snShort
endl;
Exit
return 0;
}
Assignment 3 Solution -:
*
* File: main.cpp
* Author: YOUR NAME HERE
* Created on DATE AND TIME HERE
* Purpose: Sorting a 2-D a
ay of characters if row and columns match
*
System Li
aries Here
#include cin,cout
#include
strlen(),strcmp(),strcpy()
using namespace std;
User Li
aries Here
Global Constants Only, No Global Variables
Allowed like PI, e, Gravity, conversions, a
ay dimensions necessary
const int COLMAX=80;
Only 20 required, and 1 for null terminato
Function Prototypes...