Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

HElp reg a program in C multi dim array

Status
Not open for further replies.

thecall

Junior Member level 2
Joined
Mar 4, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,448
while learning C i am working on multidimensional arrays.... i am trying to rite a program in C to

pick up the largest number from any 5 row by 5 column matrix

i did it in one dimension by first sorting the array and then printing the last digit of array but i am lost in multidim please help dont give me prog just need to know the logic or way plz help
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
You can do this with a nested loop.

Outer loop
Loop through the rows, first row
array[0][0];

Inner Loop
Loop through the column and save biggest number;
array[0][1];
array[0][2];
etc.
array[0][MAX];


Then next row
array[1][0];

etc.

Last row
array[MAX][0];
 

thecall

Junior Member level 2
Joined
Mar 4, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,448
thnx trying it again
 

thecall

Junior Member level 2
Joined
Mar 4, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,448
But how to find the largest no among a row or coulmn

should we sort it first an then print or tell any esy approach
 

M!k

Advanced Member level 2
Joined
Apr 22, 2002
Messages
682
Helped
109
Reputation
218
Reaction score
35
Trophy points
1,308
Activity points
4,822
You don't have to sort the array. Just do a compare inside the inner loop and compare if the current value of the array is bigger than a variable which stores a "highscore".
If it's bigger, store the array's value in the highscore variable.
E.g.:
highscore = 0; // initialize highscore with 0 for unsigned type, -xyz for signed type
(outer loop)
(inner loop)
if (array[x][y] > highscore) highscore = array[x][y]; // remember highest value
(end of inner loop)
(end of outer loop)
printf(Highest value: , %...", highscore); //... depends on type of array
 

thecall

Junior Member level 2
Joined
Mar 4, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,448
i wrote this code its working but its tooo long give suggstion how to optimize this code.


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n[5][5]={
2,4,7,5,9,
6,8,9,56,8,
3,5,6,57,89,
1,2,5,4,8,
5,8,6,3,200
};
int i,j,m,k,o,p,q;

printf("Matrix is\n {");

for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
printf(" %d",n[j]);
if(j==4)
{
printf("\n");
}
}
}
printf("}\n\n\n");



for(j=0;j<=4;j++)
{

if(n[0][j]>n[0][0])
{
m=n[0][j];
n[0][j]=n[0][0];
n[0][0]=m;

}

}

for(j=0;j<=4;j++)
{

if(n[1][j]>n[1][0])
{
//n[1][0]=n[0][j];
k=n[1][j];
n[1][j]=n[1][0];
n[1][0]=k;
}

}
for(j=0;j<=4;j++)
{

if(n[2][j]>n[2][0])
{
//n[1][0]=n[0][j];
o=n[2][j];
n[2][j]=n[2][0];
n[2][0]=o;
}
}
for(j=0;j<=4;j++)
{

if(n[3][j]>n[3][0])
{
//n[1][0]=n[0][j];
p=n[3][j];
n[3][j]=n[3][0];
n[3][0]=p;
}

}
for(j=0;j<=4;j++)
{

if(n[4][j]>n[4][0])
{
//n[1][0]=n[0][j];
q=n[4][j];
n[4][j]=n[4][0];
n[4][0]=q;
}
}
if(m>k)
{
if(m>o)
{
if(m>p)
{
if(m>q)
{
printf("biggest no %d",m);
}
}
}
}
if(k>m)
{
if(k>o)
{
if(k>p)
{
if(k>q)
{
printf("biggest no %d",k);
}
}
}
}

if(o>m)
{
if(o>k)
{
if(o>p)
{
if(o>q)
{
printf("biggest no %d",o);
}
}
}
}

if(p>m)
{
if(p>k)
{
if(p>o)
{
if(p>q)
{
printf("biggest no %d",p);
}
}
}
}


if(q>m)
{
if(q>k)
{
if(q>p)
{
if(q>o)
{
printf("biggest no %d",q);
}
}
}
}


getch();

}
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Code:
#include <stdio.h>
#include<conio.h>

#define ROW_SIZE  5
#define COL_SIZE  5

void main(void)
{
int array[ROW_SIZE][COL_SIZE]=
{{2,4,7,5,9},{6,8,9,56,8},{3,5,6,57,8},{1,2,5,4,8},{5,8,6,3,200}};

int ix, iy, max = 0;

clrscr();   /* In C, you cant call any function before variable declaration */

for(ix = 0; ix < ROW_SIZE; ix++)
  {
  for(iy = 0; iy < COL_SIZE; iy++)
    {
    if(max < array[ix][iy])
      {
      max = array[ix][iy];
      }
    }
  }

printf("Max size is %d \n", max);
getch();
}
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top