C++ Course Learning [6]-Modular Solution of 2-Special Functions

Posted by Tiigeress on Mon, 20 May 2019 06:34:35 +0200

Preface

Learning materials are detailed in the Computer Programming (C++) course of Xi'an Jiaotong University, MOOC, China University. Portal.
This part mainly talks about the modular solution of 2-special functions.

Topics and Solutions

1. Writing Recursive Functions to Find the Maximum Array

Writing a function to find the maximum value of array elements in an array requires that it must be solved recursively.

/*Writing a function to find the maximum value of array elements in an array requires that it must be solved recursively.*/

# include <iostream>

using namespace std;

int GetMax(int num[], int a,int b){  //Finding the Maximum of Items a to b
  int c=num[b];

  if(b == a)  return c;
  else{
    c = (GetMax(num,a,b-1)>c)?GetMax(num,a,b-1):c;
    return c; 
  } 
}

int main(){
  //---define---
  int num[20];
  int n;
  int Max;

  //---input---
  cin>>n;
  for (int i=0;i<n;i++)
  {
    cin>>num[i];
  }

  //---execute---
  Max = GetMax(num, 0, n-1);

  //---output---
  cout<<Max<<endl;

  return 0;
}

2. Writing Inline Functions to Find the Area and Perimeter of Rectangles

Write function to calculate the area and perimeter of rectangle, because the formula is very simple, please use inline function to write, improve the efficiency of program operation.

/*Writing a function to find the maximum value of array elements in an array requires that it must be solved recursively.*/

# include <iostream>

using namespace std;

inline int GetSize(int a, int b){ 
  int size = a*b;
  return size;
}

inline int GetPerimeter(int a, int b){ 
  int perimeter = 2*(a+b);
  return perimeter;
}

int main(){
  //---define---
  int a,b;

  //---input---
  cin>>a;
  cin>>b;

  //---execute---

  //---output---
  cout<<GetSize(a,b)<<" ";
  cout<<GetPerimeter(a,b)<<endl;

  return 0;
}

3. Write overloaded functions to print strings

Write the function print_space to print a string, requiring a space between each letter of the printed string. Two functions with the same name are required, one supporting character array input and the other supporting string type input. Then write the main function to test the two functions, the first using character array input, and the second using string type input.

/*Write the function print_space to print a string, requiring a space between each letter of the printed string.
Two functions with the same name are required, one supporting character array input and the other supporting string type input.
Then write the main function to test the two functions, the first using character array input, and the second using string type input.*/

# include <iostream>
# include <cstring>

using namespace std;

void print_spaced(char word[], int length){
  int i;
  cout<<word[0];
  if(length>1){
    for(i=1;i<length;i++) cout<<" "<<word[i];  
  }

}

void print_spaced(string xword, int count){
  int i;
  int pos = 1;
  if(count>1){ 
  for(i=1;i<count;i++){
    xword.insert(pos," ");
    pos = pos + 2;
  }
  }
  cout<<xword;
}

int main()
{
  //---define---
  int i,length = 0,strlen;
  char word[100];
  string xword;

  //---input---
  cin.getline(word,100);
  getline(cin,xword);

  //---execute---
  for(i=0;word[i]!='\0';i++)  length++;
  print_spaced(word, length);
  cout<<endl;
  strlen = xword.length(); 
  print_spaced(xword, strlen);

  //---output---

  return 0;
}

4. Sorting function overloading

Write a set of overloaded sorting functions, which can sort two integers, three integers, four integers and integer arrays from large to small. The function is called sort. The sorting of arrays should use recursive method, and print function should be added to display the sorted array elements in one line.

/*Writing a function to find the maximum value of array elements in an array requires that it must be solved recursively.*/

# include <iostream>

using namespace std;

void sort(int &a,int &b){
  int tmp;
  if(a<b){
    tmp = b;
    b = a;
    a = tmp;
  }
}

void sort(int &a,int &b,int &c){
  sort(a,b);
  sort(a,c);
  sort(b,c);
}

void sort(int &a,int &b,int &c,int &d){
  sort(b,c,d);
  sort(a,c,d);
  sort(a,b,d);
  sort(a,b,c);
}

void sort(int (&num)[100],int n){
  int i,j,tmp;
  for(i=0;i<n;i++){
    for(j=0;j<n-1;j++){
      if(num[j]<num[j+1]){
        tmp = num[j+1];
        num[j+1] = num[j];
        num[j] = tmp;
      }
    }
  }
}

void print(int num[], int n){
  cout<<num[0];
  for(int i=1;i<n;i++){
    cout<<" "<<num[i];
  }
}

int main()
{
int a,b,c,d;
int data[100];  //array
int k,n,i;
  cin>>k;
  switch(k)
  {
    case 1: //Case 1, Binary sorting
        cin>>a>>b;
        sort(a,b);
        cout<<a<<" "<<b<<endl;
        break;
    case 2: //Case 2, Triple Sort
        cin>>a>>b>>c;
        sort(a,b,c);
        cout<<a<<" "<<b<<" "<<c<<endl;          
        break;      
    case 3: //Case 3, Quaternion Sorting
        cin>>a>>b>>c>>d;
        sort(a,b,c,d);
        cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
        break;  
    case 4: //Case 4, Array Sorting
        cin>>n;
        for(i=0;i<n;i++)
           {
            cin>>data[i];
        } 
        sort(data,n);
        print(data,n);
        break;      
  }
  return 0;
}

5. Write recursive functions to reverse the sequence of strings

Writing functions to output a string in reverse order requires that recursive functions be used.

/*Writing functions to output a string in reverse order requires that recursive functions be used.*/

# include <iostream>

using namespace std;

char tmp;
int i;

void GetSwap(char (&word)[100],int n){  //Inverse operation on the first n numbers
    //1. Inverse order the first n-1 numbers
    //2. Save word[n-1]
    //3. Move the number that has been reversed one bit back (0 to n-2)
    //4. Move word[n-1] to the first place 
  if(n>1){
    GetSwap(word, n-1);
    tmp = word[n-1];
    for(i=n-2;i>=0;i--) word[i+1]=word[i];
    word[0]=tmp;
  }
}

int main()
{
  //---define---
  char word[100];
  int length=0;      

  //---input---
  cin.getline(word,100);

  //---execute---
  for(int i=0;word[i]!='\0';i++){
    length++;
  }
  //Cout < < length > length > endl; 
  GetSwap(word,length);

  //---output---
  for(int i=0;i<length;i++){
    cout<<word[i];
  }
  return 0;
}

Topics: Programming