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 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.
 
There 2 ways u could get the count
1. Use a count var and whenever u set any value to a struct, increment it.
2.
//Initialize the var
for(int i =0; i<15; i++)
{
strcpy(sx.code,&quot;&quot;);
}
//add the values
.
.
.
//Since the max num of structs in Arr are 15
for(i =0; i<15; i++)
{
if(strcmp(sx.code,&quot;&quot;) < 0)
break;
}
//the value of i will give you the number of structs that uv
//set
 
Thanks for the responce.
But 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<iostream>
#include <string>
#include <fstream>
using namespace std;

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

struct sndex sx[15];

void addDatEntry(char* param);

void main(){


//Since the max num of structs in Arr are 15
for(int i =0; i<15; i++)
{
strcpy(sx.code,&quot;\0&quot;);
}

addDatEntry(&quot;s100&quot;);
addDatEntry(&quot;s100&quot;);
addDatEntry(&quot;s101&quot;);
addDatEntry(&quot;s102&quot;);
addDatEntry(&quot;s101&quot;);
}

void addDatEntry(char* param)
{
for(int i =0; i<15; i++)
{
if(strcmp(sx.code,&quot;&quot;) == 0)
{
strcpy(sx.code, param);
sx.freq = 1;
break;
}
else
{
if(strncmp(sx.code, param,4) == 0)
{
sx.freq++;
break;
}
}
}
}
This shoud work..

Thanks,
Preetham.
Rating is not important, but it helps..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top