Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

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
 

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];
 

thnx trying it again
 

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
 

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
 

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();

}
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top