Doubles trouble

As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list are twice some other item in the same list. You will need a program to help you with the grading. This program should be able to scan the lists and output the correct answer for each one. For example, given the list

1 4 3 2 9 7 18 22

your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.



The input file will consist of one or more lists of numbers. There will be one list of numbers per line. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99. Each line will be terminated with the integer 0, which is not considered part of the list. A line with the single number -1 will mark the end of the file. The example input below shows 3 separate lists. Some lists may not contain any doubles.



The output will consist of one line per input list, containing a count of the items that are double some other item.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example input:

1 4 3 2 9 7 18 22 0

2 4 8 10 0

7 5 11 13 1 3 0

-1

// Example output(respectively):

3

2

0



I'm having trouble starting this code out. I'm not exactly sure how to input the numbers the program is giving us.
If anyone can come up with a complete code and explain what each line does that would be fantastic. We need to have this complete and understand everything as soon as possible, we have a test tomorrow and our professor said that knowing this will help.
You will not understand everything by tomorrow, if someone just hands out some code (even with explanations) for a homework, whose entire purpose is that you do learn by doing.

If you don't know the basics, then read them again:
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/files/
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/arrays/
I'm still having trouble starting this code out. It wasn't due today.
Last edited on
I would like some help on this though. Arrays are not my strong suit and he didn't even teach us how to solve this assignment in class. So any help will be appreciated even the backhanded help.
Take a stab at it and post your code. We'll try and point you in the right direction, but we won't write the code for you.
Last edited on
I wasn't asking you to, more or less I was asking how to input the numbers from the program into my code for it to run. So far I haven't been able to do that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
  const int size = 100;
  int array[size];
  int count = 0;
  
  for (int i = 0; i < size; i++)
  {
    cout << array[i];   
  }

  
  
  
  return 0;
}


1 4 3 2 9 7 18 22 0
2 4 8 10 0
7 5 11 13 1 3 0
-1

^^^^ This is what it want's me to output using an array. The 0's end the line and the -1 ends the input. I don't have the slightest clue as to how to start my code so that it can run it correctly. So far I've gotten answers like this:

19964627541844753937-2199684523819968442528163806056199684204880124464040-141837419512446604242276380872438087243805808001244684425418543342721244748425001484229837422983118465315670161244748424466438010880838058080012447720124482000124478000380580812447964222899038087043808740124487242623483801088019968360801996602758-136124484840-1418374195124486842422763808744380874402147307520430524412448924254185433427212449564250014842298374229831184653126304305244214730752043016911244912214730752014504842534561851572995-24229831422985143016911244988430161543016914250034

And that is just with that bit of code at the top.
Also, this is not reading from a file.
Last edited on
The bit of code you posted is printing an uninitialized array. i.e. You're doing a cout at line 12, but you've never set the contents of the array to anything.

So try adding a loop to enter numbers at line 9:
9
10
11
    for (int i = 0; i < size; i++)
    {  cin >> array[i];   
    }

So it should look something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  const int size = 100;
  int array[size];
  int count = 0;
  
  for (int i = 0; i < size; i++)
  { 
    cin >> array[i];   

    for (int i = 0; i < size; i++)
    {
      cout << array[i];   
    }
  
  }


Because this doesn't work either. It outputs too much information and comes back wrong when the test is run. Also, it still is not inputting the numbers.

So I changed it around a bit.
1
2
3
4
5
6
7
8
9
10
  for (int i = 0; i < size; i++)
  { 
    cin >> array[i];   

    //for (int i = 0; i < size; i++)
    {
      cout << array[i];   
    }
  
  }

This is what it is outputting:

14329718220248100751113130-1
TIME EXCEEDED!
Last edited on
In your first snippet, your for loops are nested. You're going to output the entire array every time the user enters one number.

The second snippet should work, although it's going to ask the user for one number, then output that number each time through the loop.

Topic archived. No new replies allowed.