C++ Rules ***// you might be given a problem that requires writing a function or
ID: 3837752 • Letter: C
Question
C++ Rules ***// you might be given a problem that requires writing a function or might require creating a // structure // if you have to write a function, you do not need to write a prototype to go with it // you do not need to include iostream or do the using namespace std; statement in your function // but if you use any other built-in functions like strlen, you need to include the necessary // include files for them. // on the exam if I use the term "C-type string", it means an array of char with an end-of-string // mark, '', marking the last character in the array*****
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 // 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.
8) // if (N1 > N2) // Smaller = N2; // else // Smaller = N1;
Explanation / Answer
1.
struct Date
{
int month,day,year;
};
2.
struct name
{
char first[20];
char middle;
char last[30];
};
3.
struct name
{
char *first;
char *middle;
char *last;
};
4.
int countVowels( char string1[50])
{
int i,vowel;
vowel = 0;
int len=strlen(string1);
for(i=0;i<len;i++)
{
if(string1[i]=='a' || string1[i]=='A' || string1[i]=='e' || string1[i]=='E' || string1[i]=='i' || string1[i]=='I' || string1[i]=='o' || string1[i]=='O' || string1[i]=='u' || string1[i]=='U')
vowel=vowel+1;
}
return vowel;
}
5.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* ReArrange(char firstName[50],char lastName[50])
{
char *name = (char *)malloc(100);
strcat(name,lastName);
strcat(name,",");
strcat(name,firstName);
return name;
}
int main(void) {
printf("%s",ReArrange("Bullwinkle","Moose"));
return 0;
}
Output:
Moose,Bullwinkle
6.
double AvgValues()
{
double val,sum;
sum = 0;
int i,n;
printf("How many numbers to average");
scanf("%d",&n);
printf(" Enter the numbers");
for(i=0;i<n;i++)
{
scanf("%f",&val);
sum = sum + val;
}
return sum/n;
}
7.
int GCD(int n1,int n2)
{
int i,gcd;
for(i=1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
return gcd;
}
8.
int Smaller(int n1,int n2)
{
int small;
if(n1 > n2)
small = n2;
else
small = n1;
return small;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.