arrow
arrow

PF-PUCIT(NEW-CAMPUS)

ASSIGNMENT 09:

Functions


                            
                                returndataType function name(dataType argumentName1,dataType argumentName1,...);
                                .Prototypes;
                                1. Void Functions
                                2.return Type Functions
                                3.Pass by refernece Functions 
                                4.recursive Functions
                            
                        

Task 1. Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power() that takes a double value for n and an int value for p, and returns the result as a double value. Write a main() function that gets values from the user to test this function.


                        
#include <iostream>
using namespace std;

double power(double num, int po);

int main()
{
    double num;
    int po;
    cout << "Enter the value of number:" << endl;
    cin >> num;
    cout << "Enter the value of power:" << endl;
    cin >> po;
    cout << "Answer: " << power(num, po) << endl;

    return 0;
}

double power(double num, int po)
{
    double ans = 1;
    for (int i = 0; i < po; i++)
    {
        ans = ans * num;
    }
    return ans;
}
                        
                    

Task 2. Write a function called zeroSmaller() that is passed two int arguments by reference and then sets the smaller of the two numbers to 0. Write a main() program to exercise this function


                        
#include<iostream>
using namespace std;

void zeroSmaller(int &n1, int &n2);

int main()
{
    int num1;
    int num2;
    cout<<"Enter the number_1:"<<endl;
    cin>>num1;
    cout << "Enter the number_1:" << endl;
    cin >> num2;
    zeroSmaller(num1,num2);
    cout<<"The number 1 = "<<num1<<endl;
    cout<<"The number 2 = "<<num2<<endl;
    return 0;
}

void zeroSmaller(int &n1, int &n2)
{
    if (n1 < n2)
    {
        n1 = 0;
    }
    if (n2 < n1)
    {
        n2 = 0;
    }
}
                        
                    

Task 3.Write a program that creates a two-dimensional array initialized with test data. Use any data type you wish. The program should have the following functions: • getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array.

• getAverage

This function should accept a two-dimensional array as its argument and return the average of all the values in the array.

• getRowTotal .

This function should accept a two-dimensional array as its first argument and an integer as it second argument. The second argument should be the subscript of a row in the array. The function should return the total of the values in the specified row.

• getColumnTotal .

This functionshould accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The function should return the total of the values in the specified column.

• getHighestInRow

This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the highest value in the specified row of the array.

• getLowestInRow

This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the lowest value in the specified row of


                        
#include <iostream>
#include <iomanip>
using namespace std;

const int row = 7;
const int col = 5;

void display(int matrix[][col], int row);
int total(int matrix[][col], int row);
float average(int matrix[][col], int row, int total);
int rowTotal(int matrix[][col], int row, int rowNum);
int colTotal(int matrix[][col], int row, int colNum);
int high_inRow(int matrix[][col], int row, int rowNum);
int low_inRow(int matrix[][col], int row, int rowNum);

int main()
{

    int matrix[row][col] = {{1, 2, 3, 4, 5},
                            {6, 7, 8, 9, 10},
                            {11, 12, 13, 14, 15},
                            {16, 17, 18, 19, 20},
                            {21, 22, 23, 24, 25},
                            {26, 27, 28, 29, 30},
                            {31, 32, 33, 34, 35}};

    int colNum = 0;
    int rowNum = 0;

    display(matrix, row);

    cout << "The total sum of the all values = " << total(matrix, row) << endl;
    cout << "The average of the all values = " << average(matrix, row, total(matrix, row)) << endl;
    cout << "Enter the row number whose sum you want to find:" << endl;
    cin >> rowNum;
    cout << "The sum of the row " << rowNum << " = " << rowTotal(matrix, row, rowNum) << endl;
    cout << "Enter the col number whose sum you want to find:" << endl;
    cin >> colNum;
    cout << "The sum of the col " << colNum << " = " << colTotal(matrix, row, colNum) << endl;
    cout << "Enter the row whose highest value that you want to display:" << endl;
    cin >> rowNum;
    cout << "The highest value in the row " << rowNum << " :" << high_inRow(matrix, row, rowNum) << endl;
    cout << "Enter the row whose highest value that you want to display:" << endl;
    cin >> rowNum;
    cout << "The lowest value in the row " << rowNum << " :" << low_inRow(matrix, row, rowNum) << endl;

    return 0;
}

void display(int matrix[][col], int row)
{
    // Display
    cout << "Matrix :" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << left;
            cout << setw(5) << matrix[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

int total(int matrix[][col], int row)
{
    int total;
    // get-total
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            total += matrix[i][j];
        }
    }
    return total;
}

float average(int matrix[][col], int row, int total)
{
    // get-average
    float average;
    average = float(total) / (row * col);
    return average;
}
int rowTotal(int matrix[][col], int row, int rowNum)
{
    // get-rowTotal
    int rowTotal;
    for (int i = 0; i < col; i++)
    {
        rowTotal += matrix[rowNum][i];
    }

    return rowTotal;
}

int colTotal(int matrix[][col], int row, int colNum)
{
    // get-colTotal
    int colTotal;
    for (int i = 0; i < row; i++)
    {
        colTotal += matrix[i][colNum];
    }

    return colTotal;
}

int high_inRow(int matrix[][col], int row, int rowNum)
{
    // get-HighinRow
    int high_inRow;
    high_inRow = matrix[rowNum][0];
    for (int i = 0; i < col; i++)
    {
        if (matrix[rowNum][i] > high_inRow)
        {
            high_inRow = matrix[rowNum][i];
        }
    }
    return high_inRow;
}

