This is my instructions


and i am using Dev-C++ 4.9.9.2
and i am using the Mingw compiler

i am a beginner.

and this is my code.

#include <iostream>
#include <iomanip>

using namespace std;
void everything();
int main()
{  
    everything();

    system("pause");
    return 0;
}
void everything()
{
       int a,b,k,sum;
    bool flag;
  
    cout<<"Please enter the amount of students taking COSC 208"<<endl;
   cin>>a;
  int cosc208[a];
  int Intersection[a];
   cout<<"Please enter the ID's for all the students who are taking COSC 208 and press enter"<<endl;
   for (int i=0; i<a; i++)
    cin>>cosc208[i];
    cout<<"Please enter the amount of studnts whose major is CTEC."<<endl;
  cin>>b;
 int CTECstudents[b];
    cout<<"Please enter the ID's for all the students with CTEC as thier major."<<endl;
    for (int i=0; i<b; i++)
    cin>>CTECstudents[i];
    for(int i=0;i<a; i++)
    {
    flag==false;
    for(int j=0;j<b; j++)
    {
    if(cosc208[i]==CTECstudents[j])
    flag==true;
    if (flag==true)
    {
    Intersection[k]=cosc208[i];  
    sum=k++;
}
    cout<<sum<<endl;
}
    }
    int i=0;
    while(i<k)
    {
     cout<<"The CTEC students taking COSC 208 are: "<<Intersection[i]<<endl;
     i++;
     }

}


i just can't get the intersection array to cout right

Attachments IMG01102-20100920-2323.jpg 580.02 KB IMG01103-20100920-2323.jpg 580.73 KB

How do you want it to look? If you only want "The CTEC students taking COSC 208 are: " to appear once, take it out of your loop. It's a pretty simple fix.

Is your code even compiling? The first error I see is on line 20 where the array size is a non-const integer.

You never initialize Intersection or k before you use them. As a result, this line Intersection[k]=cosc208[i]; winds up pointing somewhere completely off the wall. Pay attention to your initializations.

Is your code even compiling? The first error I see is on line 20 where the array size is a non-const integer.

On many compilers it won't. I can't compile it on VC++2008, but I can on Code::Blocks. Some newer ones allow it and (supposedly) it will be allowed in C++0x.

Edited 6 Years Ago by Fbody: n/a

Hey,

First thing I noticed is that sum isn't initialized. It's possible to go through your nested for loops and for it to find no comparison. It will then cout an uninitialized variable on line 43.

For that mater, I don't think you initialized k either.

Set k = 0 and Sum = 0 and see if that helps.

One other thing I noticed ...

sum=k++;

Line 41, I believe (once k is initialized) sum will be set to 0 the first iteration of the loop, and k will be incremented. If you want sum to be equal to 1 on the first iteration you will need

sum = ++k;

Edit:

What Fbody said :)

Edited 6 Years Ago by kes166: n/a

This is my new code

#include <iostream>
#include <iomanip>

using namespace std;
void everything();
int main()
{  
    everything();

    system("pause");
    return 0;
}
void everything()
{
       int a,b,k=0,sum=0;
    bool flag;
  
    cout<<"Please enter the amount of students taking COSC 208"<<endl;
   cin>>a;
  int cosc208[a];
  int Intersection[a];
   cout<<"Please enter the ID's for all the students who are taking COSC 208 and press enter"<<endl;
   for (int i=0; i<a; i++)
    cin>>cosc208[i];
    cout<<"Please enter the amount of studnts whose major is CTEC."<<endl;
  cin>>b;
 int CTECstudents[b];
    cout<<"Please enter the ID's for all the students with CTEC as thier major."<<endl;
    for (int i=0; i<b; i++)
    cin>>CTECstudents[i];
    for(int i=0;i<a; i++)
    {
    flag==false;
    for(int j=0;j<b; j++)
    {
    if(cosc208[i]==CTECstudents[j])
    flag==true;
    if (flag==true)
    {
    Intersection[k]=cosc208[i];  
    sum=++k;;
}
    cout<<sum<<endl;
}
    }
    int i=0;
    while(i<k)
    {
     cout<<"The CTEC students taking COSC 208 are: "<<Intersection[i]<<endl;
     i++;
     }

}

I initialized k in line 15 and intersection in line 21 and i use mingw compile and it does compile

Edited 6 Years Ago by TinhornAdeniyi: n/a

This really doesn't tell us much. What are your new results?

Line 21 is the declaration of Intersection, there is no initialization. It really should be this:

int Intersection[a] = {0};

That will set all elements to 0 at the outset.

Before i changed int Intersection[a]={0}

i got:
Please enter the amount of students taking COSC 208
5
Please enter the ID's for all the students who are taking COSC 208 and press ent
er
11
12
13
2
22
Please enter the amount of studnts whose major is CTEC.
5
Please enter the ID's for all the students with CTEC as thier major.
11
12
13
2
22
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Press any key to continue . . .

