Question

In: Computer Science

Never declare a C++ global variable in this class. But global constants are ok. unsigned Size1...

Never declare a C++ global variable in this class. But global constants are ok.

unsigned Size1 = 100;

// never do this at global scope

const unsigned Size2 = 120;

// this is ok (but probably not very helpful)

Don't use the string class and don't use system functions for math (like pow) or strings (like strlen).

These functions are meant to be short and simple; if you're writing something long and complicated,

look for a simpler solution.

void countdown( unsigned seconds );

If seconds is 5, countdown will output

5...

4...

3...

2...

1...

Blast off!

and likewise for other values of seconds.

It's not necessary to program in the effect of waiting for one second between outputs, but if you want

to do that and if you're working in a Windows environment, you can #include <windows.h> and call

Sleep( millis ) to pause for millis milliseconds. e.g.,

#include <iostream>

#include <windows.h>

using namespace std;

int main(){

cout <<"The name is Bond...";

Sleep(1000); // pause one second for dramatic effect

cout <<"James Bond\n";

} // main

unsigned add(unsigned a, unsigned b);

To make this work, you're allowed to use the increment operators and the decrement operators.

You're allowed to use the conditional operator and the relational operators, but not ordinary

arithmetic operators like addition, subtraction, multiplication, division, or mod. The idea is to

increment b and decrement a until a is zero, and then return b.

For example, add(3, 7) should return 10.

unsigned multiply(unsigned a, unsigned b );

To make this work, you're allowed to call your add function, and use the increment and decrement

operators, but not to use + - * / % directly. You can add a to itself b times to get the answer.

For example, multiply(3, 7 ) should return 21.

unsigned power( unsigned a, unsigned b );

To make this work, you're allowed to call your multiply function, and use the increment and

decrement operators, but not to use + - * / % directly. You can multiply a by itself b times to get the

answer.

For example, power(3, 7) should return 2187

Don't worry about the possibility of overflow, and don't worry about efficiency.

void output( const char * s );

Assume (without checking) that s is a null-terminated C-string. Output the chars in s, one per line (not

including the terminating null char)

For example, output("blah") should output

b

l

a

h

unsigned length( const char * s );

Assume without checking that s is a null-terminated C-string. Return the number of chars in s, not

including the terminating null char.

For example, length("blah") should return 4

unsigned howMany( char c, const const * s );

Assume without checking that s is a null-terminated C-string. Return the number of times that char c

appears in s before s's terminating null char.

void reverse( char * s );

Assume without checking that s is a null-terminated C-string. Reverse the order of the chars before

the terminating null char.

For example, if main said

char buff[] = "blah"; // array has 5 chars

reverse( buff );

cout <<buff;

then the program should output halb

void copy( char * destination, const char * source );

Assume without checking that source is a null-terminated C-string; further assume that destination is

an array big enough to hold the source string (including the terminating null char). Copy the C-string

(including the terminating null char, but nothing after the terminating null char) from source to

destination.

For example, if main said

char s[] = "blah";

char d[ 1000 ]; // 995 bytes bigger than we need

copy( d, s );

cout <<d;

then the program should output blah

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <iostream>

#include <windows.h>

using namespace std;

//this function waits for seconds and then output blast off

void countdown(unsigned seconds)

{

while (seconds != 0)

{

cout << seconds << "...\n";

seconds--;

Sleep(1000);

}

cout << "Blast off!\n";

}

//this function returns addition of a and b

unsigned add(unsigned a, unsigned b)

{

while (a != 0)

{

a--;

b++;

}

return b;

}

//this function returns multiplication a and b

unsigned multiply(unsigned a, unsigned b)

{

unsigned ans = 0;

while (b != 0)

{

ans = add(ans, a);

b--;

}

return ans;

}

//this function returns a to the power b

unsigned power(unsigned a, unsigned b)

{

unsigned ans = 1;

while (b != 0)

{

ans = multiply(ans, a);

b--;

}

return ans;

}

//this function output characters of given string one per line

void output(const char *s)

{

int i = 0;

while (s[i] != '\0')

{

cout << s[i] << endl;

i++;

}

}

