In: Computer Science
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
// 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: