C Program To Find Range of Set of Numbers

Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.

Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C
C Program To Find Absolute Value of a Number

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5

Output:
Small Number = 1
Big Number = 5
Range is 4

Logic To Find Range of Set of Numbers

First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.

Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.

Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.

For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.

Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.

Finally, we use below formula to calculate range of the list:
range = big – small;

Video Tutorial: C Program To Find Range of Set of Numbers


[youtube https://www.youtube.com/watch?v=-yN6KsA8d-k]

YouTube Link: https://www.youtube.com/watch?v=-yN6KsA8d-k [Watch the Video In Full Screen.]

Source Code: C Program To Find Range of Set of Numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int small, big, range, num, limit;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);
    scanf("%d", &num);

    small = big = num;

    limit = limit - 1;

    while(limit)
    {
        scanf("%d", &num);

        if(num > big)
        {
            big = num;
        }

        if(num < small)
        {
            small = num;
        }

        limit--;
    }

    range = big - small;

    printf("Small Number = %d\nBig Number = %d\n", small, big);
    printf("Range is %d\n", abs(range));

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
0
-1
2
3
4
Small Number = -1
Big Number = 4
Range is 5

Output 2:
Enter the limit
6
Enter 6 numbers
10
9
8
7
6
5
Small Number = 5
Big Number = 10
Range is 5

Note: abs() returns the absolute value of a number. Absolute value is always positive.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Working With Arrays: MongoDB

Lets learn some of the methods and operators to work with arrays in MongoDB.

In this video tutorial, we’ll be looking at:
update()
$set
$push
$pop
$pushAll
$pull
$pullAll
$addToSet

update set push pop pushAll pull pullAll addToSet operators mongodb

test database, names collection

1
2
3
4
5
6
7
8
9
MongoDB shell version: 2.6.1
connecting to: test
> db.names.find()
 
> db.names.insert({"_id": 1, "a": [1, 2, 3, 4]});
WriteResult({ "nInserted" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 1, 2, 3, 4 ] }

We insert a document into “names” collection. We’ll be working on the array field present in the document.

Working With Arrays: MongoDB


[youtube https://www.youtube.com/watch?v=xc5TiSqvnPQ]

YouTube Link: https://www.youtube.com/watch?v=xc5TiSqvnPQ [Watch the Video In Full Screen.]



update() method

1
2
3
4
5
> db.names.update({"_id": 1}, {"a": [1, 2, 3, 4, 5]});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 1, 2, 3, 4, 5 ] }

We can update the array by simply using update() method. But here, we need to remember all the elements of the array as well as the new element to be inserted into the array. Thus, this method is somewhat tedious.

$set operator

1
2
3
4
5
> db.names.update({"_id": 1}, {$set: {"a.5": 6}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 1, 2, 3, 4, 5, 6 ] }

We could insert an element into the array by making use of $set operator. Here we need to know the position where the new element needs to be inserted. In mongoDB, array index starts from zero.

$push operator

1
2
3
4
5
> db.names.update({"_id": 1}, {$push: {"a": 7}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 1, 2, 3, 4, 5, 6, 7 ] }

using $push operator we can insert an element to the right hand side of the array. Here we simply specify the key and the value/element to be inserted.

$pop: {a: 1}

1
2
3
4
5
> db.names.update({"_id": 1}, {$pop: {"a": 1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 1, 2, 3, 4, 5, 6 ] }

$pop operator which has 1 as value to the key, removes the right most element from the array.

$pop: {a: -1}

1
2
3
4
5
> db.names.update({"_id": 1}, {$pop: {"a": -1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 2, 3, 4, 5, 6 ] }

$pop operator which has -1 as value to the key, removes the left most element from the array.

$pushAll operator

1
2
3
4
5
> db.names.update({"_id": 1}, {$pushAll: {"a": [7, 8]}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 2, 3, 4, 5, 6, 7, 8 ] }

$pushAll operator inserts all the elements(array of elements) specified, to the right hand side of the existing array.

$pull operator

1
2
3
4
5
> db.names.update({"_id": 1}, {$pull: {"a": 3}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 2, 4, 5, 6, 7, 8 ] }

$pull operator pulls or removes the specified element from the array, irrespective of its position.

$pullAll operator

1
2
3
4
5
> db.names.update({"_id": 1}, {$pullAll: {"a": [2, 7, 8]}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 4, 5, 6 ] }

$pullAll operator pulls/removes all the elements(array of elements) specified from the array, irrespective of its position.

$addToSet operator

1
2
3
4
5
> db.names.update({"_id": 1}, {$addToSet: {"a": 3}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find()
{ "_id" : 1, "a" : [ 4, 5, 6, 3 ] }

$addToSet operator adds specified element to the array, if its not already present in the array. If the element is already present in the array, then it doesn’t add it once again.

1
2
3
4
> db.names.update({"_id": 1}, {$addToSet: {"a": 3}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.names.find()
{ "_id" : 1, "a" : [ 4, 5, 6, 3 ] }

If the element is already present in the array, then $addToSet doesn’t add it once again.

Update with UNSET Operator: MongoDB

Lets use $unset operator along with update() method, to update the documents.

update with set operator mongodb Update with UNSET Operator: MongoDB

test database, names collection

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.names.find().pretty()
{
        "_id" : ObjectId("53c3fc2005e0ce2719d91bd2"),
        "Company" : "Google",
        "Product" : "Nexus",
        "No" : 1
}
{
        "_id" : ObjectId("53c3fd3bb9ae26fa217b1e12"),
        "Company" : "Apple",
        "Product" : "iPhone",
        "No" : 2
}

Here we have 2 documents with fields _id, Company, Product and No.

Update with UNSET Operator: MongoDB


[youtube https://www.youtube.com/watch?v=8UUdITiZ5ZE]

YouTube Link: https://www.youtube.com/watch?v=8UUdITiZ5ZE [Watch the Video In Full Screen.]



We could remove a field using update() method, by not specifying it in the second argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.names.update({"No": 1}, {"Company": "Google", "No": 1});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{
        "_id" : ObjectId("53c3fc2005e0ce2719d91bd2"),
        "Company" : "Google",
        "No" : 1
}
{
        "_id" : ObjectId("53c3fd3bb9ae26fa217b1e12"),
        "Company" : "Apple",
        "Product" : "iPhone",
        "No" : 2
}

Related Read: Update Method: MongoDB

This way, we could eliminate the field Product. But this is a tedious process – we need to remember all the fields inorder to achieve this. It gets difficult when we have many fields in our document. To solve this problem, we have $unset operator, where we only need to know the field name which we want to remove.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.names.update({"No": 1}, {$unset: {"Product": 1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{
        "_id" : ObjectId("53c3fc2005e0ce2719d91bd2"),
        "Company" : "Google",
        "No" : 1
}
{
        "_id" : ObjectId("53c3fd3bb9ae26fa217b1e12"),
        "Company" : "Apple",
        "Product" : "iPhone",
        "No" : 2
}

$unset operator syntax is same to unset or remove array from a document.

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
36
> db.names.update({"No": 1}, {$set: {"Product": ["LG", "Samsung", "HTC"]}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{
        "_id" : ObjectId("53c3fc2005e0ce2719d91bd2"),
        "Company" : "Google",
        "No" : 1,
        "Product" : [
                "LG",
                "Samsung",
                "HTC"
        ]
}
{
        "_id" : ObjectId("53c3fd3bb9ae26fa217b1e12"),
        "Company" : "Apple",
        "Product" : "iPhone",
        "No" : 2
}
 
> db.names.update({"No": 1}, {$unset: {"Product": 1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{
        "_id" : ObjectId("53c3fc2005e0ce2719d91bd2"),
        "Company" : "Google",
        "No" : 1
}
{
        "_id" : ObjectId("53c3fd3bb9ae26fa217b1e12"),
        "Company" : "Apple",
        "Product" : "iPhone",
        "No" : 2
}

Related Read: Update with SET Operator: MongoDB

Update with SET Operator: MongoDB

Lets use $set operator along with update() method, to update the documents.

test database, names collection

1
2
3
> db.names.find()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }

update-with-set-operator-mongodb

Related Read: Update Method: MongoDB

Lets update the first document using only update method.

1
2
3
4
5
6
7
8
9
10
> db.names.update({"name": "Alia"}, {"name": "Alia", "age": 25});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{
        "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"),
        "name" : "Alia",
        "age" : 25
}

Everything is ok in this case. But what if you forget to mention the name field in the second argument of update() method.

1
2
3
4
5
6
> db.names.update({"name": "Alia"}, {"age": 25});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "age" : 25 }

In this case, the name field gets erased. As we did not specify name field in the second parameter of update() method.

Using $set operator

1
2
3
4
5
6
7
8
9
10
> db.names.update({"name": "Bebo"}, {$set: {"age": 25}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "age" : 25 }
{
        "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"),
        "name" : "Bebo",
        "age" : 25
}

Here we make use of document with name as Bebo. Using $set operator we only specify the fields we want to add or update. Need not specify other existing fields in order to retain them.

Update with SET Operator: MongoDB


[youtube https://www.youtube.com/watch?v=nsDnZcstUV4]

YouTube Link: https://www.youtube.com/watch?v=nsDnZcstUV4 [Watch the Video In Full Screen.]



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
36
37
> db.names.update({"name": "Bebo"}, {$set: {"age": 25, "salary": 25}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "age" : 25 }
{
        "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"),
        "name" : "Bebo",
        "age" : 25,
        "salary" : 25
}
 
 
> db.names.update({"name": "Bebo"}, {$set: {"age": 26, "salary": 25}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "age" : 25 }
{
        "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"),
        "name" : "Bebo",
        "age" : 26,
        "salary" : 25
}
 
 
> db.names.update({"name": "Bebo"}, {$set: {"salary": 30}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "age" : 25 }
{
        "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"),
        "name" : "Bebo",
        "age" : 26,
        "salary" : 30
}

We can update existing field or add a field to the existing document using $set operator and need not to worry about the other fields in the document.