can someone help me fix this code, please? Beginner

So basically I have 2 arrays, int A[6][8] and bool B[6][8].
What I need to do is to read from a file 48 numbers and put them in the array. Till there everything is fine.
After that I should assing bool valuables to the elements of B so that for every i in[0...5] and j in [0...7] B[i][j]=true if and only if every component of the row A[i] can be found in the column A[][j].
Then save the bool valuables of B into a file called output.

I basically got stuck at the bool part. Here is my code. I need to use while and if to solve it, and not do-while or for.. It might be really easy, but for me it's not !

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
#include<iostream>
#include<fstream>
using namespace std;
main()
{
 int A[6][8];
 int i=0, j=0;
 int B[6][8];
 ifstream IN("input.txt");
 while(i<6)
 {
  while(j<8)
  {
   IN>>A[i][j];
   cout<<A[i][j]<<endl;
   j++;
  }
  i++;
  j=0;
 }
 i=0;

 int  t=0, l=0, p=0;
 while(p<6)
 {
  while(t<8)
  {
   while(j<8)
   {
    if(A[i][t]==A[p][j])
    {
     cout<<l;
     l++;
    }
    j++;
   }
   cout<<l;
   cout<<"\n";
   i++;
   if(i==6)
   {
    if(l>=5)
    {
     B[i][j]=1;
    }
    else
    {
     B[i][j]=0;
    }
    l=0;
    i=0;
    t++;
   }
  }
  t=0;
  p++;
 }
 i=0;
 j=0;
 ofstream OUT("output.txt");
 while(i<6)
 {
  while(j<8)
  {
   if(B[i][j]==1)
    OUT<<B[i][j];  }

 }
}


Any help would be appreciated!
Last edited on
B should be of bool and not of int. Think of it as a grid of checkboxes.
Your formatting makes your code hard to read, especially since you're not allowed to use for loops.

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
#include <iostream>
#include <fstream>

using namespace std;

main()
{
    int A[6][8];
    int i=0, j=0;
    int B[6][8];
    ifstream IN("input.txt");

    while(i<6)
    {
        while(j<8)
        {
            IN>>A[i][j];
            cout<<A[i][j]<<endl;
            j++;
        }
        i++;
        j=0;
    }
    i=0;

    int  t=0, l=0, p=0;
    while(p<6)
    {
        while(t<8)
        {
            while(j<8)
            {
                if(A[i][t]==A[p][j])
                {
                    cout<<l;
                    l++;
                }
                j++;
            }
            cout<<l;
            cout<<"\n";
            i++;
            if(i==6)
            {
                if(l>=5)
                {
                    B[i][j]=1;
                }
                else
                {
                    B[i][j]=0;
                }
                l=0;
                i=0;
                t++;
            }
        }
        t=0;
        p++;
    }
    i=0;
    j=0;

    ofstream OUT("output.txt");

    while(i<6)
    {
        while(j<8)
        {
            if(B[i][j]==1)
                OUT<<B[i][j];
        }
    }
}


Why is line 45 if(l>=5)? Shouldn't it be if (l > 5) or if (l == 6)?

Also, you're missing a j = 0; somewhere between lines 40 and 56.

Lines 47 and 51 should use B[p][t] and not B[i][j].

For the loop beginning line 71, why are you just outputting a bunch of 1's? That doesn't tell you anything without the 0's too. You might want to add some line breaks too, so you end up with a 6x8 rectangle instead of a 48 long line. You also forgot to increment i and j at the end of those loops.
Last edited on
Topic archived. No new replies allowed.