473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help me fix this code: Finding the missing number in the array

I'm trying to write a program where in you input 9 numbers from 1-10, then it determines the missing number. Here's my code. It has a lot of errors. Help me do the right structure. Maybe after that, I'll improve it by setting conditions like no repetition/ 0< number <10. Just help me to fix this first:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int d[]={1,2,3,4,5,6,1,7,9, 10};
  7.     int i=0,j=0,c=0,missing=0;
  8.  
  9. for (i = 0; i < 9; i++)
  10. {
  11.     cout << "input: ";
  12.     cin >> d[i];
  13.     c = 0;
  14.     for (j = 0; j<10; j++)
  15.     {
  16.         if (i == d[j])
  17.         {
  18.             c = i;
  19.         }
  20.     }
  21.     if (i==d[j])
  22.     {
  23.         missing = j;
  24.  
  25.     }
  26. }
  27.     cout << "missing is " << missing << endl;
  28.     return 0;
  29. }
  30.  
  31.  
I'm so new to C++. It always prints out the number 10 even though I typed it already. I really have no idea how to determine the missing one.
Nov 27 '12 #1
3 2536
whodgson
542 512MB
There are 10 numbers between 1 and 10 inclusive and there are 10
integers in d[]. Your two loop conditions are 8 and 9. Other than those observations I have no idea what you are doing.
*"It always prints out the number 10"
this is because when j<10 or 9 (and finishes iterating)it has reached the last array element which contains 10. Remember j starts counting at 0.
Nov 27 '12 #2
Can't this be an easiest approach

1) Sort the given sequence
2) fix the no. with the difference of two consecutive no. as > 1; duplicates can be filtered out with the difference as zero.

Hope this help.
Nov 27 '12 #3
whodgson
542 512MB
Or you could:
1)Inside a loop iterate through the array and at each element compare loop i with array[i].If they are equal (==) continue.
Expand|Select|Wrap|Line Numbers
  1. 2)If (array[i]!= loop (i))cout<<i<<" ";//not in array so print
Nov 28 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ton 't Lam | last post by:
Suppose I have an array my @arr = ( 1,4); I need to have the missing numbers: my @res = ( 2,3,5); How to calculate this easy from @arr ? Best Regards, Ton
4
by: Mahesh BS | last post by:
Hello, I need to write a query to find out a set of missing number in a given sequence. Eg : a Column in some table has the following data
5
by: le0 | last post by:
Hello guys, Im really having a hard time doing this, I have a record set with the ItemNo field with the data type as Text. In the record that I have, I want to find the missing number in the...
1
by: luftikus143 | last post by:
Hi there, I developed a PHP code calculating the number of visits of a web site based on the more-or-less common 30-minutes approach. But meanwhile, the log-database got so huge, that it takes...
10
by: strife | last post by:
Hi, This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot. I have to figure out the highest number from a users...
1
by: suresh_nsnguys | last post by:
Respected sir/madam, In My Application, users can send me an email with some keyword 'XXXX' to our mail server 'XXXX@xxxxx.com'.i am using imap_open() and other imap functions to read that...
10
by: ALKASER266 | last post by:
Hey guyz I have a prac and I am beginner and I did this code> Is my code is complete and if is it not complete how i can complete it? and how i can arrange it more? How I can make my driver to...
2
by: awaisworld13 | last post by:
I want to write a program which take input and save the poistions of the array elements in another array. let if i enter an array A= 4 A=1 A=2 A=3 now the output array will hold 1 2 3
1
by: banoo | last post by:
You are given an unsorted list of n−1 distinct integers from the range 1 to n. Write a python program to find the missing integer. I just began learning python language so am finding it difficult....
5
by: Bernardo | last post by:
Okay, so I am making a program that accepts user input of integers. As you can see I am using an array and i need to get the greatest number, and smallest, range, mean, etc. But I successfully did...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.