after i changed it....it would compile and i got the following results

21 C:\Users\Fawaz\Documents\COSC 113\PA1.cpp variable-sized object `Intersection' may not be initialized


i think the problem is the sum
it keeps going back to zero for some reason.

Edited 6 Years Ago by TinhornAdeniyi: n/a

No, it's something in here

for(int i=0;i<a; i++)
{
  flag==false;
  for(int j=0;j<b; j++)
  {
    if(cosc208[i]==CTECstudents[j])  //<---comparison never executes
      flag==true;
    if (flag==true)
    {
      Intersection[k]=cosc208[i];  
      sum=++k;;
    }
    cout<<sum<<endl;
  }
}

When I step it through a debugger, it seems that it never executes the comparison on Line 6. There is a logic error somewhere in there that I'm not seeing.

Also, I just noticed that Lines 3 and 7 are equality comparisons, not assignments. Remove one of the '=' from the statements. When I did, it started performing the comparison. It seems that the compiler optimized its execution away because it didn't do anything anyway.

There are still a few things missing and some logic that needs correcting, but I'll leave those for you to dig in to once you get this working properly.

for(int i=0;i<a; i++){
  flag==false;
  for(int j=0;j<b; j++){
    if(cosc208[i]==CTECstudents[j])  //<---comparison never executes
      flag==true;
    if (flag==true){
      Intersection[k]=cosc208[i];  
      sum=++k;;
    }
    cout<<sum<<endl;
  }
}

Few things I've noticed. flag == false; should be flag = false; and flag == true; should be flag = true;

also, sum=++k;; should be sum=++k;

I'm not seeing anything else.

Before i changed int Intersection[a]={0}

i got:
Please enter the amount of students taking COSC 208
5
Please enter the ID's for all the students who are taking COSC 208 and press ent
er
11
12
13
2
22
Please enter the amount of studnts whose major is CTEC.
5
Please enter the ID's for all the students with CTEC as thier major.
11
12
13
2
22
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Press any key to continue . . .

after i changed it....it would compile and i got the following results

21 C:\Users\Fawaz\Documents\COSC 113\PA1.cpp variable-sized object `Intersection' may not be initialized


i think the problem is the sum
it keeps going back to zero for some reason.

Yeah, the string of zeros came from not having assigned flag to anything. make it one = instead of two, and see what you get.

I have made some revision to the code as per your advice (Thank you!)

and this is the code

#include <iostream>
#include <iomanip>

using namespace std;
void everything();
int main()
{  
    everything();

    system("pause");
    return 0;
}
void everything()
{
       int a,b,k=0,sum=0,i,j;
    bool flag;
  
    cout<<"Please enter the amount of students taking COSC 208"<<endl;
   cin>>a;
  int cosc208[a];
  int Intersection[a];
   cout<<"Please enter the ID's for all the students who are taking COSC 208 and press enter"<<endl;
   for (int i=0; i<a; i++)
    cin>>cosc208[i];
    cout<<"Please enter the amount of studnts whose major is CTEC."<<endl;
  cin>>b;
 int CTECstudents[b];
    cout<<"Please enter the ID's for all the students with CTEC as thier major."<<endl;
     
    for (i=0; i<b; i++)
    cin>>CTECstudents[i];
    cout<<"The CTEC students taking COSC 208 are: ";
    for(i=0;i<a; i++)
    {
    flag=false;
    for(j=0;j<b; j++)
    {
    if(cosc208[i]==CTECstudents[j])
    {
    flag=true;
}
    if (flag=true)
    {
    Intersection[k]=cosc208[i];
   cout<<Intersection[k]<<" ";
   k++;
}
}
    }
}

and this is my result
Please enter the amount of students taking COSC 208
5
Please enter the ID's for all the students who are taking COSC 208 and press ent
er
11
12
13
2
22
Please enter the amount of studnts whose major is CTEC.
5
Please enter the ID's for all the students with CTEC as thier major.
22
33
44
55
66
The CTEC students taking COSC 208 are: 11 11 11 11 11 12 12 12 12 12 13 13 13 13
13 2 2 2 2 2 2 2 2 2 2 Press any key to continue . . .

I think fbody is right and there is something wrong with the comparisons
Thank you guys again

Edited 6 Years Ago by TinhornAdeniyi: To better show whats wrong with the code

for(i=0;i<a; i++){
        flag=false;
        for(j=0;j<b; j++){
            if(cosc208[i]==CTECstudents[j]){
                flag=true;
            }
            if (flag=true){ //this needs to be ==
                Intersection[k]=cosc208[i];
                cout<<Intersection[k]<<" ";
                k++;
            }
        }
    }

Your equals are messed up again.

When you are assigning a value to a variable, you use =
When you are doing a comparrison in an if statement, you use ==
in the above if statement, everytime that line is executed, flag is set to true, and that if statement will run every time.

