Problem with outputs?

The program needs to read in two sorted arrays each of at most 10 positive integers - assume that the user follows directions and enters only positive integers in ascending order - and merges them into one larger sorted array. -1 is the end of input flag.

Example of program:
Enter a sorted array of no more than 10 elements: 1 2 4 7 10 -1
Enter a sorted array of no more than 10 elements: 3 4 6 13 17 22 30 -1
Merged array: 1 2 3 4 4 6 7 10 13 17 22 30

The only problem: It drops off the last two outputs. If I try to make the output for loop output two more times, they are junk values. I'm not the best at debugging and I really can't find the problem with this. Please help.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
int main()
{
    using namespace std;
    int list1[10], list2[10], merge[20], i, j, lim1, lim2, input;
    //-----------
    cout << "Enter a sorted array of no more than 10 elements: ";
    //READING INPUT
    cin >> input;
    for(i=0;i<10&&input!=-1;i++)
    {                  
        //store it in input at the counter spot
        list1[i] = input;
        lim1 = i;
        if(i==9)
            break;
        //get another input
        cin >> input;
    }
    cout << "Enter a sorted array of no more than 10 elements: ";
    //READING INPUT
    cin >> input;
    for(j=0;j<10&&input!=-1;j++)
        {                  
        //store it in input at the counter spot
        list2[j] = input;
        lim2 = j;
        if(i==9)
            break;
        //get another input
        cin >> input;
    }    
    int count1(0), count2(0);
    
    for(i = 0; i < lim1 + lim2; i++)
    {if(list1[count1] < list2[count2])
        {merge[i] = list1[count1];
        count1++;}
    else
    {merge[i] = list2[count2];
    count2++;}
    } 
    
    for(i=0;i<lim1+lim2;i++)
        cout << merge[i] << " ";
    
    /*for(i=0;i<lim1;i++)//take the first 10 values from list1
       { merge[i] = list1[i];
        }
    for(i=0;i<lim2;i++)//take the next 10 values from list2
        {merge[i+10] = list2[i];
        }
    
    for(i=0;i<lim1+lim2;i++)//test output
        cout << merge[i] << " ";*/
    
    cout << endl;
    system("pause");
    return 0;
}
closed account (j3Rz8vqX)
Insert this on line 34 to help you understand the following for-loop:
 
    cout<<"Limit1: "<<lim1<<", Limit2: "<<lim2<<endl;

Because limit is definitely not what you want it to be.

Hint: "i" started at 0, limit should start at 1.

Example:
1 element at index [0].

Another Hint: When was limit altered? (lim1 and lim2)

Edit Hint: Lines 14 and 27.
Last edited on
I fixed the problem with the limits, and now it isn't cutting off the last two inputs (thank you!), but there is a problem with inputting multiple-digit numbers (such as 10, 100, etc.), and it will default their values to -2.
Last edited on
closed account (j3Rz8vqX)
Update: Found your problem.

You were going out of bounds.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    for(i = 0; i < lim1 + lim2; i++)
    {
        cout<<"1: "<<list1[count1]<<", 2: "<<list2[count2]<<endl;
        if(list1[count1] < list2[count2])
        {
            merge[i] = list1[count1];
            count1++;
        }
        else
        {
            merge[i] = list2[count2];
            count2++;
        }
    }

Insert the bolded line to your program to debug your problem.
1: is array 1's value, 2: is array 2's value.

You can do it!
Test input: 100, -1, 100, -1

You will get:
1[0]:100, 2[0]:100
1[0]:100, 2[1]:20


Taking 100, from array 2 because
list1[count1] < list2[count2]
list1 was not less than list2, it was equal. And taking 20 because 100 is greater than 20.

Where'd the twenty come from? undefined behavior because you are accessing data that hasn't been assigned a value.
Last edited on
Topic archived. No new replies allowed.