Question

•The multiplication operator works by summing some value, x, by another value, y. Write a function...

•The multiplication operator works by summing some value, x, by another value, y. Write a function definition called multiply that takes two integer parameters to perform the operation described. Return the answer to the calling function.
•Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function.
•Write a function called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values.
•Write a function called countConsonants that takes an input file as its parameter. The function should read a word from a file, and a special character. It should count how many vowels and how special characters are in that string. The function should return the vowel and special character count to the calling function.

C++ program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans1 : multiplication function

#include <iostream>
using namespace std;

int multiply(int a, int b)
{
    int mul = 0;
    for(int i=0;i<b;i++)
    {
        mul += a;
    }
    return mul;
}

int main()
{
    cout<<multiply(3,4);
    return 0;
}

Output:

12

Ans 2: random function

#include <iostream>
using namespace std;

int randCount()
{
    int num, count = 0;
    for(int i=0;i<5;i++)
    {
        num = (rand() % (15 - 8 + 1) + 8);
        if(num % 2 == 0)
        {
            count++;
        }
    }
    return count;
}

int main()
{
    cout<<randCount();
    return 0;
}

Output:

T

Ans 3:

#include <iostream>
#include <stdlib.h>
using namespace std;

//Function for getting the product
int getProduct(int n) 
{ 
    int product = 1; 
  
    while (n != 0) { 
        product = product * (n % 10); 
        n = n / 10; 
    } 
  
    return product; 
} 
  
// Function to find the product of digits from L to R
int productinRange(int l, int r) 
{ 
    if (r - l > 9) 
        return 0; 
  
    else { 
        int p = 1; 
  
        // Iterate between L to R 
        for (int i = l; i <= r; i++) 
  
            p *= getProduct(i); 
  
        return p; 
    } 
} 

//Summary Function taking file arguments
int Summary(FILE *input, FILE *output)
{
    int sum_even = 0,sum_odd = 0, cumu = 1;
    int a, b;
    fscanf(input, "%d", &a);
    fscanf(input, "%d", &b);
    fclose (input);
    for(int i=a; i<=b; i++)
    {
        if(i%2==0)
        {
            sum_even += i;
        }
        else
        {
            sum_odd += i;
        }
    }
    cumu = productinRange(a, b); 
    if(output == NULL)
    {
        // File not created hence exit
        printf("Unable to create file.\n");
        exit(EXIT_FAILURE);
    }

    fprintf (output, "Sum of even: %d\n",sum_even);
    fprintf (output, "Sum of odd:  %d\n",sum_odd);
    fprintf (output, "Cumulative product:  %d\n",cumu);
    fclose (output);
}


int main()
{
    FILE *myFile;
    FILE * fp;
    
    myFile = fopen("input.txt", "r");
    fp = fopen ("output.txt","w");
    Summary(myFile, fp);
    return 0;
}

Input file:

input.txt I output.txt 20. ain.cpp 1 11 15

Output file:

pp input.txt output.txt Sum of even: 26 Sum of odd: 39 Cumulative product: 120

Ans 4:

#include <iostream>
#include <stdlib.h>
using namespace std;


//countConsonants Function taking file arguments
int countConsonants(FILE *input)
{
    char line[1000];
    fgets(line, 1000, input);
    int vowels = 0, specialChar = 0;
    
    for (int i = 0; line[i] != '\0'; ++i) {
        if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
            line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
            line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
            line[i] == 'U') {
            ++vowels;
        } else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
            continue;
        } else if (line[i] >= '0' && line[i] <= '9') {
            continue;
        } else{
            ++specialChar;
        }
    }
    cout<<"Text : "<<line;      
    cout<<"\nSpecial Character : "<<specialChar;
    cout<<"\nVowels : "<<vowels;
    fclose (input);
 }


int main()
{
    FILE *myFile;
    myFile = fopen("input.txt", "r");
    countConsonants(myFile);
    return 0;
}

Input file:

... cpp input.txt Hello; ;###AE

Output:

Text : Hello;; ###AE Special Character : 5 Vowels : 4

1596071138535_image.png

Add a comment
Know the answer?
Add Answer to:
•The multiplication operator works by summing some value, x, by another value, y. Write a function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

  • Write a function called productEven that takes as its parameter an input file. The function should...

    Write a function called productEven that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. c++ program

  • please write program in C++ Write a function called productEven, that takes as its parameter an...

    please write program in C++ Write a function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. please write the program in C++

  • Lab 1 Q1. Write a Python program with the following function building block. •All input values...

    Lab 1 Q1. Write a Python program with the following function building block. •All input values are to be taken fro m the user. •Ensure to check for possible error cases like mismatch of data type and input out of range, for every function.       Function 1: countVowels(sentence) – Function that returns the count of vowels in a sentence. Function 2: Sum of all even numbers between two numbers, identify number of parameters   Q2. Write a Python program that reads a...

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • Using python Question 13 (20 points) Write a function named Linestats that finds the number of...

    Using python Question 13 (20 points) Write a function named Linestats that finds the number of consonant and vowel letters on each line of a file and writes those statistics to a corresponding line of a new file, separated by a space. Definitions: A vowel letter is one of a, e, i, o, u, along with the corresponding uppercase letters..A consonant letter is not a vowel letter. The function linestats takes two parameters: 1. inFile, a string, the name of...

  • in Java and also follow rubric please 4. Write a complete program to do the following:...

    in Java and also follow rubric please 4. Write a complete program to do the following: Using an input and output files, write a program that will read 20 numbers from an input file called InFile. Sum all even numbers and multiply all odd numbers. Print the numbers read from the input file to an output file called OutFile. Also print the sum of the even numbers and the product of the odd numbers to the output file. Rubric: •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT