d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need C++ Help
Add Reply New Topic New Poll
Member
Posts: 13,899
Joined: Aug 14 2009
Gold: 1,399.00
Oct 3 2013 11:28pm
I need a program that calculate the sum, average and the numer that more repeats with a matrix of 2 arrays..
i need to ask the dimensions of the matrix.
Here:
it can sum its elements and it can calculate its average but i dont know how to print the numer that more repeats (i can put a counter to print how many times but dunno how to print the exactly number)


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

using namespace std;

int i,j;
int arra [10][10];
int f=0,c=0;
int sum, avg, mod=0;

int main (){


cout << "Number of files?" << endl;
cin >> j;


cout << "Number of columns?" << endl;
cin >> i;


if (j<=10 && i<=10){

cout << "Give me the elements:" << endl;

for (f=0; f<j; f++){
for (c=0; c<i; c++){

cin >> arra [f][c];
avg += arra [f][c];

}
}
}

cout << "Avg is:" << avg /j*i << endl;
cout << "Sum is:" << avg << endl;
cout << "Mod is:" << mod << endl;
getch();
return 0;
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 3 2013 11:38pm
hashtable is one method. each number you see becomes the key and the value is the count. print the key with the highest value. alternatively you can flatten it to a 1 dimensional array and sort it so everything's together, then iterate over it counting up which is the most seen. you'll have to think about what you're doing if more than one number repeats the most.

This post was edited by carteblanche on Oct 3 2013 11:39pm
Member
Posts: 13,899
Joined: Aug 14 2009
Gold: 1,399.00
Oct 3 2013 11:49pm
Quote (carteblanche @ Oct 4 2013 12:38am)
hashtable is one method. each number you see becomes the key and the value is the count. print the key with the highest value. alternatively you can flatten it to a 1 dimensional array and sort it so everything's together, then iterate over it counting up which is the most seen. you'll have to think about what you're doing if more than one number repeats the most.


still confused on how to do this can u explain it to me? :P
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2013 10:36pm
ok. array method.

suppose your matrix is this:

8 2 2
5 4 2
1 1 9

you can construct an array like so:
8 2 2 5 4 2 1 1 9

now you can sort it

1 1 2 2 2 4 5 8 9

you'll notice all the identical numbers are together. now you can iterate over it, comparing the left and right indexes to see if the number changed. and you can use a counter to see how long this stream of consecutive numbers is. you'll see that 2 has the longest consecutive run with cardinality 3, so 2 is the answer.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll