UIU: Learn C by Examples: Part: 1


Question 1:

Write a program that will output the greatest number between two numbers


#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{

    int firstNumber;
    int secondNumber;

    scanf ("%d %d", &firstNumber, &secondNumber);

    if (firstNumber > secondNumber) {
        printf ("%d\n", firstNumber);

    } else {
        printf ("%d\n", secondNumber);
    }

    return 0;
}

Question 2:

Write a program that will output the smallest number between two numbers. (try yourself)

Question 3:

Write a program that will output the greatest number among three numbers


#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{

    int firstNumber;
    int secondNumber;
    int thirdNumber;

    scanf ("%d %d %d", &firstNumber, &secondNumber, &thirdNumber);

    if (firstNumber > secondNumber) {
        if (firstNumber > thirdNumber) {
            printf ("%d\n", firstNumber);

        } else {
            printf ("%d\n", thirdNumber);

        }
    } else {
        if (secondNumber > thirdNumber) {
            printf ("%d\n", secondNumber);

        } else {
            printf ("%d\n", thirdNumber);

        }
    }

    return 0;
}


#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{

    int firstNumber;
    int secondNumber;
    int thirdNumber;

    scanf ("%d %d %d", &firstNumber, &secondNumber, &thirdNumber);
    
    if (firstNumber > secondNumber && firstNumber > thirdNumber) {
        printf ("%d\n", firstNumber);
        
    } else if (secondNumber > firstNumber && secondNumber > thirdNumber) {
        printf ("%d\n", secondNumber);
        
    } else {
        printf ("%d\n", thirdNumber);
    }

    return 0;
}

Question 4:

Write a program that will output the smallest number among three numbers (try yourself in both mentioned ways showed in Question 3 solution)

Question 5:

Write a program that will output two numbers in sorted order (descending order: greater number will come first)


#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{

    int firstNumber;
    int secondNumber;

    scanf ("%d %d", &firstNumber, &secondNumber);

    if (firstNumber > secondNumber) {
        printf ("%d %d\n", firstNumber, secondNumber);
        
    } else {
        printf ("%d %d\n", secondNumber, firstNumber);
    }
    

    return 0;
}

Question 6:

Write a program that will output two numbers in sorted order (ascending order: smaller number will come first) [try yourself]

Question 7:

Write a program that will output three numbers in sorted order (descending order: greater number will come first)


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{

    int firstNumber;
    int secondNumber;
    int thirdNumber;

    scanf ("%d %d %d", &firstNumber, &secondNumber, &thirdNumber);

    if (firstNumber > secondNumber) {
        if (firstNumber > thirdNumber) {
            if (secondNumber > thirdNumber) {
                printf ("%d %d %d", firstNumber, secondNumber, thirdNumber);

            } else {
                printf ("%d %d %d", firstNumber, thirdNumber, secondNumber);

            }
        } else {
            printf ("%d %d %d", thirdNumber, firstNumber, secondNumber);

        }
    } else if (secondNumber > thirdNumber) {
        if (firstNumber > thirdNumber) {
            printf ("%d %d %d", secondNumber, firstNumber, thirdNumber);

        } else {
            printf ("%d %d %d", secondNumber, thirdNumber, firstNumber);

        }
    } else {
        if (firstNumber > secondNumber) {
            printf ("%d %d %d", thirdNumber, firstNumber, secondNumber);

        } else {
            printf ("%d %d %d", thirdNumber, secondNumber, firstNumber);

        }
    }

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

Question 8:

Write a program that will output three numbers in sorted order (ascending order: greater number will come first) [try yourself]

Question 9:

Write a program that will output “Yes” if two numbers are distinct (different, not same)


#include <cstdio>

using namespace std;

int main(int argc, const char * argv[])
{

    int firstNumber;
    int secondNumber;

    scanf ("%d %d", &firstNumber, &secondNumber);
    
    if (firstNumber != secondNumber) {
        printf ("Yes\n");
        
    } else {
        printf ("No\n");
        
    }

    return 0;
}

Question 10:

Write a program that will output “Yes” if three numbers are distinct (different, not same)

try yourself and check these input against your code


1 1 1
No

1 1 2
No

2 1 1
No

3 4 3
No

4 5 2
Yes

Question 11:

Write a program that will output the greatest number between

two numbers and
three numbers and
an array of numbers

using overloading function (same function name, but different parameter)


#include <cstdio>

using namespace std;

/**
* return the greatest number between two numbers
*/
int maximum(int a, int b)
{
    if (a > b) return a;    // if 'a' is greater then just return 'a'
    return b;               // otherwise, return 'b'
}

/**
* return the gretest number among three numbers
*/
int maximum(int a, int b, int c)
{
    return maximum(a, maximum(b, c));
}

int maximum(int a [], int length)
{
    int ret = 0;

    for ( int i = 0; i < length; i++ ) {
        ret = maximum(ret, a [i]);
    }

    return ret;
}

int main(int argc, const char * argv[])
{
    int a = 10, b = 15, c = 25;

    int array [] = {3, 6, 2, 1, 9, 8};

    printf ("%d\n", maximum(a, b));

    printf ("%d\n", maximum(a, b, c));

    printf ("%d\n", maximum(array, 5));

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s