Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

ASSIGNMENT - 13

Course name : Programming Fundamental


Department : Software Engineering
Submitted by : Zara Khush Bakhat
Submitted to : Ms Aasma Akram
Submitted on : 11- March - 2023

Page 1|6
7.6(FILL IN THE BLANKS)

a) goes out of the scope and is destroyed.

b) static or global array.

c) list of values.

d) p*q

e) fractions[6][3]

7.7 (true or false) explain why

a) The vector member function at provides bounds checking and throws an exception if its
argument is an invalid subscript.

True.

The std::vector class provides the at member function, which provides bounds checking on the input
argument and throws an std::out_of_range exception if the argument is an invalid subscript.

b) the value of an element of a vector can be accessed or modified using curly brackets ({})

True.

Page 2|6
In C++, the value of an element of a vector can be accessed or modified using the subscript operator [] or
using curly brackets {}. This is because std::vector overloads both operators to provide access to its
elements.

c) to indicate reserve 50 locations for integer array p , you write p[50];

False

In C++, to indicate that you want to reserve space for an integer array p with 50 locations, you would use
the std::vector class and call its reserve member function

d) A for statement must be used to initialize the element of a 25 element array to one.

False.

In C++, you can use various methods to initialize the elements of an array to a specific value, and a for
loop is not necessarily required.

e) Two nested for statements must be used to total the elements of a three dimensional array.

False

If you want to sum the elements of a three-dimensional array in C++, you need to use two nested for
loops to iterate through each element of the array. The outer for loop iterates over the first dimension of
the array, while the inner for loops iterate over the second and third dimensions of the array.

7.8(write c++ statements)

a) display the value of element 3 of character array vowels.

std::cout << vowels[3];

b) read the value from the keyboard for the element 2 of one dimentional integer array marks.

cin >> marks[1]

c) initialize each of the 4 elements of one dimentioal integer array g to 7.

int g[4] = {7, 7, 7, 7};

d) Total and display the elements of floating point array c of 200elements.

float c[200];

float total = 0.0;

for (int i = 0; i < 200; i++) {

total += c[i];

Page 3|6
cout << "Total = " << total << endl;

e) copy array a into the first portion of array b Assume that both arrays contain doubles and that
arrays a and b have 17 and 41 elements, respectively.

for (int i = 0; i < 17; i++) {

b[i] = a[i];

7.9 (TWO DIMENTIONAL ARRAY QUESTIONS)

Consider a 2-by-3 integer array t.


a. Write a declaration for t.
int t[ 2 ][ 3 ];
b. How many rows does t have?
2
c. How many columns does t have?
3
d. How many elements does t have?
6
e. Write the names of all the elements in row 1 of t.
t[ 0 ][ 0 ], t[ 0 ][ 1 ], t[ 0 ][ 2 ]
f. Write the names of all the elements in the column 2 of t.
t[ 0 ][ 1 ], t[ 1 ][ 1 ]
g. Write a single statement that sets the element of t in row 1 and column 2 to zero.
t[ 0 ][ 1 ] = 0;
h. Write a series of statements that initialize each element of t to zero. Do not use a loop.
t[2][3] = {{0,0,0},{0,0,0}};
i. Write a nested counter controlled for statement that initializes each element of t to zero.
for ( int i = 0; i < 2; ++i ){
for ( int j = 0; j < 3; ++j )
t[ i ][ j ] = 0;
}
j. Write a nested range base for statements that initialize each elements of t to zero.
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
t[i][j] = 0;
}
}

k. Write a statement that input the values for the elements of t from the keyboard.
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter value for t[" << i << "][" << j << "]: ";
cin >> t[i][j];
}
}

l. Write a series of statements that determine and display the smallest value in array t.
int smallest = t[ 0 ][ 0 ];
Page 4|6
for ( int r = 1; r < 2; ++r ) {
for ( int c = 1; c < 3; ++c ){
if ( t[ r ][ c ] < smallest )
smallest = t[ r ][ c ];
cout << smallest;
}}
m. Write a statement that displays the elements of the 0 row of t
cout << t[ 0 ][ 0 ] << ’ ’ << t[ 0 ][ 1 ] << ’ ’ << t[ 0 ][ 2 ] << endl;
n. Write a statement that totals the elements of the column 2 of t.
int sum = 0;
for (int i = 0; i < 2; i++) {
sum += t[i][1];
}
o. Write a series of statements that prints the array t in neat, tabular format. List the column
subscripts as headings across the top and list the row subscripts at the left of each row.
cout << " 0 1 2"<<endl;
for ( int r = 0; r < 2; ++r )
{ cout << r << ’ ’;
for ( int c = 0; c < 3; ++c )
cout << t[ r ][ c ] << " ";
cout << ’\n’; }

7.11(one dimensional array question)


write statement that perform the following one dimensional array operations :
a. initialize the 12 elements of int array items to 6.
int items[12] ={6,6,6,6,6,6,6,6,6,6,6,6};
B) Add 6 to first six elements of int array items.
for (int i = 0; i < 6; i++) {
items[i] += 6;
}
b. Read next 6 values from the user and store in the int array items.
cout << "Enter 6 integer values: ";
for (int i = 0; i < 6; i++) {
cin >> items[i];
}
c. Display the 12 values of int array items in column format with 4 values in one row, the next 4
values in the next row , and so on.
for (int i = 0; i < 12; i++) {
if (i % 4 == 0 && i != 0) {
cout << endl;
}
cout << arr[i] << "\t";
}

7.12(FIND THE ERRORS)


a) Assume that matrix is an array of integers with 3 rows and 3 column;
cin>>matrix [3] [2];
array is 3 by 3 so matrix[3][3], is correct .
b) Assume that matrix is an array of integers with 3 rows and 2 column;
matrix [3] []= 10;
Colum is not defined .
Page 5|6
c) array <double,>rate{22.5,9.5,30.0,5.0,7.5};
The error in the given code is the extra comma inside the angle brackets after the data type 'double'.
The correct syntax for declaring an array of double values is as follows:
array <double,5>rate{22.5,9.5,30.0,5.0,7.5};
Note that the number 5 inside the angle brackets represents the size of the array and needs to be
specified.
d) Assume that marls in an array of 5 integers.
Cout<<marks[5];

7.15(two dimensional array initialization)


Label the elements of 3-by-5 two dimentional array sales to indicate the order in which they are set
to zero by the following program segment:
for ( size-t row {0}; row < sales.size(); ++row )
{
for (size-t column {0}; column < sales[row]; ++column )
{
sales[ row ][ column ] = 0;
}
}
sales[ 0 ][ 0 ] =0 ;
sales[ 0 ][ 1 ]=0;
sales[ 0 ][ 2 ]=0 ;
sales[ 0 ][ 3 ]=0;
sales[ 0 ][ 4 ]= 0;
sales[ 1 ][ 0 ]=0;
sales[ 1 ][ 1 ]=0;
sales[ 1 ][ 2 ]=0;
sales[ 1 ][ 3 ]=0;
sales[ 1 ][ 4 ]=0;
sales[ 2 ][ 0 ]=0;
sales[ 2 ][ 1 ]=0;
sales[ 2 ][ 2 ]=0;
sales[ 2 ][ 3 ]=0;
sales[ 2 ][ 4 ]=0;

Page 6|6

You might also like