5 Replies - 758 Views - Last Post: 31 August 2011 - 07:52 AM Rate Topic: -----

#1 sandaruwanc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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 addingdata(int array[M][N]);
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;



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

   getch();
}

// Starting Functions.....

void addingdata(int array[M][N]){

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

}


void addmatrix(int array[M][N],int arr[])
{
	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....




Please help me to do this...

Is This A Good Question/Topic? 0
  • +

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

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 sandaruwanc  Icon User is offline

  • D.I.C Head

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

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

Posted 31 August 2011 - 12:41 AM

View PostAdak, 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??
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • 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]. */
void addingdata(Matrix);

/* (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 addmatrix(Matrix, int arr[]); 
void findmax(int a[M],int x);



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 sandaruwanc  Icon User is offline

  • D.I.C Head

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

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

Posted 31 August 2011 - 07:52 AM

View Postbaavgai, 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]. */
void addingdata(Matrix);

/* (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 addmatrix(Matrix, int arr[]); 
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.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1