C (Array) Final
C (Array) Final
Given the declaration `double currentBalance[91];`, let's identify the requested details:
****************
*****************
****************
**************
e. What are the indices of the first, middle, and the last elements?
3. Identify error(s), if any, in the following array declarations. If a statement is incorrect, provide the
correct statement
e. double[50] gpa;
This declaration is incorrect because the array size must be specified after the array name.
This declaration is incorrect because in C/C++, the size of an array must be a constant
integral value or an expression that evaluates to a constant integral value. LENGTH is
declared as a const double, which is not an integer type.
int alpha[50];
b. Initialize each component of alpha to -1.
int alpha[50];
for (int i = 0; i < 50; i++) {
alpha[i] = -1;
}
f. Use a for loop to output the value of a component of alpha if its index is a multiple of 2 or 3.
#include <iostream>
using namespace std;
int main(){
int alpha[50];
for(int j=0;j<50;j++)
{
cout<<"please enter value";
cin>>alpha[j];
}
for(int i=0;i<50;i++)
{
if(alpha[i]%2 ==0 ||alpha[i]%3==0)
cout << "alpha[" << i << "] = " << alpha[i] <<endl;
}
return 0;
}
for (int i = 2; i < 8; i++) { list[i] = list[i – 1] * list[i – 2]; if (i > 5) list[i] = list[i] - list[i - 1]; }
21. Consider the following overloaded function headings: (6) void printList(int list[], int size); void
printList(string sList[], int size); and the declarations: int ids[50]; double unitPrice[100]; string
birds[70]; Which of the following function calls is valid, that is, will not cause syntax or run time
error?
Conclusion:
Based on the analysis, the valid function calls that will not cause syntax or runtime errors
are:
a. printList(ids, 50);
b. printList(birds, 70);
e. printList(birds, 50);
28. When an array is passed as an actual parameter to a function, what is actually being passed?
When an array is passed as an actual parameter to a function in what is actually being passed is
the address of the first element of the array.
only a pointer to the first element of the array (the base address) is passed to the function. This
pointer allows the function to access and manipulate the elements of the array directly, even
though the function itself only receives a single pointer argument.
// Output:-
Cindy Blair
Chris Johnson
Sheila Mann
34. Given the declaration: char name[30]; mark the following statements as valid or invalid. If a
statement is invalid, explain why.
Validity: Invalid.
Explanation: Arrays in C/C++ cannot be assigned values using the assignment operator ( =) b
when they are not declared as pointers. Instead, you need to use functions like strcpy() to
copy strings into arrays.
Validity: Valid.
c. strcpy(name, "Jacksonville");
Validity: Valid.
Validity: Valid.
e. name[0] = 'K';
Validity: Valid.
Validity: Invalid.
Explanation: Comparing name with "Cynthia" using >= is invalid. In C/C++, comparing strings
using relational operators like >= or <= compares their addresses in memory, not their
contents. To compare strings lexicographically, you should use strcmp():
35. Given the declaration: char str1[20]; char str2[15] = "Fruit Juice"; mark the following statements
as valid or invalid. If a statement is invalid, explain why
a. strcpy(str1, str2);
valid
b. if (strcmp(str1, str2) == 0) cout << " str1 is the same as str2" << endl;
Valid
Invalid - In C++, you cannot assign one array to another directly like str1 = str2;. You
need to iterate through the elements and copy them one by one using strcpy or a loop.
d. if (str1 > str2) cout << "str1 > str2." << endl;
Valid
40. Suppose that array matrix is as defined in Exercise 39. Write C11 statements to accomplish the
following:
c. Output the contents of the first row and last column element of matrix.
d. Add 13.6 to the last row and last column element of matrix.
#include <iostream>
using namespace std;
int main(){
double array[4][3]={{2.5,3.2,6.0},{5.5, 7.5, 12.6},{ 11.25, 16.85, 13.45},{8.75, 35.65,
19.45}};
cout << "Enter numbers for the first row of the matrix:" << endl;
for(int i=0;i<3;i++)
cin>>array[0][i];
cout << "Contents of the last column of the matrix:" << endl;
for(int j =0;j<=3;j++)
cout<<array[j][2]<<endl;
cout << "Contents of the last column of the matrix:" << endl;
cout<<array[0][1]<<endl;
// Add 13.6 to the last row and last column element of matrix
array[4][3]=13.65;
cout << "Updated matrix:" << endl;
for (int a = 0; a < 4; a++) {
for (int s = 0; s <3; s++) {
cout << array[a][s] << " ";
}
cout << endl;
}
return 0;
}