0% found this document useful (0 votes)
21 views

C (Array) Final

The document discusses arrays in C/C++ including array declarations, passing arrays to functions, initializing and accessing array elements, and array operations. It provides examples of valid and invalid array code and asks questions to test understanding of arrays.

Uploaded by

jsksvoey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C (Array) Final

The document discusses arrays in C/C++ including array declarations, passing arrays to functions, initializing and accessing array elements, and array operations. It provides examples of valid and invalid array code and asks questions to test understanding of arrays.

Uploaded by

jsksvoey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Mark the following statements as true or false.

c. The size of an array is determined at compile time.


//True
Given the declaration:
int list[10]; /*the statement:*/ list[5] = list[3] + list[2];
//True
j. The declaration: char name[16] = "John K. Miller"; declares name to be an array of 15
characters because the string "John K. Miller" has only 14 characters.
//false.
l. As parameters, two-dimensional arrays are passed either by value or by reference.
//false.
2. Consider the following declaration: (1, 2) double currentBalance[91]; In this declaration, identify
the following:

Given the declaration `double currentBalance[91];`, let's identify the requested details:

a. The array name

The name of the array is `currentBalance`.

****************

b. The array size

The size of the array is 91.

*****************

c. The data type of each array component

The data type of each array component is `double`.

****************

d. The range of values for the index of the array

The range of values for the index of the array is from 0 to 90

**************

e. What are the indices of the first, middle, and the last elements?

- **First element**: The index of the first element is 0.

- **Middle element**: = ={91 - 1}/{2} = ( 45 )

- **Last element**: The index of the last element is 90.

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.

f. const double LENGTH = 26; double list[LENGTH - 1];

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.

6. Write C11 statement(s) to do the following:

a. Declare an array alpha of 50 components of type int.

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;
}

9. What is stored in list after the following C11 code executes?

int list[8]; list[0] = 1; list[1] = 2;

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]; }

Final Array list:

//list = [1, 2, 2, 4, 8, 32, 256, 7936]

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);

Matches void printList(int list[], int size);

b. printList(birds, 70);

Matches void printList(string sList[], int size);

e. printList(birds, 50);

Matches void printList(string sList[], int size);

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.

32. What is the output of the following C11 program segment?

// 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.

a. name = "Bill William";

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.

b. strcmp(name, "Tom Jackson");

Validity: Valid.

c. strcpy(name, "Jacksonville");

Validity: Valid.

d. cin >> name;

Validity: Valid.

e. name[0] = 'K';

Validity: Valid.

f. bool flag = (name >= "Cynthia");

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

c. if (strlen(str1) >= strlen(str2)) str1 = str2;

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:

a. Input numbers in the first row of matrix.

b. Output the contents of the last column of matrix.

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;
}

You might also like