# How to find a Max in an Array in C

Page 1 of 1

## 5 Replies - 1144 Views - Last Post: 31 August 2011 - 07:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=245505&amp;s=79c8bb75ddab9965e472082ddef4a215&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sandaruwanc

Reputation: 0
• Posts: 71
• Joined: 31-August 11

# How to find a Max in an Array in C

Posted 31 August 2011 - 12:06 AM

I Have a Question like this...

A TV channel rating company requires a small computerized system to store and
analyze total no of TV viewers for different time slots throughout the day among few
popular TV Channels. There are 10 popular TV Channels and the survey was conducted
during a month for one hour time slots from 4.00pm to 11.00pm.
Write a program in C for the following:
(i.) Input data to a Matrix U [M, N].
(ii.) Find the sum of the TV viewers for each time slot and store the results in a Vector V [M].
(iii.) Find the sum of the TV viewers for each channel and store the results in a Vector W [N].
(iv.) Use the Vector V [M] to find the best time slot.( best time slot should have viewers
for each channel).
(v.) Use the Vector W [N] to find the best Channel.( best Channel should have viewers
for each time slot).
(vi.) Calculate total number of viewers for that month.
Hint: write separate functions to each and every activity and call them in the main function

I Can't understand how to do iv and v parts of that question...
This is my Existing code

```#include <stdio.h>
#include <conio.h>

#define M 3 /*row*/
#define N 4/*column*/

void addmatrix(int array[M][N], int arr [M]);
void findmax(int a[M],int x);

int main(){

//   clrscr();

int U[M][N]={0};
int V[M]={0};
//  int W[N]={0};

int b=0;

findmax(V,B)/>;
// printf("%d",B)/>;

getch();
}

// Starting Functions.....

for (int i =0; i<M; i++){
for ( int j=0; j<N; j++){
printf("Input your value to [%d] th [%d] th location : ",i,j);

scanf("%d",&array[i][j]);
}
}

}

{
for(int j=0; j<M ; j++){
for(int k=0; k<N; k++){
arr[j]+=array[j][k];
}
printf("%d \t",arr[j]);
}
printf("\n\n\n\n");
}

// Find my Max ... ... ...

void findmax (int f[M], int c){
c=f[0];

for (int i=1; i<M; i++){
if (c<f[i]){
c=f[i];
}
}
printf("%d",c);
}

//End of max........

// End of functions....

```

Is This A Good Question/Topic? 0

## Replies To: How to find a Max in an Array in C

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How to find a Max in an Array in C

Posted 31 August 2011 - 12:29 AM

Print up your Vectors, just one at a time, and see how you'd solve this with paper and pencil. Work up a couple sample sets of data for the Vectors.

You'll see what you need to do, before long. At least, that's what I would have to do, since the answer is not simply the max viewers for an hour's time slot, which I would have thought would be the case.

### #3 sandaruwanc

Reputation: 0
• Posts: 71
• Joined: 31-August 11

## Re: How to find a Max in an Array in C

Posted 31 August 2011 - 12:41 AM

Adak, on 31 August 2011 - 12:29 AM, said:

Print up your Vectors, just one at a time, and see how you'd solve this with paper and pencil. Work up a couple sample sets of data for the Vectors.

You'll see what you need to do, before long. At least, that's what I would have to do, since the answer is not simply the max viewers for an hour's time slot, which I would have thought would be the case.

I can't understand how to slove this sir.. Don't you have an Idea??

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How to find a Max in an Array in C

Posted 31 August 2011 - 12:57 AM

For IV, best time slot, I would have thought it would be the time slot with the highest number of viewers - regardless of the distribution in the channels.

Similarly for V. Best channel should have the most viewers, not have viewers distributed into the most time slots.

But that's me, and not your problem.

That's why I say print out the vectors[], and then work it through by hand a time or two, and see just what it is that they're asking for. Maybe you need to (say for V, best Channel), count up which channel has the most time slots, with viewers (the widest distribution). Same idea for IV, look for the broadest distribution, rather than the total.

### #5 baavgai

• Dreaming Coder

Reputation: 6605
• Posts: 13,945
• Joined: 16-October 07

## Re: How to find a Max in an Array in C

Posted 31 August 2011 - 04:11 AM

Quote

Hint: write separate functions to each and every activity and call them in the main function

Why not start there?

e.g.
```#define M 3 /*row*/
#define N 4 /*column*/

typedef int Matrix[M][N];
typedef int VectorM[M];
typedef int VectorN[N];

/* (i.) Input data to a Matrix U [M, N]. */

/* (ii.) Find the sum of the TV viewers for each time slot and store the results in a Vector V [M]. */
void findSumForM(Matrix, VectorM);

/* (iii.) Find the sum of the TV viewers for each channel and store the results in a Vector W [N]. */
void findSumForN(Matrix, VectorN);

/* (iv.) Use the Vector V [M] to find the best time slot.( best time slot should have viewers
for each channel). */
/* this is sneaky, you need the matrix as well */
int findBestTimeSlot(Matrix, VectorM);

/* (v.) Use the Vector W [N] to find the best Channel.( best Channel should have viewers
for each time slot). */
int findBestChannel(Matrix, VectorN);

/* (vi.) Calculate total number of viewers for that month. */
int getTotal(Matrix);

/* I'm unclear what these two apply to */
void findmax(int a[M],int x);

```

Hope this helps.

### #6 sandaruwanc

Reputation: 0
• Posts: 71
• Joined: 31-August 11

## Re: How to find a Max in an Array in C

Posted 31 August 2011 - 07:52 AM

baavgai, on 31 August 2011 - 04:11 AM, said:

Quote

Hint: write separate functions to each and every activity and call them in the main function

Why not start there?

e.g.
```#define M 3 /*row*/
#define N 4 /*column*/

typedef int Matrix[M][N];
typedef int VectorM[M];
typedef int VectorN[N];

/* (i.) Input data to a Matrix U [M, N]. */

/* (ii.) Find the sum of the TV viewers for each time slot and store the results in a Vector V [M]. */
void findSumForM(Matrix, VectorM);

/* (iii.) Find the sum of the TV viewers for each channel and store the results in a Vector W [N]. */
void findSumForN(Matrix, VectorN);

/* (iv.) Use the Vector V [M] to find the best time slot.( best time slot should have viewers
for each channel). */
/* this is sneaky, you need the matrix as well */
int findBestTimeSlot(Matrix, VectorM);

/* (v.) Use the Vector W [N] to find the best Channel.( best Channel should have viewers
for each time slot). */
int findBestChannel(Matrix, VectorN);

/* (vi.) Calculate total number of viewers for that month. */
int getTotal(Matrix);

/* I'm unclear what these two apply to */
void findmax(int a[M],int x);

```

Hope this helps.

I have stucked.. addmatrix is for add the tv viewers to a 1D array... Find max is incomplete.. I need help for that.. I can't understand, how to satisfy two requirments in the Question.. ... Plz help me.....