Wednesday, July 6, 2011

Arrays, Enums and Structures

Here is the review of arrays and the examples for structures and enums

#include <iostream>
#include <string>
using namespace std;


void ArrayReview();
void PrintArray(int myArray3[], int size);
void CharacterArrays();
void StructureExample();
void ArrayOfStructures();
void EnumerationExamples();

struct product
{
string productName;
double productWeight;
double Price;
};

enum colors
{
red, green, blue, purple, orange, yellow
};


int main()
{
//ArrayReview();
//CharacterArrays();
//StructureExample();
//ArrayOfStructures();

EnumerationExamples();
char c;
cin >> c;
}

void ArrayReview()
{
int myArray[4];
myArray[0]=2;
myArray[1]=14;
myArray[2]=5;
myArray[3]=9;

int myArray2[]={2, 4, 5, 6, 9};

PrintArray(myArray,4);


}

void PrintArray(int myArray3[], int size)
{
for (int i =0;i<size;i++)
{
cout << myArray3[i] << endl;
}
}

void CharacterArrays()
{
char city[50];

cout << "Enter a City Name" << endl;
cin.getline(city,50);

cout << "\n\nYour city is : " << city;

}

void StructureExample()
{
product poprocks;
poprocks.Price=1.25;
poprocks.productName="pop rocks";
poprocks.productWeight=3;

cout << "You bought 3 packages of " << poprocks.productName << endl;
cout << "that will be $"<<poprocks.Price * 3 << endl;
cout << "thank you " << endl;

}

void ArrayOfStructures()
{
product grocery[3];

product bread;
bread.Price=3.25;
bread.productName="Daves Killer bread";
bread.productWeight=12;

product chocolate;
chocolate.Price=3.79;
chocolate.productName="Theo Dark Chocolate";
chocolate.productWeight=2.75;

product steaksauce;
steaksauce.Price=4.10;
steaksauce.productName="A1";
steaksauce.productWeight=8;


grocery[0]=bread;
grocery[1]=chocolate;
grocery[2]=steaksauce;

double total=0;

for(int i=0;i<3;i++)
{
cout << "you bought " << grocery[i].productName << " at a price of "
<< grocery[i].Price << endl;
total +=grocery[i].Price;
}

cout << "Your total is $" << total << endl;

}

void UnionExample()
{
union test
{
double double_val;
int int_val;
};

cout << sizeof(long) << " bytes";

test test1;

test1.int_val=45;

test test2;

test2.double_val=3.45;

cout << test1.int_val / test2.double_val << endl;

}

void EnumerationExamples()
{
colors spectrum;
spectrum= orange;
cout << "Our color is " << spectrum << " the next color is " << spectrum + 1
<< "the previous color was " << spectrum-1 << endl;
}

No comments:

Post a Comment