Answered! 4. Given the declaration: char str1[21]; char str2[21];…

4. Given the declaration:

char str1[21];

char str2[21];

a. Write a C++ statement that stores “Sunny Day” in str1.

b. Write a C++ statement that stores the length of str1 into the int variable

length.

c. Write a C++ statement that copies the value of name into str2.

d. Write C++ code that outputs str1 if str1 is less than or equal to str2, and

otherwise outputs str2.

5. Define a two-dimensional array named temp of three rows and four columns of type int

such that the first row is initialized to 6, 8, 12, 9; the second row is initialized to 17, 5,

10, 6; and the third row is initialized to 14, 13, 16, 20.

Expert Answer

 4.

// a. Write a C++ statement that stores ” Sunny Day” in str1.
char str1[21] = “Sunny Day”;

// b. Write a C++ statement that stores the length of str1 into the int variable length.
int arrLength = strlen(str1);

// c. Write a C++ statement that copies your name into str2.
char str2[21];
strcpy(str2, str1);

// d. Write C++ code that outputs str1 if str1 is less than or equal to str2, and otherwise outputs str2.
if (strlen(str1) <= strlen(str2))
cout << str1 << endl;
else
cout << str2 << endl;

_________________

5.

Here is the program as per your requirement.

#include <iostream.h>
int main()
{
int r,c;
int temp[3][4] ={6,8,12,9,17,5,10,6,14,13,16,20}; // initializing

cout << “n Printing the array:”;
for(r=0;r <3 ; r++)
{
for(c=0; c<4;c++)
{
cout << temp[r][c] << ” “;
}
cout << endl;
}
cout << “n Printing all the elements in the last column of the array: n”;
for(r=0;r <3 ; r++)
{
for(c=0; c<4;c++)
{
if(c==3) // if it reached last column then print.
cout << temp[r][c] << ” “;
}
cout << endl;
}
int sum=0;
cout << “n Printing the sum of all elements in the first row of the array:”;
for(r=0;r <3 ; r++)
{
for(c=0; c<4;c++)
{
if(r==0) // if it is first row add
sum = sum + temp[r][c];

}
// cout << endl;
}
cout << “nSum : ” << sum;;

return 0;
}

Output:

Still stressed from student homework?
Get quality assistance from academic writers!