Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ ***// on the problems, if I use the term \"C-type string\" it means an array

ID: 3837745 • Letter: C

Question

C++

***//   on the problems, if I use the term "C-type string" it means an array of type char which will

   //   have an end-of-string mark ('') as the last item in the array

   //   If I require that you use a particular structure in one of the problems, I will give you an

   //   electronic copy of that structure so you do not have to retype it.***

1) //   create a structure named Date to represent a date with whole numbers for the month, day, and

   //   year

2) //   create a structure to hold a person's name with a first name being a c-type string holding 20 printable characters, middle name being an initial, and last name a c-type also string holding 30 printable characters.

   3) //   create a structure to hold a person's name with the three parts of first, middle, and last

   //   each being a c-type string that could change in size

4) //   write a function named CountVowels which has one parameter (a c-type string) and returns the

   //   number of vowels (a, e, i, o, u) in the string of characters.

5) //   write a function named ReArrange which has two parameters, both c-type strings. The first

   //   parameter is a first name, second parameter is a last name. the function returns a

   //   c-type string that contains the last name, a comma, a space, and the first name combined.

   //   this string will be created dynamically. So that if the first and last names are

   //   "Bullwinkle" and "Moose", the c-type string returned is "Moose, Bullwinkle"

6) //   write a function called AvgValues. This function asks the user how many doubles they wish to

   //   average, then reads in that many doubles, and returns the average back to main as a double

7) //   write a function called GCD (Greatest Common Divisor) which has two integer parameters.

   //   the function will return the largest integer that divides evenly into both of the parameters.

Explanation / Answer

Hello,
The program is pretty self explanatory with comments.
Regarding variable char array: it could be done in 2 ways
   1. using string (pretty easy)
   2. vector char array
I have given both. you can choose the one suitable

Please reply back if you have any queries regarding this. Hope this helps.
------------------------------------------------------------------
main.cpp
------------------------------------------------------------------
#include <iostream>
#include<vector>
#include <string.h>
#include <iterator>

struct Date{
   int day;
   int month;
   int year;
};

struct pName{
   char firstName[20];
   char middleName;
   char lastName[30];
};

/*since it is mentioned that the names could change size,
   1.   string could be used.
   2. inorder to declare a variable char array, vector can be used
   will use both and show here
*/

//using char vector
struct pNameVarUsingVector{
   std::vector<char> firstName;
   std::vector<char> middleName;
   std::vector<char> lastName;
};


//using string
struct pNameVar{
   std::string firstName;
   std::string middleName;
   std::string lastName;
};

//function to find no of vowels
int CountVowels(char name[]){
   int vowelCount = 0;
   for(int i = 0; name[i] != ''; i++){
       //check if uppercase/lowercase char equals any of vowels
       if(name[i] == 'a' || name[i] == 'A' ||name[i] == 'e' ||name[i] == 'E' ||
       name[i] == 'i' ||name[i] == 'I' ||name[i] == 'o' ||name[i] == 'O' ||
       name[i] == 'u' ||name[i] == 'U'
       ){
           //if vowel, count that
           vowelCount++;
       }
   }
   return vowelCount;
}

/**
best way to return a char array is using
**/
std::string ReArrange(char firstName[], char lastName[]){
   return std::string(lastName +std::string(", ")+ firstName);
}
/**
method to get user input and average that
**/
double AvgValues(){
   double avgSum = 0, inputNum;
   int noOfElements, i = 1;
   std::cout<<"Enter number of elements you wish to average"<<std::endl;
   std::cin>>noOfElements;
   while(i<= noOfElements){
       std::cout<<"Enter the element "<<i<<" : ";
       std::cin>>inputNum;
       avgSum+=inputNum;
       i++;
   }
   if(avgSum > 0){
       return avgSum/noOfElements;
   }
  
   return avgSum;
  
}

/**
this problem can be done in two ways.
1. for loop (as we have done here)
2. Recursive method (which is the commented portion.)
You can choose to use whichever method you want
**/
int GCD(int a, int b){
   int gcd = 0;
   if(a==0 || b==0)
       return gcd;
   if(a==b)
       return a;
   for(int i =1; i <= a && i <= b; i++){
       if(a%i==0 && b%i== 0)
           gcd = i;
   }
  
   /*recursion
   if(a>b)
       return GCD(a-b,b)
   return GCD(a,b-a);
   */
   return gcd;
}
int main(int argc, char** argv) {
   //using date structure
   struct Date newDate;
   newDate.day = 01;
   newDate.month = 05;
   newDate.year = 2017;
   std::cout<<"The date is "<<newDate.day<<"/"<<newDate.month<<"/"<<newDate.year<<std::endl;
  
   //fixed name lenth
   struct pName pnameStruct;
   strcpy(pnameStruct.firstName,"John");
   pnameStruct.middleName = 'K';
   strcpy(pnameStruct.lastName, "Doe");
   std::cout<<"The name is "<<pnameStruct.firstName<<" "<<pnameStruct.middleName<<" "<<pnameStruct.lastName<<std::endl;
  
   //variable name length using vector
   struct pNameVarUsingVector pVarStructVector;
   //store value to variable
   std::string str = "John1";
   std::copy(str.begin(), str.end(), std::back_inserter(pVarStructVector.firstName));
   str= "MiddleName1";
   std::copy(str.begin(), str.end(), std::back_inserter(pVarStructVector.middleName));
   str= "LastName1";
   std::copy(str.begin(), str.end(), std::back_inserter(pVarStructVector.lastName));  
   std::copy(pVarStructVector.firstName.begin(), pVarStructVector.firstName.end(),std::ostream_iterator<char>(std::cout));
   std::cout<<" ";
   std::copy(pVarStructVector.middleName.begin(), pVarStructVector.middleName.end(),std::ostream_iterator<char>(std::cout));
   std::cout<<" ";
   std::copy(pVarStructVector.lastName.begin(), pVarStructVector.lastName.end(),std::ostream_iterator<char>(std::cout));
   std::cout<<std::endl;
  
   //variable name lengths using string
   struct pNameVar pVarStruct;
   pVarStruct.firstName = "First";
   pVarStruct.middleName = "Middle";
   pVarStruct.lastName = "Last";
   std::cout<<"The name is "<<pVarStruct.firstName<<" "<<pVarStruct.middleName<<" "<<pVarStruct.lastName<<std::endl;
  
   char newChar[50] = "Text to find no of vowels";
   //using the countVowels method
   std::cout<<std::endl<<CountVowels(newChar)<<std::endl;
  
   //using ReArrange method
   std::cout<<ReArrange("John","Doe")<<std::endl;
  
   //using AvgValue
   std::cout<<std::endl<<"average is = "<<AvgValues();
  
   //using GCD
   std::cout<<std::endl<<"GCD of 6,2 = "<<GCD(6,2);
   return 0;
}