Chapter 4

 

 

 


Text Box:  
Basic Array

#include <iostream.h>
#include <iomanip.h>

main()
{
	int i;
	
	/* An array is an ordered list defined by a sequence of numbers, 
characters, or nothing at all; */
	
	int dad[5] = {3, 4, 5, 2, 14}; //5 array elements and 5 initializers
	int puppy[5]={0};	// elements initialized to 0
	int mom[] = {6, 7, 8, 10, 6};	/* You don't need to insert the 
subscript if you insert the initializers, the program will 
interpret this array as one with 5 elements */

	for (i=0; i<=4; i++)
		puppy[i]=dad[i]+mom[i]; 

cout<<"Element in x"<<setw(20)<<"Element in a"<<setw(10)<<"SUM" 
    <<endl;
	
	for (i=0; i<=4; i++)
		cout<<setw(6)<<dad[i]<<setw(20)<<mom[i]<<setw(15)<<puppy[i]
    <<endl;

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Array With If Test

#include <iostream.h>

main()
{
	int i;
	
	int dalmatian[6];

	cout<<"Insert values into array"<<endl;

	for (i=0; i<6; i++)
		cin>>dalmatian[i]; 

	cout<<"Values greater than 10: ";
	for (i=0; i<6; i++)
		if (dalmatian[i]>10)
			cout<<dalmatian[i]<<" ";

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Subscripted Subscript

#include <iostream.h>
#include <math.h>

main()
{
	int i;
	
	int collie[]={1, 2, 3, 4, 5};
	int lassie[6]; 

	cout<<"Cubes: ";

	for (i=1; i<=5; i++)
		lassie[i]=pow (i, 3);

	cout<<"The cube of 3 is"<<endl;
	cout<<lassie[3]<<endl;
	
// Subscripted Subscript:
cout<<lassie [collie[2]]<<endl; /* This is the same as lassie[3] 
  since collie[2]=3 */

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Char Array

#include <iostream.h>
main()
{

	char name[] = "Ice";	/* Array name has 4 elements because all 
strings end with a null character ('\0') */
	
char name2[] = {'I', 'c', 'e', '\0'};
	
char breed[15]; // This array will take in a string
		/* You must insert a subscript for this array if you aren't 
going assign values at first */
		
	cout<<"Dog:"<<endl;
	cout<<name<<endl;
	cout<<name2<<endl;

	cout<<"\nInsert breed: ";
	cin>>breed;

	cout<<"\nA "<<breed<<"!"<<endl;
	
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Double-Subscripted Array

#include <iostream.h>
main()
{
	int classes[4][3]={{7,10,11},{7,6,12},{2,13,15},{2,5,18}};	
// {class hour, # girls, # boys)

	int countboys=0, countgirls=0, seventh=0;

	for (int i=0; i<=3; i++)
	{
		countboys+=classes[i][2];
		countgirls+=classes[i][1];
	}

	cout<<"Boys: "<<countboys<<endl;
	cout<<"Girls: "<<countgirls<<endl;

	for (i=0; i<=3; i++)
	{
		if (classes[i][0]==7)
			seventh++;
	}

	cout<<endl<<"Total number of 7th hour classes: "<<seventh<<endl
    <<endl;
	
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Array to Function

#include <iostream.h>

void funct (int[], int); /* This function takes in an array, and the size 
 of the array */

main()
{
	const int arraysize=10;	/* Constant variables cannot be modified in 					the middle of the program */

	int list [arraysize];

	funct (list, arraysize);

	return 0;
}

void funct (int array[], int size) 
{
	int chihuahua;
	
	cout<<"Insert values into array of size "<<size<<endl;

	for (chihuahua=0; chihuahua<size; chihuahua++)
		cin>>array[chihuahua];

	cout<<endl;
	
	for (chihuahua=0; chihuahua<size; chihuahua++)
		cout<<array[chihuahua]<<" ";
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Bubble Sort

#include <iostream.h>

main()
{
	int i, z, store;
	
	int labrador[]={6, 3, 1, 2, 9, 33, 2, 4};

	cout<<"LIST: ";
	for (i=0; i<=7; i++)
		cout<<labrador[i]<<" ";
	
	for (i=0; i<7; i++) /* Outter loop keeps track of what element will 
be compared to all other elements */
	{
for (z=0; z<7; z++) /* Inner loop keeps track of elements that 
will be compared to each I */
			if ( labrador[z] < labrador[z+1] )	/* Check if a 
  certain element is smaller than the one after it.  
  If it is, then they must switch places in the array */
			{
store = labrador[z]; /* store is an intermediate 
variable that holds an element of the array 
when it is being flopped with another 
element */ 
				
labrador[z]=labrador[z+1];
				labrador[z+1]=store;
			}
}
		
	cout<<"\nSORTED LIST (descending order): ";
	
for (i=0; i<=7; i++)
		cout<<labrador[i]<<" ";
	
	cout<<endl;
	
return 0;
}