Need help why is it keep looping? :(

Apr 27, 2012 at 7:39pm
So this is my code but its not printing anything....

#include<iostream>
using namespace std;

int main()
{
const int SKIP=-999999;
const int SIZE=20;
const int ROWS=4;
const int COLS=5;
int unsorted_array[ROWS][COLS]= { 16,22,99,41,18,
-258,4,101,5,98,
105,6,15,2,45,
33,88,72,16,3} ;
int s_index = 0;
int min_value,rowMin,
rowCol,row,
col,colMin;

int sorted[SIZE];

cout <<"Array Sorted"<<endl
<<"___________"<<endl<<endl;

while (s_index<(ROWS*COLS))
{
min_value = unsorted_array[0][0];
rowMin = 0;
rowCol=0;
row=0;
while (row<ROWS)
{
col=0;
while (col<COLS)
{
if (unsorted_array[row][col]< min_value)
{
min_value = unsorted_array[row][col];
rowMin = row;
colMin = col;
}
col=col+1;
}
;
row=row+1;
}

sorted[s_index]= min_value;

if (sorted[s_index]>=0)
{
cout<<" "<<sorted[s_index];
unsorted_array[rowMin][colMin]=SKIP;
}
s_index=s_index+1;
}
cout<<endl;
Last edited on Apr 27, 2012 at 8:53pm
Apr 27, 2012 at 8:21pm
thats because you are not intructing your program to wait after it has displayed somthing. It displays it, then goes on to the next instruction, and eventually ends.
Apr 27, 2012 at 8:51pm
ok now its keep looping....

const int SKIP=-999999;
const int SIZE=20;
const int ROWS=4;
const int COLS=5;
int unsorted_array[ROWS][COLS]= {(16,22,99,41,18),
(-258,4,101,5,98),
(105,6,15,2,45),
(33,88,72,16,3)};
int s_index=0;
int min_value,rowMin,rowCol,row,col,colMin;
int sorted[SIZE];

cout <<"Array Sorted"<<endl
<<"___________"<<endl<<endl;


while (s_index<SIZE)
{
rowMin=0;
rowCol=0;
min_value = unsorted_array[0][0];
row=0;
while (row<ROWS)
{
col=0;
while (col<COLS)
{
if (unsorted_array[row][col]< min_value)
{
min_value = unsorted_array[row][col];
rowMin = row;
colMin = col;

}
;
col=col+1;
}
row=row+1;
}
sorted[s_index]= min_value;

while (sorted[s_index]>=0)
{
cout<<" "<<sorted[s_index];

}
unsorted_array[rowMin][colMin]=SKIP;
s_index=s_index+1;
}
Topic archived. No new replies allowed.