CODE doesnt work, int error...

So i did a task to improve my programming a bit but obviously my code has problems after almost finish the task and testing it out it gives me this error and i have no clue how to fix IT.

Cannot convert int * to int

The error is where "int temp" is.

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
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
randomize();
int a[50][50];
int i,j, n, m;
 cout<<"Input N : ";
   cin>>n;
   cout<<"Input M: ";
   cin>>m;
   for(i=0; i<n; i++){
   	for(j=0; j<n; j++){
      a[i][j]=random(10);
      cout<<a[i][j]<<" ";
        }
      cout<<"\n";
        }
        if (a[j-1] > a[j])
      {
       int temp = a[j];
        a[j-1] = a[j];
        a[j] = temp;
        }

getch();
}


any idea?
Last edited on
a[j] is a pointer to an int.
temp is an int.

They're completely different things. You can't set temp, an int, to the same value as an int pointer.

What are you trying to do there?
bubble sort algorithm

The task was to get random numbers in order using bubble sort algorithm.
No no, I mean at that line, what int value are you trying to read? What is this:
int temp = a[j];
trying to do?

Did you mean something like
int temp = a[i][j]; ?
Saw the mistake, thanks

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
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main ()
{
randomize();
int a[50][50];
int i,j, n, m;
int temp;
 cout<<"Input N: ";
   cin>>n;
   cout<<"Input M: ";
   cin>>m;
   for(i=0; i<n; i++){
   	for(j=0; j<n; j++){
      a[i][j]=random(10);
      cout<<a[i][j]<<" ";
        }
      cout<<"\n";
        }
        if (a[j-1] > a[j])
      {
       temp = a[i][j];
       a[i][j]= a[i][j-1];
        a[i][j-1] = temp;
        }

getch();
}


figured out why temp wasnt working, now how do i output the result?

just like this :
1
2
3
a[i][j-1] = temp;
         cout<<a[i][j]<<" ";
        }

?
Last edited on
Topic archived. No new replies allowed.