Look at the first iterration of your loops.
a = 5 and b = 5 .. That won't change
i = 0, flag = false
j = 0, k = 0
It then asks if cosc208[0] = CTECstudents[0], it doesn't so it skips down.
It then sets flag = true
It then sets Intersection[0] = cosc208[0] = 11
k now equals 1 and the inner loop continues

j = 1
It then asks if cosc208[0] = CTECstudents[1], it doesn't so it skips down.
It then sets flag = true (note, it's already true, there's a logical error here)
It then sets Intersection[1] = cosc208[0] = 11
k now equals 2 and the inner loop continues.

etc etc

You need to fix your if statement, and reinitialize your flag for every interation of the inner loop, or better yet, get rid of the flag completly and change your if statement from

if (flag=true){ //this needs to be ==
    Intersection[k]=cosc208[i];
    cout<<Intersection[k]<<" ";
    k++;
}

to

if(cosc208[i]==CTECstudents[j]){ 
   Intersection[k]=cosc208[i];
   cout<<Intersection[k]<<" ";
   k++;
}

That'll allow you to completly remove your flag variable. Additionally, once you do find the match, you can break out of the inner loop since you already found a match, and don't need to continue the inner for loop.

Edited 6 Years Ago by kes166: n/a

You need to fix your if statement, and reinitialize your flag for every interation of the inner loop, or better yet, get rid of the flag completly and change your if statement from

if (flag=true){ //this needs to be ==
    Intersection[k]=cosc208[i];
    cout<<Intersection[k]<<" ";
    k++;
}

to

if(cosc208[i]==CTECstudents[j]){ 
   Intersection[k]=cosc208[i];
   cout<<Intersection[k]<<" ";
   k++;
}

That'll allow you to completly remove your flag variable. Additionally, once you do find the match, you can break out of the inner loop since you already found a match, and don't need to continue the inner for loop.

LOL, I was actually getting to this, but you beat me to it...

@OP:
You really should follow this suggestion. There are substantially fewer chances for logic errors. Especially the kinds of errors you are experiencing now.

Edited 6 Years Ago by Fbody: n/a

I think kes166 pointed out your problems.

I'd just suggest that when you compile your programs, pass the following switches to the compiler (via Dev-C++ IDE) -Wall -Wextra -pedantic That will enable most of the warnings and generally also warns about (some) non-standard code (like variable length arrays, which you are using).

Also, why not switch from Dev-C++ to say Code::Blocks, which comes with probably ~5 years more recent compiler (and IDE too).

Ladies and Gentlemen, it finally works.
I changed the comparisons to kes166's recommendations.
Here is the final code:

#include <iostream>
#include <iomanip>

using namespace std;
void everything();
int main()
{  
    everything();

    system("pause");
    return 0;
}
void everything()
{
       int a,b,k=0,sum=0,i,j;
  
    cout<<"Please enter the amount of students taking COSC 208"<<endl;
   cin>>a;
  int cosc208[a];
  int Intersection[a];
   cout<<"Please enter the ID's for all the students who are taking COSC 208 and press enter"<<endl;
   for (int i=0; i<a; i++)
    cin>>cosc208[i];
    cout<<"Please enter the amount of studnts whose major is CTEC."<<endl;
  cin>>b;
 int CTECstudents[b];
    cout<<"Please enter the ID's for all the students with CTEC as thier major."<<endl;
     
    for (i=0; i<b; i++)
    cin>>CTECstudents[i];
    cout<<"The CTEC students taking COSC 208 are: ";
    for(i=0;i<a; i++)
    {
    for(j=0;j<b; j++)
    {
    if(cosc208[i]==CTECstudents[j]){ 
   Intersection[k]=cosc208[i];
   cout<<Intersection[k]<<" ";
   k++;

}
}
}}

and this is my result:

Please enter the amount of students taking COSC 208
5
Please enter the ID's for all the students who are taking COSC 208 and press ent
er
11
12
13
2
22
Please enter the amount of studnts whose major is CTEC.
5
Please enter the ID's for all the students with CTEC as thier major.
22
33
44
2
55
The CTEC students taking COSC 208 are: 2 22 Press any key to continue . . .

Thank you guys so much.
I can assure you i will not only ask question but attempt to answer them(i've even started)
and to mitrmkar's question, i can't switch to code::blocks, because i am only in my second class of c++..... and we use dev C in everything. I wouldn't want it to work at home and not at school.

and can you go into more detail into how i will "pass the following switches to the compiler" " -Wall -Wextra -pedantic "

>> how i will "pass the following switches to the compiler

In the Dev-C++ IDE, there probably is something along the lines Project Options / Compiler Options, where you can specify these additional switches. If you cannot find anything like that, maybe ask your teacher.

This question has already been answered. Start a new discussion instead.