Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Needed in counting elements in array

Status
Not open for further replies.

swaroop

Programmer
Feb 4, 2001
100
US
#include<iostream>
#include <string>
#include <fstream>
using namespace std;

struct sndex{
char code[4];
int freq;
};


int main(){

sndex sx[15];


strcpy (sx[0].code, &quot;S100&quot;);
//sx[0].code[4], '\0';
sx[0].freq = 1;

strcpy (sx[1].code, &quot;S100&quot;);
//sx[1].code[4], '\0';
sx[1].freq = 1;

strcpy (sx[2].code , &quot;S120&quot;);
// sx[2].code[4], '\0';
sx[2].freq = 1;

strcpy (sx[3].code , &quot;S130&quot;);
// sx[3].code[4], '\0';
sx[3].freq = 1;

strcpy (sx[4].code , &quot;S120&quot;);
// sx[4].code[4], '\0';
sx[4].freq = 1;

strcpy (sx[5].code , &quot;S130&quot;);
// sx[5].code[4], '\0';
sx[5].freq = 1;

strcpy (sx[6].code , &quot;S140&quot;);
// sx[6].code[4], '\0';
sx[6].freq = 1;

strcpy ( sx[7].code , &quot;S130&quot;);
// sx[7].code[4], '\0';
sx[7].freq = 1;

strcpy (sx[8].code , &quot;S150&quot;);
//sx[8].code[4], '\0';
sx[8].freq = 1;

strcpy (sx[9].code , &quot;S140&quot;);
// sx[9].code[4], '\0';
sx[9].freq = 1;
strcpy (sx[10].code , &quot;S150&quot;);
//sx[8].code[4], '\0';
sx[10].freq = 1;

cout << &quot;CODE FREQ&quot; << endl;
cout << &quot;============================&quot; << endl;

/*
for(int i =0; i<10; i++){

cout << sx.code << &quot; &quot; << sx.freq << endl;


}

*/

int N = 11;

int m=0;
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++) {
//if(sx==sx[j])sx[j]=' ';
if(!strcmp(sx.code,sx[j].code)){
sx[j].freq+=1;
cout << sx[j].code << &quot; &quot; << sx[j].freq<<endl;
}
}
}
return 0;

}


In the above code I wanted to count no of structs.
Someone give me a clue....

Thanks in advance.

Regards,

Swaroop.
 
I wanted to count the structures containing same values.
The result must look like this:

CODE FREQ
=========================
S100 2
S120 2
S130 3
S140 2
S150 2

Thanks in advance....

Regards,

Swaroop.
 
#include <vcl.h>
#pragma hdrstop
//***********************************************
//instance of tek-tips
//***********************************************
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
struct Sndex
{
char arr_code[4];
int int_freq;
};
class Cmain
{
private:
Sndex arr_Sn[15];
bool bool_first; //if it is first different data, it is true;
public:
Cmain()
{
bool_first = true;
int i;
Sndex *ptr_Sn ;
for(int j = 0;j< 15; j++)
{
ptr_Sn = &(this->arr_Sn[j]);
i = random(5);
switch (i)
{
case 0:
strcpy(ptr_Sn->arr_code, &quot;S100&quot;);
ptr_Sn->int_freq = 0;
break;
case 1:
strcpy(ptr_Sn->arr_code, &quot;S200&quot;);
ptr_Sn->int_freq = 0;
break;
case 2:
strcpy(ptr_Sn->arr_code, &quot;S300&quot;);
ptr_Sn->int_freq = 0;
break;
case 3:
strcpy(ptr_Sn->arr_code, &quot;S400&quot;);
ptr_Sn->int_freq = 0;
break;
default:
strcpy(ptr_Sn->arr_code, &quot;S500&quot;);
ptr_Sn->int_freq = 0;
break;
}
}
cout<<&quot;constuct successfully!&quot;<<endl;
}
~Cmain(){}
void fn_display();
void fn_sort_inner(Sndex *start, Sndex *ptr_end);
void fn_sort();
};
void Cmain::fn_display()
{
Sndex *ptr_Sn;
for(int i=0; i< 15;i++)
{
ptr_Sn = &(this->arr_Sn);
cout<<ptr_Sn->arr_code<<&quot;\t\t&quot;<<ptr_Sn->int_freq<<endl;
}
}
void Cmain::fn_sort_inner(Sndex *ptr_start, Sndex *ptr_end)
{
int i = 0;
Sndex *ptr_Sn;
Sndex *ptr_new_start;
for(ptr_Sn = ptr_start;ptr_Sn<=ptr_end;ptr_Sn++)
{
if(int z =( strcmp(ptr_Sn->arr_code, ptr_start->arr_code) == 0))
{
i++;
}
else
{
if(bool_first)
{
ptr_new_start = ptr_Sn;
this->bool_first = false;
}
}
}
cout<<ptr_start->arr_code<<&quot;\t\t&quot;<<i<<endl;
this->bool_first = true; //to prepare for the next loop;
if(ptr_new_start != ptr_end)
{
fn_sort_inner(ptr_new_start, ptr_end);
}
}
void Cmain::fn_sort()
{
fn_sort_inner(arr_Sn, arr_Sn+14);
}
#pragma argsused
int main(int argc, char* argv[])
{
Cmain *obj_main = new Cmain;
obj_main->fn_display();
getch();
cout<<&quot;CODE&quot;<<&quot;\t\t&quot;<<&quot;FREQ&quot;<<endl;
cout<<&quot;========================================&quot;<<endl;
obj_main->fn_sort();
delete [] obj_main;
getch();
return 0;
}

the code above is passed through in the IDE C++ Builder 5
the result is like this:

constuct successfully!
S500 0
S500 0
S200 0
S500 0
S300 0
S400 0
S200 0
S500 0
S200 0
S200 0
S200 0
S500 0
S100 0
S300 0
S400 0
CODE FREQ
========================================
S500 5
S200 5
S500 3
S300 2
S400 2
S200 4
S500 2
S200 3
S500 1
S100 1
S300 1

Well, you will notice that it is not fit your purpose. But it approachs your meaning. Please revise it yourself.
Now I am being confused. Anticipate it be helpful. If you
return your revised edition, thank you very much.


 
Why not put the array into a binary tree and for every time you add the same element you increment a counter in the node??? Then you could use the common binary traversal methods to output the values from smallest to largest etc. Also, you could keep the same array you have in the beginning but just put pointers to the elements in the array.

Matt
 
Well, searching the web you can find source code for a binary search tree. It would require a bit of modification to do what you need. A node would now look like

struct node
{
int data;
int count;
node* right,*left;
node():right(NULL),left(NULL),count(0){}
};

the implementation of a Binary Search Tree will not add duplicates so the count is for when you insert a number that already exists. Just increment the count for it.

If this seems a bit much, using the built in sorting algorithm qsort with a compare function you write will suffice as well

int compare(void* a,void* b)
{
<yourType>* aVal = (<yourType>*)a;
<yourType>* bVal = (<yourType>*)b;

return ( (*aVal <*bVal) ? -1:
(*aVal == *bVal) ? 0:1);
}

and pass this as the compare funciton to qsort along with your array and its size. This will sort the list in ascending order. Now a loop to go through each element can be used to output the counts for each value.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top