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...
you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
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...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART A: (Minusculo Inn) Minusculo Inn has only eight guest rooms: Room Number Amenities 101 1 king size bed 102, 103,104 2 double beds 201, 202 1 queen size bed 203, 204 1 double bed & sofa bed Write a program that asks for the room number and displays the amenities. If the user enters a room number that does...
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
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT