Assignment 8a - Function 1

1.

Define a function draw(row,column,symbol) to display a rectangular pattern of symbols.

Sample Output
Number of rows and columns = 4 6
Symbol = #

#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#

Program Skeleton


void draw(int row, int column, char symbol){
	// ...

}
int main(){

        int col, row;
        char sym;

        cout << "Number of rows and columns = ";
        cin >> row >> col;

        cout << "Symbol = ";
        cin >> sym;

        draw(row, col, sym);

        return 0;
}

   
2.

Write a function prime(x) to check whether the positive integer x is a prime number. If x is prime, true will be returned. Otherwise, false is the return value. Hence, find all the prime numbers between any two positive integers m and n (n>m) by calling the function in the main program.

#include <iostream>
using namespace std;
bool prime(int x)
{  
  ......
}  
   
int main()
{  
  ......
}  
   
3.

factorial is the product of all positive integers less than or equal to a given positive integer. For example, 7! = 1 x 2 x 3 x 4 x 5 x 6 x 7. Write a function to find the factorial of a positive integer n, where n > 1. Then find the factorials of all integer between x and y.

The sample output is shown below

Sample output

x = 0
x = 3
y = 2
y = 5
3! = 6
4! = 24
5! = 120

The outline of the program is shown below

#include <iostream>
using namespace std;

int factorial(int x){
    // ...
}

int main(){
    
    int x,y,i;
    
    do{
        cout << "x = ";
        cin >> x;
    }while(x<1);
    
    do{
        cout << "y = ";
        cin >> y;
    }while(y <= x);
    
    // ...
    
    return 0;
}
   
4. Write a C++ program to search if number x is found in the number array.
Program outline
#include <iostream>
using namespace std;

int numbers[10]={12,5,7,19,4,8,20,17,2,3};
bool searchnumber(int x){
	//....
}
int main()
{
    int x, i;
    
    cout << "x=";
    cin >> x;
    
    if(_________________)
        cout << "Found"<< endl;
    else
        cout << "Not found" << endl;
    
    return 0;
}

   
5. Write a c++ program to count the number of occurance of char x in the string "sentence".
Sample output

x=e
count of e = 4


Program outline
#include <iostream>
using namespace std;

string sentence="Sacred Heart Canossian College";

int countchar(char x){

	// your code

}
int main()
{
    char x;
    
    cout << "x=";
    cin >> x;
    
    cout << "count of " << x << " = " << ______________ << endl;
    return 0;
}
   
  Optional Questions
3. Re-write Assignment 2 Q6 by defining the following functions
- findallowance(income): find the allowance of the tax payer
- findTax(net_chargeable, income): calculate the tax based one the net_chargeable income and the annual income
The main function of the program is given below:
int main ()
{
      string name;
      float income, net_chargeable;
      int more = 1;                             // Boolean
      while (more==1)
      {
              cout << "Name? ";
              cin >> name;
              
              do {
                    cout << "Total annual income? ";
                    cin >> income;
              } while (income < 0);

              net_chargeable = income - findallowance(income);

              cout << "Tax for " << name << " is $" << int(findTax(net_chargeable, income)) << endl;
              do {
                      cout << "Any more (yes=1/no=0)? ";
                      cin >> more;
              } while ((more != 1) && (more != 0));
            
      }
    return 0;
}