//this function returns length of given string

unsigned length(const char *s)

{

unsigned len = 0;

while (s[len] != '\0')

{

len++;

}

return len;

}

//this function returns count of given character in given string

unsigned howMany(char c, const char *s)

{

int count = 0, i = 0;

while (s[i] != '\0')

{

if (s[i] == c)

count++;

i++;

}

return count;

}

//this function reverses given string

void reverse(char *s)

{

int len = length(s);

for (int i = 0; i < len / 2; i++)

{

char temp = s[i];

s[i] = s[len - i - 1];

s[len - i - 1] = temp;

}

}

//this function copies source string to destination string

void copy(char *destination, const char *source)

{

int i = 0;

while (source[i] != '\0')

{

destination[i] = source[i];

i++;

}

destination[i] = '\0';

}

int main()

{

countdown(5);

cout << "add(3,7): " << add(3, 7) << endl;

cout << "multiply(3,7): " << multiply(3, 7) << endl;

cout << "Power(3,7): " << power(3, 7) << endl;

output("blah");

cout << "length('blah'): " << length("blah") << endl;

cout << "howMany('a','radio'): " << howMany('a', "radio") << endl;

char buff[] = "blah";

reverse(buff);

cout << "reverse(buff): " << buff << endl;

char s[] = "blah";

char d[1000];

copy(d, s);

cout << "copy(d,s): " << d;

}

Output:


Related Solutions

How does one declare a class variable in C#?
How does one declare a class variable in C#?
Declare a string variable and initialize it with your first name ( in C++)
Declare a string variable and initialize it with your first name ( in C++)
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class...
Step 1: Player Variable Declaration and Initialization Part A: In the Player class, declare two class variables, map and guess that are two dimensional arrays of ints. Part B: In the constructor of the Player class, initialize the map and guess arrays with the appropriate size: map: 10 by 10 (10 outer arrays with 10 elements each) guess: 5 by 2 (5 outer arrays with 2 elements each) Step 2: Player.readPlayerSheet() Part A The readPlayerSheet method in the Player class...
Write a program in c# that declare a variable and store user name jjohn in. Then,...
Write a program in c# that declare a variable and store user name jjohn in. Then, write a statement that will make your program display at the screen the content of that variable, followed by " I would like to know your height in centimeter. Please enter it:". Then, write a statement that will store the value entered by the user, allowing decimal numbers ie some precision, a fraction part. Finally, write statements (more than one can be needed) so...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i //...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values ============================================= 2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers...
swift language declare a Swift array variable that can hold instances of any class type
swift language declare a Swift array variable that can hold instances of any class type
Complete the following using c++ (a) Declare a class called Capacitor representing the electronic component capacitor,...
Complete the following using c++ (a) Declare a class called Capacitor representing the electronic component capacitor, which has the following characteristics: Capacitance in Farads (example value: 0.47 mF) Voltage rating in Volts (example value: 25.0 V) Temperature rating in Celcius (example value: 85.0 oC) Type ("Paper", "Polyester", "Electrolytic", "Ceramic"). These attributes should be represented as the data members of the class and be hidden from the outside of the class. The class should also have a constructor used to initialise...
Consider the following unsigned decimal constants: 12345, 34567, 9876543. Perform the following operations: Note: Do not...
Consider the following unsigned decimal constants: 12345, 34567, 9876543. Perform the following operations: Note: Do not use any pseudo instructions to do so. (a) Write MIPS instruction(s) to store each constant in temporary registers(i.e., any of $t0, $t1, . . . $t7) (b) Write MIPS instruction(s) to perform an addition operation of the given numbers with the decimal number 123. Make sure the numbers follow the size limitations of the immediate instruction type.
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions....
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions. Please see Q2 and Q3 for the name of the second and third function. (5 points) Write a function string reverseString that reverses a string (first member function). It takes a string parameter and returns its reversed version. (10 points) 1b. In the class ReverseUniverse in Q1. Write a function vector reverseVector that reverses a vector (second member function). It takes a double vector...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT