Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a C program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array.
Example: Expected Output
Enter 5 integer numbers 1 5 6 3 5 Enter the number to be searched … 5
5 has appeared at position 2 in the array. 5 has appeared at position 5 in the array.
Final Result: 5 has appeared 2 times in the array.
Visual Representation
Video Tutorial: C Program To Search A Number And Count Its Occurrence In An Array
Source Code: C Program To Search A Number And Count Its Occurrence In An Array
#include
#define N 5
int main()
{
int a[N], i, key, count = 0;
printf("Enter %d integer numbers\n", N);
for(i = 0; i
Output 1:
Enter 5 integer numbers
1
5
9
6
4
Enter the number to be searched …
4
4 has appeared at position 5 in the array.
Final Result: 4 has appeared 1 times in the array.
Output 2:
Enter 5 integer numbers
1
2
3
4
5
Enter the number to be searched …
6
Final Result: 6 has appeared 0 times in the array.
Output 3:
Enter 5 integer numbers
1
5
4
5
2
Enter the number to be searched …
5
5 has appeared at position 2 in the array.
5 has appeared at position 4 in the array.
Final Result: 5 has appeared 2 times in the array.
Logic To Search A Number And Count Its Occurrence In An Array
We ask the user to enter N integer numbers(25 integer numbers according to the problem statement) and store it inside array a[N]. Next we ask the user to input the number to be searched – we store the user input inside variable key.
Inside for loop
We make use of for loop to iterate through entire array. For each iteration we check if the value present at a[i] is equal to value present in key. If it’s true, then we display the position(index value + 1) at which “key” appears and also increment the value of variable count by 1.
After all the iterations of for loop, we display the number of occurrences of key inside the array – value of which is present in variable count.
Explanation With Example
If int a[5] = {1, 5, 6, 3, 5};
key = 5;
i
a[i]
a[i] == key
count
0
1
FALSE
0
1
5
TRUE
1
2
6
FALSE
1
3
3
FALSE
1
4
5
TRUE
2
5 has appeared at position 2 in the array.
5 has appeared at position 5 in the array.
Final Result: 5 has appeared 2 times in the array.
Note: We increment the position of key in the array by 1, as users are not used to counting from 0. (Array index starts from 0.)
We’ve seen the working of explain() method briefly in previous video tutorials – in this video tutorial lets dive in to learn more about this useful method, which helps us analyze and optimize our MongoDB commands.
Here MongoDB server is returning a basic cursor, as we do not have index on field “a”. Also note that, it’s scanning 1000 documents in the “name” collection and scanning 1000 index keys in “system.indexes” collection, where the default key is “_id”.
Here we create index on key “a” and “b” – it’s called compound key – after which the explain() method shows that MongoDB server is returning a Btree cursor, meaning it’s making use of index key to execute the query/command.
Also not the number of objects scanned in the “name” collection as well as in the “system.indexes” collection.
Covered Index or indexOnly If our command is requesting for values present inside the index, mongo engine will fetch that information from the “system.indexes” collection itself and does not go to the collection where the entire document is present – in our case “name” collection. Since index is on keys “a” and “b” in our case, if we query for “a” and “b” values only, it fetches those values directly from the index information and will not go to “name” collection. Hence the speed of execution of the command is faster, and this type of commands are very efficient in creating optimized web applications.
Note from above explain() result, the number of objects scanned in the “name” collection is 0, and the number of scans made on indexes in only 1.
We learnt the uses of having an index/key on our collection and how to create the index. Now, in this video tutorial lets learn how to get index on individual collection and how to drop / remove / delete the index we’ve created.
After creating “another” collection, mongoDB engine generates default key on its “_id” field. And “system.indexes” shows all the keys present inside the database for all the collections it has. This can get messy if we have large number of collections – which we do in even slightly bigger projects.
Lets learn to create index and to optimize the database in MongoDB.
Creating “Database”: “temp”, “Collection”: “no”, and inserting 10 Million documents inside it
1
2
3
4
5
use temp
switched to db temp
for(i=0; i
use temp
switched to db temp
for(i=0; i
Since Mongo Shell is built out of JavaScript, you can pass in any valid Javascript code to it. So we write a for loop and insert 10 Million documents inside “no” collection.
“no” collection has 10 Million record, but it won’t fetch you all records at once, as it would take a lot of time and resources of your computer! So it only fetches 20 records at a time. You can iterate through next 20 documents by using command “it“.
find() method scans through all the documents present in the collection to find multiple matches for the condition. So in above case, find() method scans through 10 Million documents, hence returns the result slowly. Where as findOne() method stops scanning the collection as soon as it finds the first matching document, so findOne() returns result faster than find() method.
We create index on “student_id”. It takes little time to create the index, as we have 10 Million documents inside “no” collection.
After creating index on “student_id”, run the same command and you’ll get the results instantly – maybe it takes 0.01 ms, but the delay can’t be noticed. Why does it return results faster after creating index on “student_id”? Watch this short video lesson to know it: index / key: MongoDB
If we have 3 fields in a document – name, age, sex We could make name or age or sex or (name, age) or (name, age, sex) as index.
Assume that we make a index out of (name, age, sex) In this case, we need to use the keys from left to right.
If we use “name” in our command, it makes use of the index. If we use (“name”, “age”) in our command, it makes use of the index. If we use (“name”, “age”, “sex”) in our command, it makes use of the index.
If we use “age” in our command, it can’t use the index for its operation. If we use (“age”, “sex”) in our command, it can’t use the index for its operation.
If we use (“name”, “sex”) in our command, it simply uses “name” field and ignores “sex” field.
For convenience, mongoDB adds “_id” field to each document inserted. “_id” is unique across the collection. And index is automatically created on “_id”.
Since index information is stored in “system.indexes” collection – it consumes disk too. So we need to make sure to add indexes to only those fields which we access frequently. Also note that, each time a document is inserted, “system.indexes” collection must be updated with the new index information, which takes time, bandwidth and disk space. So we need to be careful while creating indexes.