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

Printing from an array of type struct

Status
Not open for further replies.

lbucko

Programmer
Feb 13, 2002
11
0
0
IE
Have two problems, first of all I want to print the values from an array of type struct Res,
Code:
struct res {
	int doc_id;
	float score;
	};
typedef struct res Res;

Res results[NUM_SITES][100];//Array I want to print from
I've tried the following but can't seem to get it right
Code:
printf((results[i][j].score)"\n", results);
I want to print both score and doc_id, but can't get either at the moment, my syntax is probably totally wrong.

When I print out these results I also want to be able to accept or reject each entry ie. let the user type in "Y" or "N" after each entry. I presume I would just have to loop through the results in some manner but am not too sure. Any help please!
 
You probably want something like...

printf("Doc Id: %d, Score: %f\n", (results[j]).doc_id, (results[j]).score);

However, I have not tried this and I don't know if it will even compile.
In any case, you would have to create a loop inside a loop ( "j" inside "i" ).
It sounds a bit like homework so I don't want to just give it away.
 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define SIZE_X 5
#define SIZE_Y 5
#define MAX 100

typedef struct _RES
{
int iDocID;
int iScore;
} RES;

void Populate2DArray( RES res[][SIZE_X],int x,int y,int iMaxRand );
void Out2DArray( RES res[][SIZE_X],int x,int y );

int main( void )
{
RES results[SIZE_Y][SIZE_X];

Populate2DArray( results,SIZE_X,SIZE_Y,MAX );
Out2DArray( results,SIZE_X,SIZE_Y );

return 0;
}

void Populate2DArray( RES res[][SIZE_X],int iX,int iY,int iMaxRand )
{
int iRows,iColumns;
time_t t;
srand( time( &t ) );

for( iColumns=0;iColumns<iY;iColumns++ )
for( iRows=0;iRows<iX;iRows++ )
{
res[iColumns][iRows].iDocID=rand()%iMaxRand;
res[iColumns][iRows].iScore=rand()%iMaxRand;
}

}

void Out2DArray( RES res[][SIZE_X],int iX,int iY )
{
int iRows,iColumns;

for( iColumns=0;iColumns<iY;iColumns++ )
for( iRows=0;iRows<iX;iRows++ )
{
printf( &quot;Index %d-%d:\n&quot;,iColumns,iRows );
printf( &quot;iDocID=%d\n&quot;,res[iColumns][iRows].iDocID );
printf( &quot;iScore=%d\n\n&quot;,res[iColumns][iRows].iScore );
}

} Mike L.G.
mlg400@blazemail.com
 
Not just homework rwb1959...unfortunatly! Thanks for the input anyway!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top