Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am given an array of integers where the integers are sorted in an increasing order and the difference between adjacent elements is one except for a particular pair of integers where the difference is two. I have to locate the missing integer. I have written the code and so far, I cannot see any problem with it but the required output is not being produced. I would like yourhelp in understanding the mistake in my code.

Example: 11 12 13 15
The missing number is 14

Example: 3 5 6 7
The missing integer is 4

What I have tried:

This is my code. I am gettin the output as 12 for the array values below.
<pre>// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iso646.h>
#include <vector>
#include <string>
#include<iostream>
#include <algorithm>


using namespace std;

int FindMissingNumber(vector<int>& nums)
{
	int result = 0;

	//sort(nums.begin(), nums.end());

	for (int i = 0; i < nums.size(); i++)
	{
		for (int j = i + 1; j < nums.size() - 1; j++)
		{
			if (nums[j] - nums[i] == 2)
			{				
				result = (nums[i] + 1);
			}
		}
	}

	return result;
}

int main() {
 
	vector<int> nums = { 10, 11, 12, 13, 15 };

		cout << FindMissingNumber(nums) << endl;	

	system("pause");
	return 0;
}
Posted
Updated 23-May-17 21:17pm
v3
Comments
Peter_in_2780 23-May-17 22:55pm    
Big hint. You only need one loop.

The logic of your FindMissingNumber function is flawed. Try:
C++
#include <vector>
#include <string>
#include <iostream>


using namespace std;

bool FindMissingNumber(const vector<int>& nums, int & result)
{
  for (size_t i = 0; i < nums.size()-1; i++)
  {
    if ( nums[i] != nums[i+1]-1)
    {
      result = (nums[i]+1);
      return true;
    }
  }
  return false;
}

int main()
{
  vector<int> nums = { 10, 11, 12, 13, 15 };

  int result;
  if ( FindMissingNumber( nums, result) )
    cout << "missing number is " << result << endl;
  else
    cout << "there is no missing number" << endl;
  return 0;
}
 
Share this answer
 
Comments
Maciej Los 24-May-17 4:13am    
5ed!
CPallini 24-May-17 4:43am    
Thank you very much!
Quote:
I have written the code and so far, I cannot see any problem with it but the required output is not being produced.

As already told you, use the debugger to see what your code is doing.
Think about what you have to do to solve the problem by hand, and look at what your code is doing with debugger.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900