int low_inRow(int matrix[][col], int row, int rowNum)
{
    // get-lowinRow
    int low_inRow;
    low_inRow = matrix[rowNum][0];
    for (int i = 0; i < col; i++)
    {
        if (matrix[rowNum][i] < low_inRow)
        {
            low_inRow = matrix[rowNum][i];
        }
    }
    return low_inRow;
}

                        
                    

Task 4.Write a function called hms_to_secs() that takes three int values—for hours, minutes, and seconds—as arguments, and returns the equivalent time in seconds (type long). Create a program that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from the user (format 12:59:59), calling the function, and displaying the value of seconds it returns.


                        
#include <iostream>
using namespace std;
struct time{
  int hrs;
  int min;
  int sec;
};
int hms_to_sec(int hrs, int min, int sec);

int main()
{
    time t;
    char temp;
    char ch = 'y';

    do
    {
        if (ch == 'y')
        {
            cout << "Enter the time in the format of 23:59:59 ";
            cin >> t.hrs >> temp >> t.min >> temp >> t.sec;
            cout << "The given time in the seconds :" << endl;
            cout << hms_to_sec(t.hrs, t.min, t.sec) <<" sec"<< endl;
            cout << "Again Want to continue:" << endl;
            cin >> ch;
            ch = tolower(ch);
        }
        else
        {
            cout << "Invalid Input:Please Enter the valid Input:" << endl;
            cin >> ch;
            ch = tolower(ch);
        }
    } while (ch != 'n');

    cout << "\t\t\tQuiting.................." << endl;

    return 0;
}

int hms_to_sec(int hrs, int min, int sec)
{

    long long int seconds;

    seconds = (((hrs * 60) + min) * 60) + sec;

    return seconds;
}
                        
                    

Task 5.reate a structure Distance having to data members, integer feet and float inches. Write a function that takes two Distance values as arguments and returns the larger one. Include a main() program that accepts two Distance values from the user, compares them, and displays the larger.


                        
#include <iostream>
#include <iomanip>
using namespace std;
const int members = 2;

struct distanc_e
{
    int feet;
    float inches;
};

distanc_e largDis(distanc_e dis[], int size);
void show(distanc_e dis);

int main()
{
    distanc_e dis[members];
    for (int i = 0; i < members; i++)
    {
        cout << "Enter the Data for the Distance " << i + 1 << " :" << endl;
        cout << "Enter the feets of the distance  " << i + 1 << " :" << endl;
        cin >> dis[i].feet;
        cout << "Enter the inches of the distance  " << i + 1 << " :" << endl;
        cin >> dis[i].inches;
    }
    show(largDis(dis, members));
    return 0;
}
void show(distanc_e dis)
{
    cout << "The total feet are :" << dis.feet << "." << dis.inches << endl;
}

distanc_e largDis(distanc_e dis[], int size)
{
    distanc_e total;
    total.feet = 0;
    total.inches = 0;
    for (int i = 0; i < size; i++)
    {
        total.feet += dis[i].feet;
        total.inches += dis[i].inches;
        if (total.inches >= 12)
        {
            total.feet += 1;
            total.inches -= 12;
        }
    }

    return total;
}

                        
                    

Task 6. reate a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter
hours:”, and so forth). The program should then store the time in a variable of type struct time, and finally print out
the total number of seconds represented by this time value: write a program that obtains two time values from the user in 12:59:59 format, stores them in struct time variables, converts each one to seconds (type int), adds these quantities, converts the result back to hoursminutes-seconds, stores the result in a time structure, and finally displays the result in 12:59:59 format. Keep the same functionality, but modify the program so that it uses two functions. The first, time_to_secs(), takes as its only argument a structure of type time, and returns the
equivalent in seconds (type long).
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds


                        
#include<iostream>
using namespace std;
struct time{
   int hrs;
   int mins;
   int secs;
};

int time_to_secs(time t1);
void secs_to_time(long long int seconds);

int main()
{
    const int size = 2;
    time t1;
    time t2[size];
    long long int sum = 0;
    cout << "First Part of the program:" << endl;
    cout << "Enter hours:";
    cin >> t1.hrs;
    cout << "Enter minutes:";
    cin >> t1.mins;
    cout << "Enter seconds:";
    cin >> t1.secs;
    cout << "The total seconds in the given time :" << endl;
    cout << time_to_secs(t1);
    cout << endl;
    cout << "Second part of the program:" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << "Enter the time " << i + 1 << endl;
        cout << "Enter hours:";
        cin >> t2[i].hrs;
        cout << "Enter minutes:";
        cin >> t2[i].mins;
        cout << "Enter seconds:";
        cin >> t2[i].secs;
        cout << endl;
        sum += time_to_secs(t2[i]);
    }
    cout << sum << endl;
    secs_to_time(sum);

    return 0;
}

int time_to_secs(time t1)
{
    long long int seconds;

    seconds = (((t1.hrs * 60) + t1.mins) * 60) + t1.secs;

    return seconds;
}

void secs_to_time(long long int seconds)
{
    time result;
    result.hrs=seconds/3600;
    result.mins=(seconds%3600)/60;
    result.secs=seconds%60;
    cout<<"The seconds to time format :"<<endl;
    cout<<result.hrs<<":"<<result.mins<<":"<<result.secs<<endl;
}

                        
                    

Whats make this website differents from others?

Amazing Feature That Increase your Productivity

If you don't find any problem,in this website and if you have a very difficult assignment question that is not present on internet ,then you dont bother yourself,we are here to solve every problem of your life you just click on the Problem asking button and write your problem we slove the code of your problem and sent you on your email addres that you put in the sin up form.