0% found this document useful (0 votes)
4 views

Chap 7 Examples

The document contains multiple C++ programs that demonstrate the use of arrays, including one-dimensional and two-dimensional arrays, as well as structures. It covers various operations such as inputting and displaying data, calculating averages, and copying strings. The examples illustrate fundamental programming concepts and array manipulations in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chap 7 Examples

The document contains multiple C++ programs that demonstrate the use of arrays, including one-dimensional and two-dimensional arrays, as well as structures. It covers various operations such as inputting and displaying data, calculating averages, and copying strings. The examples illustrate fundamental programming concepts and array manipulations in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

#include <iostream>

using namespace std;


int main()
{
int age[4]; //array ‘age’ of 4 ints
for(int j=0; j<4; j++) //get 4 ages
{
cout << "Enter an age: ";
cin >> age[j]; //access array element
}
for(int j=0; j<4; j++) //display 4 ages
cout << "You entered " << age[j] << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 6; //size of array
double sales[SIZE]; //array of 6 variables
cout << "Enter widget sales for 6 days\n";
for(int j=0; j<SIZE; j++) //put figures in array
cin >> sales[j];
double total = 0;
for(int j=0; j<SIZE; j++) //read figures from array
total += sales[j]; //to find total
double average = total / SIZE; // find average
cout << "Average = " << average << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int month, day, total_days;
int days_per_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
cout << "\nEnter month (1 to 12): "; //get date
cin >> month;
cout << "Enter day (1 to 31): ";
cin >> day;
total_days = day; //separate days
for(int j=0; j<month-1; j++) //add days each month
total_days += days_per_month[j];
cout << "Total days from start of year is: " << total_days
<< endl;
return 0;
}
#include <iostream>
#include <iomanip> //for setprecision, etc.
using namespace std;
const int DISTRICTS = 4; //array dimensions
const int MONTHS = 3;
int main()
{
int d, m;
double sales[DISTRICTS][MONTHS]; //two-dimensional array
//definition
cout << endl;
for(d=0; d<DISTRICTS; d++) //get array values
for(m=0; m<MONTHS; m++)
{
cout << "Enter sales for district " << d+1;
cout << ", month " << m+1 << ": ";
cin >> sales[d][m]; //put number in array
}
cout << "\n\n";
cout << " Month\n";
cout << " 1 2 3";
for(d=0; d<DISTRICTS; d++)
{
cout <<"\nDistrict " << d+1;
for(m=0; m<MONTHS; m++) //display array values
cout << setiosflags(ios::fixed) //not exponential
<< setiosflags(ios::showpoint) //always use point
<< setprecision(2) //digits to right
<< setw(10) //field width
<< sales[d][m]; //get number from array
} //end for(d)
cout << endl;
return 0;
} //end main
#include <iostream>
#include <iomanip> //for setprecision, etc.
using namespace std;
const int DISTRICTS = 4; //array dimensions
const int MONTHS = 3;
int main()
{
int d, m;
//initialize array elements
double sales[DISTRICTS][MONTHS]
= { { 1432.07, 234.50, 654.01 },
{ 322.00, 13838.32, 17589.88 },
{ 9328.34, 934.00, 4492.30 },
{ 12838.29, 2332.63, 32.93 } };
cout << "\n\n";
cout << " Month\n";
cout << " 1 2 3";
for(d=0; d<DISTRICTS; d++)
{
cout <<"\nDistrict " << d+1;
for(m=0; m<MONTHS; m++)
cout << setw(10) << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint) << setprecision(2)
<< sales[d][m]; //access array element
}
cout << endl;
return 0;
}
#include <iostream>
#include <iomanip> //for setprecision, etc.
using namespace std;
const int DISTRICTS = 4; //array dimensions
const int MONTHS = 3;
void display( double[DISTRICTS][MONTHS] ); //declaration
//--------------------------------------------------------------
int main()
{ //initialize two-dimensional array
double sales[DISTRICTS][MONTHS]
= { { 1432.07, 234.50, 654.01 },
{ 322.00, 13838.32, 17589.88 },
{ 9328.34, 934.00, 4492.30 },
{ 12838.29, 2332.63, 32.93 } };
display(sales); //call function; array as argument
cout << endl;
return 0;
} //end main
//--------------------------------------------------------------
//display()
//function to display 2-d array passed as argument
void display( double funsales[DISTRICTS][MONTHS] )
{
int d, m;
cout << "\n\n";
cout << " Month\n";
cout << " 1 2 3";
for(d=0; d<DISTRICTS; d++)
{
cout <<"\nDistrict " << d+1;
for(m=0; m<MONTHS; m++)
cout << setiosflags(ios::fixed) << setw(10)
<< setiosflags(ios::showpoint) << setprecision(2)
<< funsales[d][m]; //array element
} //end for(d)
}
#include <iostream>
using namespace std;
const int SIZE =4; //number of parts in array
////////////////////////////////////////////////////////////////
struct part //specify a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{
int n;
part apart[SIZE]; //define array of structures
for(n=0; n<SIZE; n++) //get values for all members
{
cout << endl;
cout << "Enter model number: ";
cin >> apart[n].modelnumber; //get model number
cout << "Enter part number: ";
cin >> apart[n].partnumber; //get part number
cout << "Enter cost: ";
cin >> apart[n].cost; //get cost
}
cout << endl;
for(n=0; n<SIZE; n++) //show values for all members
{
cout << "Model " << apart[n].modelnumber;
cout << " Part " << apart[n].partnumber;
cout << " Cost " << apart[n].cost << endl;
}
return 0;
}
#include <iostream>
using namespace std;

int main()
{
const int MAX = 80;
char str[MAX];

cout << "Enter a string:";


cin >> str;

cout << "You entered:" << str << endl;


return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
const int MAX = 20;
char str[MAX];

cout << "\nEnter a string:";


cin >> setw(MAX) >> str;

cout << "You entered: " << str << endl;


return 0;
}
#include <iostream>
using namespace std;

int main()
{
const int MAX = 80;
char str[MAX];

cout << "\nEnter a string:";


//cin.get(str, MAX);
//cin >> str;
cout << "You entered:" << str << endl;
return 0;
}
#include <iostream>
using namespace std;

const int MAX = 2000;


char str[MAX];

int main()
{
cout << "\nEnter a string:\n";
cin.get(str, MAX, '$');
cout << "you entered:\n" << str << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
char str1[] = "Oh, Captain, my Captain!"
"Our fearful trip is done";

const int MAX = 80;


char str2[MAX];

for(int j=0; j<strlen(str1); j++)


str2[j] = str1[j];
str2[j] = '\0';
cout << str2 << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
char str1[] = "Tiger, tiger, burning bright\n"
"In the forests of the night";
const int MAX = 80;
char str2[MAX];

strcpy(str2, str1);
cout << str2 << endl;
return 0;
}
#include <iostream>
using namespace std;

int main ()
{
const int DAYS = 7;
const int MAX = 10;

char star[DAYS][MAX] = {"Sunday", "Monday", "Teusday", "Wednesday",


"Thursday", "Friday", "Saturday" };

for(int j=0; j<DAYS; j++)


cout << star[j] << endl;
return 0;
}

You might also like