Code:
#include <stdio.h>
#include <stdlib.h>
/*
* 1. Declare a structure which contains minimum two elements
* 2. in main function declair a local variable of the structure
* 3. read in the values for the structure elements (use scanf)
* 4. call a function print_data passing the local structure variable
* 5. print the values of the structure in the print_data
*/
struct petdataa
{
int idtag;
char name[20];
float weight;
};
//does not matter what you called your struct varitable name
//in main. as you see in your function you can "rename" it.
//just need to use the names in your prams now
//to reference it.
//it knows what to use because of your "data type"
//using the struct you declared name.
// size_t is for taking care of whatever data type that
//system uses for the way it is being called.
// long int etc...
//
void print_data(struct petdataa DATA[], size_t amout)
{
size_t count = 0;
while (count < amout)
{
printf("Name %s ID %d Weight %.02f\n", DATA[count].name,DATA[count].idtag,
DATA[count].weight);
count++;
}
}
int main()
{
int count = 0, amount = 0;
// bonus code
printf("Enter amount of records you want to fill in\n");
scanf(" %d", &amount);
//struct petdataa petdata[2];
// creates the array the size told at
//run time.
struct petdataa petdata[amount];
while ( count < amount )
{
printf("Enter a name, ID number and a weight in float notation.\n");
scanf(" %19s %d %f", petdata[count].name, &petdata[count].idtag,
&petdata[count].weight);
count++;
}
//pass your entire struct into your function,
//that is where your data values are at.
//
//you, the programmer already know what this
//function is going to be used for,
//it is you that is writing it
//for your use in your program.
//so use that to your advantage.
print_data(petdata, sizeof(petdata)/sizeof(petdata[0]) );
return 0;
}