Problem with Pauses and functions

Hello everyone!

Next tuesday I'll have to do a programming examination at my university, so I've been exercising lately.

Everything went fine, except for this program.

The program's purpose is this:
Given a bidimensional array (matrix) and given an integer number "n", the program recognizes the numbers in the matrix that are repeated exactly "n" times in the matrix, puts them into an 1-dimensiona array and prints it.

The program works fine as long as "n" is 2 or more, but if "n" is 1, the program works fine but doesn't stop, so I can't read the results on the screen.

HOWEVER, if for some unknow reason i put a system("pause")BOTH BEFORE AND AFTER I DECLARE the stamp function, it works and stops before closing.

Can somebody explain me why?

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
#include<iostream>
#define dim 5
using namespace std;

bool alreadyhere (int a[],int x,int pos)
{ for (int i=0;i<pos;i++)
     {if (a[i]==x)
          {return true;
          }
     }
 return false;
}

int findimention (int m[dim][dim], int r)
{int d=0;
int cont =0;
for (int i=0;i<dim;i++)
  {for (int j=0;j<dim;j++)
      {cont=0;
       for (int a=0;a<dim;a++)
          {for (int b=0;b<dim;b++)
              {if (m[i][j]==m[a][b])
                  {cont ++;
                  }
              } 
          }
       if (cont==r)
           {d++;
           }
       }
  }
return d/r;
}

int* repeated (int m[dim][dim], int r)
{int* k=new int[findimention(m,2)];
int count=0;
int pos=0;
for (int i=0;i<dim;i++)
  {for (int j=0;j<dim;j++)
     {count =0;
      for (int a=0;a<dim;a++)
          {for (int b=0;b<dim;b++)
            {if(m[i][j]==m[a][b])
                {count ++;
                }
            } 
           }
      if (count == r && alreadyhere(k ,m[i][j], pos) == false)
         {k[pos]=m[i][j];
         pos++;
         }
      }
  }
return k;
}

void stamp (int* v, int z)
{for (int i=0;i<z;i++)
   {cout<<"\nElement"<<i+1<<"--->"<<v[i];
    }
}


int main ()
{int a[][dim]= {{13,13,14,22,8},{3,17,9,21,16},{33,27,44,51,17},{8,1,-3,22,4},{2,0,17,-9,-3}};
cout<<"Function REPEATED\n\n\nInsert number of repetitions--->";
int rep;
cin>>rep;
stamp ( repeated(a,rep),findimention(a,rep));     
system ("Pause");
return 0;
}
 
 




In particular, IT WORKS FINE if i write
1
2
3
4
5
6
7
...
int main()
{...
... 
system ("Pause"); // --->THIS IS WHAT I HAVE TO ADD TO MAKE IT WORK PROPERLY AND I DON'T KNOW WHY.
stamp ( repeated(a,rep),findimention(a,rep));     
system ("Pause");



I'd really appreciate some help, as it would be a pity to score a lower grade in the exam for this apparently simple problem.


:)
Last edited on
I shows the result before CRASHING if you pause in the right time, and that should give you a hint in the right direction. This is the offending line:
59
60
61
62
for (int i = 0; i < z; i++){
    cout<<"\nElement"<<i+1<<"--->"<<v[i]; // There is no v[1]
    // But the loop tries to execute 14 times.
}
Last edited on
Topic archived. No new replies allowed.