Having problems in scanning the array.

Good day guys, im having a problem on how to scan the multidimensional array..

i have a matrix[cols][rows];

how do i scan the matrix and find the lowest number in the matrix and also find the highest of it, and get the coordinates of each..

like example:

lowest number in matrix [ 21(0,0) get the x which is 0 ]
highest number in matrix [ 57(1,2) and the y which is 2 ]

all together the saddle point (0,2) which is 34.

... please help guys??
Last edited on
Well, first initialize the lowest and highest number to be matrix[0][0] (and set saddle point x and y to both be 0).

Then from there, it's a straight nested loop (in the loop, check the current number and set the new highest/lowest number and saddle point x/y as needed).

1
2
3
4
5
for(int x=0; x<cols; x++)
     for(int y=0; y<rows; y++)
     {
          //main scanning goes here  
     }
This is how you would find the lowest I think:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(){

	int firstlowest = 0;	
	int secondlowest = 0;
	int xcor = 0;
	int ycor = 0;

	int array[10][20];
	//assume the array has data

	firstlowest = array[0][0];

	for (int i = 0; i < 10; i++) {
		for (int k = 0; k < 20; k++){
			secondlowest = array[i][k];
			if (secondlowest < firstlowest) {
				firstlowest = secondlowest;
				xcor = i;
				ycor = k;}}}


Here's the full code, it may be wrong:

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
int main(){

	int firstlowest = 0;	
	int secondlowest = 0;
	int xcor = 0;
	int ycor = 0;

	int firsthighest = 0;
	int secondhighest = 0;
	int hxcor = 0;
	int hycor = 0;

	int array[10][20];
	//assume the array has data

	firstlowest = array[0][0];
	firsthighest = array[0][0];

	for (int i = 0; i < 10; i++) {
		for (int k = 0; k < 20; k++){
			secondlowest = array[i][k];
			if (secondlowest < firstlowest) {
				firstlowest = secondlowest;
				xcor = i;
				ycor = k;}}}

	
	for (int i = 0; i < 10; i++){
		for (int k = 0; k < 20; k++){
			secondhighest = array[i][k];
			if (secondhighest > firsthighest) {
				firsthighest = secondhighest;
				hxcor = i;
				hycor = k;}}}

	cout<<"Highest value in the array is "<<firsthighest<<endl;
	cout<<"Coordinates are "<<hxcor<<" and "<<hycor<<endl;
	cout<<"Lowest value in the array is "<<firstlowest<<endl;
	cout<<"Coordinates are "<<xcor<<" and "<<ycor<<endl;

	return 0;}
Last edited on
thanks guys.. one more thing how can i make an two dimensional array that needs the user to input for its size? ill post the full code.. please anyone can you help me?


this is my full code to make a program to find the saddle point. thanks creekist ^___^ got the idea but my program wont function the way i want it to be.. got some things to fix. but i need help ..




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

#include <stdio.h>
#include <conio.h>
#define max 5

void main(){

int m[max][max],inpc,inpr,i,j;
int firstlow=0,firsthigh=0,secondlow=0,secondhigh=0,xcor=0,ycor=0,hxcor=0,hycor=0;

clrscr();

printf("Enter number of columns: ");
scanf("%d",&inpc);
printf("\nEnter number or rows: ");
scanf("%d",&inpr);

m[inpc][inpr]=m[max][max];

printf("\nYour matrix is a %d by %d matrix.",inpc,inpr);

printf("\n\nEnter value of matrix: \n");
for(i=0;i<inpc;i++){
	for(j=0;j<inpr;j++){
		
			scanf("%d",&m[i][j]);
		
			}
		}
	
printf("\nYour matrix list: \n");
for(i=0;i<inpc;i++){
	for(j=0;j<inpr;j++){
	
		printf("%d  ",m[i][j]);
		}
	printf("\n");
	}
	
firstlow=m[0][0];
firsthigh=m[0][0];
	
for(i=0;i<inpc;i++){
	for(j=0;j<inpr;j++){
	
		secondlow=m[i][j];
		if(secondlow < firstlow){
			firstlow=secondlow;
			xcor=i;
			ycor=j;
			}}}
			
for(i=0;i<inpc;i++){
	for(j=0;j<inpr;j++){
	
		secondhigh=m[i][j];
		if(secondhigh > firsthigh){
			firsthigh=secondhigh;
			hxcor=i;
			hycor=j;
			}}}
			
		printf("Highest value in the matrix is %d",firsthigh);
		printf("Coordinates are col %d row %d",hxcor,hycor);
		printf("Lowest value in the matrix is %d",firstlow);
		printf("Coordinates are col %d row %d",xcor,ycor);
		
		getch();
		
		}



Last edited on
guys my problem right now is the input size from the user i don't know if my codes are right.. ill accept some suggestions please?? .. need ideas.. and also my main goal is to find the highest number and the lowest number of the array.. and combining the highest and the lowest .. example.. highest=45 coordinates(1,2) lowest=27 coordinates(2,0) .. then get the x coordinate of the highest also get the y coordinate of the lowest resulting to (1,0) saddle point=18.. that's what i want to do :( .. help me guys..
guys.. its okay i figured it out!! ^____^ thanks.. for your help, god bless..
Topic archived. No new replies allowed.