Array problem

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

#include <iostream>
#include<iomanip>

using namespace std;

bool exists(int array[], int length, int newnum)
{
     for (int i=0; i<length; i++)
     {
         if (newnum == array[i])
         {
                     
                         cout<<"true"<<endl;
                      return true;
              
         }
       
         
     }
     return false;
}

int main()
{
    
    int count208, num208=0, countMajor, Majornum=0;
    int cosc208[25];
    int CTECstudents[25];
        for(int index=0; index<25;index++)
        {
                cosc208[index]=-1;
                CTECstudents[index]=1;
        }
        
        cout << "Enter the number of students who are taking COSC 208"<<endl;
         cin >> count208;
         for(int i=0;i<count208;i++)
         {
            
                cout << "Enter the next id of a 208 student:" <<endl;
                 cin >> num208;
    
   
                 while(exists(cosc208,count208,num208) == true)
                 {
                       cout << "Enter a unsued id of a 208 student:" <<endl;
                        cin >> num208;
                 }


                 cosc208[i]=num208;
                 for(int t=0;t<i+1;t++)
                 {
                       cout << cosc208[t]<<" ";
                 }

         }

         cout << "Enter the number of students who are majoring in CTEC"<<endl;
           cin >> countMajor;
         for(int i1=0;i1<countMajor;i1++)
         {
               cout << "Enter the next id of a CTEC major" <<endl;
                cin >> Majornum;
                while(exists(CTECstudents,countMajor,Majornum) == true)
                {
                      cout <<"Enter a unsued id of a CTEC major:" <<endl;
                      cin >> Majornum;
                }

                CTECstudents[i1]=Majornum;
                 for(int we=0;we<i1+1;we++)
                 {
                       cout << CTECstudents[we]<<" ";
                 }
          }

          cout <<"This is the differense of the 2 arrays" << endl;
          
int ntCtec[50];
bool flag=false;
bool flag1=false;
int v=0;
int m=v+1;
for(int y=0; y<count208;y++)
{
        ntCtec[y]=-1;
        }
for(int k=0; k < count208; k ++)
{ 
        for(int q=0; q<countMajor; q++)
        {
                if(cosc208[k]==CTECstudents[q])
                flag=true;
                }
                if (flag==false)
                cout<<cosc208[k]<<" ";
                }
                
                
        for(int p=0; p < countMajor; p ++)
{ 
        for(int n=0; n<count208; n++)
        {
                if(cosc208[n]==CTECstudents[p])
                flag1=true;
                }
                if (flag1==false)
                cout<<CTECstudents[p]<<" ";
              
                }
        
         
system("pause");
return 0;

}













this program is supposed to take imputs for to arrays and compare the arrays to see the differences and then output the arrays onto the print screen
i am having trouble with printing the differences of the two arrays if you run the program you will understand you cannot enter the same id twice in one array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main(int argc, char *argv[] )
{
    int a[6]={1,2,15,4,5,6};
    int b[5]={2,-1,6,10,15};
    
    int j;
    bool b_in_a;
    for(int i=0; i<6; ++i){
        j=0;
        b_in_a=false;
        while(!(b_in_a) && j<5)
            if(a[i]==b[j++]) b_in_a=true;
        if(b_in_a) cout << "b[ " << j-1 << " ] = " << b[j-1] << "\tis a[ " << i << " ]"<< endl;
    }
    
    
    return 0;
}
1
2
3
4
5
6
7
8
9
10
for(int k=0; k < count208; k ++)
{ 
        for(int q=0; q<countMajor; q++)
        {
                if(cosc208[k]==CTECstudents[q])
                flag=true;
         }
          if (flag==false)
         cout<<cosc208[k]<<" ";
 }


I just marked the error in italic, you need to add a break after a pair of elements are matched.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        for(int q=0; q<countMajor; q++)
        {
                if(cosc208[k]==CTECstudents[q])
                {
                      flag=true;
                      break;
                 }

         }
               if (flag==false)
               {
                     cout<<cosc208[k]<<" ";
                }
                flag = false;
        }
            
Topic archived. No new replies allowed.