Write one or more statements for each of the following problem situations. Assum
ID: 3689048 • Letter: W
Question
Write one or more statements for each of the following problem situations. Assume that each subscript ranges from 1 to its maximum value (rather than from 0 to its maximum value). (a) Sum the first n elements of the one-dimensional array Costs. (b) Sum all elements in column 3 of the two-dimensional array Values. Assume Values has 10 rows and 10 columns. (c) Sum all elements in row 5 of the two-dimensional array Values described in part (b). (d) Sum all elements in the first m rows and the first n columns of the two-dimensional array Values described in part (b). (e) Display the first 10 even elements in the one-dimensional string array Names; i.e., display Names(2), Names(4), . . ., Names(10). (f) Calculate the square root of the sum of the squares of the first 10 odd elements of the one- dimensional array X; i.e., calculate [X(1)2 + X(3)2 + X(5)2 + . . . + X(199)2]1/2. (g) Generate the elements of the two-dimensional array H, where each element of H is defined by the formula hij =1/(i+j 1) Assume that H has 8 rows and 12 columns. (h) Pass the array H generated in the last problem to a sub procedure called Search. Also, show the first line of the procedure definition. (i) A one-dimensional array K has n elements. Display the value of each subscript and each corresponding element for those elements whose values do not exceed Kmax. Display the output in two columns, with the value of the subscript in the first column and the corresponding subscripted variable in the second column. Label each column. (j) A two-dimensional array W has k rows and k columns. Calculate the product of the elements on the main diagonal of W, where the main diagonal runs from upper left to lower right. In other words, calculate W(1,1)*W(2,2)*W(3,3)*...*W(k,k). (l) Write a For Each-Next loop that determines the length of each string within Colors (described in the previous problem) and determines which element contains the shortest string. Display the result in a message box. Use the Len function to determine each string length.
Explanation / Answer
(a) Sum the first n elements of the one-dimensional array Costs
for(int i=1;i<=n;i++)
sum=sum+arr[i];
Cost of single for loop is O(n).Its cost is linear which grows with increase of n.
(b) Sum all elements in column 3 of the two-dimensional array Values. Assume Values has 10 rows and 10 columns.
col=3;
for(i=1;i<=10;i++)
sum=sum+arr[i][col];
Here the column is initialized to 3 to compute 3rd column element in each row.
(c) Sum all elements in row 5 of the two-dimensional array Values described in part (b).
row=5;
for(i=1;i<=10;i++)
sum=sum+arr[row][i];
Here row intial value is 5,in 5th row all elements are added.
d) Sum all elements in the first m rows and the first n columns of the two-dimensional array Values described in part (b).
m rows ,n columns
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
sum=sum+arr[i][j];
here first n values of row1 are added,then n values of row2 and so on upto row m.The cost is O(n2)
(e) Display the first 10 even elements in the one-dimensional string array Names; i.e., display Names(2), Names(4), . . ., Names(10).
for(i=2;i<=2;i+=2)
cout<<arr[i];
(f) Calculate the square root of the sum of the squares of the first 10 odd elements of the one- dimensional array X; i.e., calculate [X(1)2 + X(3)2 + X(5)2 + . . . + X(199)2]1/2.
for(i=1;i<=10;i++){
sum=sum+X[i]*X[i];
}
sum=sqrt(sum);
the cost is O(n)
g) Generate the elements of the two-dimensional array H, where each element of H is defined by the formula hij =1/(i+j 1) Assume that H has 8 rows and 12 columns
for(i=1;i<=8;i++)//8 rows
for(j=1;j<=12;j++)// for 12 columns
H[i][j]=1/(i+j-1);
All the elements are inserted to 2D array H,which takes O(n2)
h)Pass the array H generated in the last problem to a sub procedure called Search. Also, show the first line of the procedure definition.
Function definition first line:
int search(int **H){
}
function call statement in main program:
result=search(H);
I) A one-dimensional array K has n elements. Display the value of each subscript and each corresponding element for those elements whose values do not exceed Kmax. Display the output in two columns, with the value of the subscript in the first column and the corresponding subscripted variable in the second column. Label each column.
for(int i=1;i<=n;i++)
{ if(k[i]<kmax)
cout<<"subscript is"<<i<<"value is"<<k[i]<<endl;
}
(j) A two-dimensional array W has k rows and k columns. Calculate the product of the elements on the main diagonal of W, where the main diagonal runs from upper left to lower right. In other words, calculate W(1,1)*W(2,2)*W(3,3)*...*W(k,k)
product=1;//initial product value=1;
for(i=1;i<=k;i++)
product=product*W[i][i];
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.