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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I count elements in Arrey

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.
 
You have to make an array (or vector) of structs,
with a name and a frequency.
then make a loop that runs through all the elements
of the array and:
if the name is not yet in the new array,
add it and put frequency on 1
else
add one to the freqency of that element in the list

got it ? Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top