NAME: _________________________________________________ 1.Write C++ statements t
ID: 640124 • Letter: N
Question
NAME: _________________________________________________
1.Write C++ statements to do the following:
a. Declare an array alpha of 15 components of type int.
b. Output the value of the tenth component of the array alpha.
c. Set the value of the fifth component of the array alpha to 35.
d. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteenth components of the array alpha.
e. Set the value of the fourth component of the array alpha to three times the value of the eighth component minus 57.
f. Output alpha so that five components per line are printed.
2.Given the declaration:
char name[8] = "Shelly";
Mark the following statements as
Explanation / Answer
a. Declare an array alpha of 15 components of type int.
int alpha[15];
b. Output the value of the tenth component of the array alpha.
cout<<a[10];
c. Set the value of the fifth component of the array alpha to 35.
a[5]=35;
d. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteenth components of the array alpha.
a[9]=a[6]+a[13]
e. Set the value of the fourth component of the array alpha to three times the value of the eighth component minus 57.
a[4]=(3*a[8])-57;
f. Output alpha so that five components per line are printed.
for(int i=1;i<alpha.length;i++){
if(i%5==0){
cout<<" ";
}
cout<<a[i];
}
2)
a. cout << name; yes
b. for (int j = 0; j < 6; j++)
cout << name[j]; yes
c. int j = 0;
while (name[j] != '') yes
cout << name[j++];
3)
a. Write a C++ statement that stores "Sunny Day" in str1.
str1[10]= "sunny day";
b. Write a C++ statement that stores the length of str1 into the int variable length.
int len = str1.length;
c. Write a C++ statement that copies the value of name into str2.
str[2]=str1[];
d. Write C++ code that outputs str1 if str1 is less than or equal to str2, and otherwise outputs str2.
if(str1.length<=str2.length){
cout<<str1
}
else{
cout<<str2;
}
4)
#include<iostream>
#include<cctype>
#include<iomanip>
using namespace std;
void getData(char& ticketType, int& row,
char& column);
void printForm(char form[][6], int row, char column);
int main()
{
char ch, ticketType, column;
int row;
char form[13][6];
cout << "This program assigns seats for a plane. "
<< "Do you want to start now? Y/y for yes, N/n for no." << endl;
cin >> ch;
ch = static_cast<char>(toupper(ch));
while(ch == 'Y')
{
getData(ticketType, row, column);
printForm(form, row, column);
cout << "This program assigns seats for a plane. "
<< "Do you want to start now? Y/y for yes, N/n for no." << endl;
cin >> ch;
ch = static_cast<char>(toupper(ch));
if(ch == 'N')
return 0;
}// end while
system("PAUSE");
return 0;
}
void getData(char& ticketType, int& row, char& column)
{
cout << "The airplane has 13 rows, with six seats in each row. " << endl;
cout << "Enter ticket type, "
<< "F for first class, "
<< "B for business class, "
<< "E for economy class:" << endl;
cin >> ticketType;
ticketType = static_cast<char>(toupper(ticketType));
while(ticketType != 'F' && ticketType != 'B' && ticketType != 'E')
{
cout << "Invalid ticket type." << endl;
cout << "Enter ticket type, "
<< "F/f for first class, "
<< "B/b for business class, "
<< "E/e for economy class:" << endl;
cin >> ticketType;
ticketType = static_cast<char>(toupper(ticketType));
}
switch(ticketType)
{
case 'F':
cout << "Row 1 and 2 are first class, " ;
break;
case 'B':
cout << "row 3 throuh 7 are business class, ";
break;
case 'E':
cout << "row 8 through 13 are economy class." << endl;
break;
}// end switch
cout << "Enter the row number you want to sit: " << endl ;
cin >> row;
cout << "Enter the seat number (from A to F). " << endl;
cin >> column;
column = static_cast<char>(toupper(column));
}
void printForm(char form[][6], int row, char column)
{
int i, j;
cout << "* indicates that the seat is available; " << endl;
cout << "X indicates that the seat is occupied. " << endl;
cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C"
<< setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl;
for(i = 0; i < 13; i++)
{
cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
for(j = 0; j < 6; j++)
{
if(i == row - 1 && j == static_cast<int>(column)-65)
cout << right << setw(6) << "X";
else
cout << right << setw(6) << "*";
}
cout << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.