7 - 2 - Arrays of Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Arrays of Structure

#include <iostream> cout << endl; // start on a new line


#include <iomanip> cout << left; // left-justify the output
#include <string> for ( i = 0; i < NUMRECS; i++)
using namespace std; cout << setw(7) << employee[i].id
<< setw(15) << employee[i].name
const int NUMRECS =5; // maximum number of records << setw(6) << employee[i].rate << endl;
struct PayRec // this is a global declaration
{ return 0;
int id; }
string name;
double rate;
};

int main()
{
int i;
PayRec employee[NUMRECS] = {
{ 32479, "Abrams, B.", 6.72 },
{ 33623, "Bohm, P.", 7.54},
{ 34145, "Donaldson, S.", 5.56},
{ 35987, "Ernst, T.", 5.43 },
{ 36203, "Gwodz, K.", 8.72 }
};
Define array of 100 structures for each of the data types described:

Declare a structure data type named Stemp for each of the following records:

1. A student record consisting of a student identification number, number of


credits completed, and cumulative grade point average.
struct Stemp
{
int idNum
int credits
double avg;
}

Stemp studentRec [100];


Declare a structure data type named Stemp for each of the following records:

2. A student record consisting of student’s name, date of birth, number of


credits completed and cumulative grade point average.
struct Stemp
{
char name[40];
int month;
int day;
int year;
int credits;
double avg;
};

Define array of 100 structures this data type

Stemp studentRec [100];


Declare a structure data type named Stemp for each of the following record:
3. An inventory record consisting of items shown below

Part Number: 2431


Integrated Circuit family TTL
Function Type AND
Supply voltage 5.0
Units in stock 345

struct Stemp
{
int partNum;
char circuitFamily[40];
char functionType[40];
double supplyVolts;
int unitsInStock;
};

Stemp icPart [100];;


A stock record consisting of the stock’s name, the price of the stock, and the
date of purchase

struct Stemp
{
char name[40];
double price;
char date[11]; // assuming the form is dd/mm/yyyy
};

Stemp stock [100];


An inventory record consisting of an integer part number, part description,
number of parts in inventory, and an integer reorder number.

struct Stemp
{
int partNum;
char desc[100];
int quant;
int reorder;
};

Stemp inven [100];


Using the data type
struct weekDays
{
string name;
sting dayNumber;
};
Define an array of 7 structures of type MonthDays. Name the array convert[], and initialize the array with
names of 7 days of the week with number of the day
weekDays convert[7] = {
{“Monday”, “First”}, {“Tuesday", “Second”}, {“Wednesday", “Third”},
{“Thursday", “Fourth}, {“Friday”, “Fifth”}, {“Saturday", “Seventh"}
{“Sunday", “Seventh”}
};

Include the array created in a program that displays the names and number of days in the week.
Include the array created in a program that displays the names and number of the days in the week.

#include <iostream>
using namespace std;

const int MAXCHARS = 10;


const int No_of_weekDays = 7;

struct WeekDays
{
char name[MAXCHARS];
char dayNumber[MAXCHARS];
};

int main()
{
WeekDays convert[No_of_weekDays] = { {"Monday", "First"}, {"Tuesday", "Second"},
{"Wednesday", "Third"},{"Thursday", "Fourth"},
{"Friday", "Fifth"}, {"Saturday", "Sixth"},
{"Sunday","Seventh"}
};
int i;

for(i = 0; i < No_of_weekDays; ++i)


cout << '\n' << convert[i].name << " is the "
<< convert[i].dayNumber << " day of the week";
cout << endl;

return 0;
}
Using the data type shown below write a program that accepts a number of the day in a week from a user in
numerical form and displays the name of the day and the number of the day of the week. Thus in response to an
input of 3, the program would display Wednesday is third day of the week.

struct MonthDays
{
string name;
string dayNumber;
};

You might also like